Notifications
Clear all

[Closed] Creating Effect of "Gravity" Between Objects

Well I thought of this idea when I realised that there was nothing that could truly calculate things like planetary gravity and orbital motion so I set out to write a script that does exactly this. I hope that in the future I can incorporate many more features but for now I want it to calculate gravity between objects. I’m honestly completely stuck. I wrote a script and it makes one object repel the other. What I want to do is to create an acceleration vector (Removes need to use objects’ own mass) then using the time and the acceleration vector calculate the displacement of the object and to move it by how much in what direction in all three axes.

Here’s the code I wrote:

(
	local GravFloater
rollout GravRollout "Grav Calc" width:200 height:300
(
	spinner spn1 "" pos:[140,130] width:40 height:16 range:[0,100,0]
	label M "Mass of Object 1" pos:[15,130] width:115 height:20
	label G "Mass of Object 2" pos:[15,160] width:115 height:20
	spinner spn2 "" pos:[140,160] width:40 height:16 range:[0,100,0]
	button Run "Run" pos:[14,192] width:166 height:50
	button btn19 "Close" pos:[14,250] width:166 height:41
	on Run pressed  do
(
		t=animationRange.end

		A=6.673*10^(-11)

		if t<animationRange.end+1 do(

		for t=1 to animationRange.end+1 do(

		d=sqrt(($Sphere002.pos.x-$Sphere001.pos.x)^2+($Sphere002.pos.y-$Sphere001.pos.y)^2+($Sphere002.pos.x-$Sphere001.pos.y)^2)

		with animate on move $Sphere002 ($Sphere002.pos-[$Sphere002.pos.x-$Sphere001.pos.x,$Sphere002.pos.y-$Sphere001.pos.y,$Sphere002.pos.z-$Sphere001.pos.z])

		sliderTime=t)
	)
)
)
on Execute do (
	GravFloater=NewRolloutFloater "God" 200 350
	addRollout GravRollout GravFloater
)
)

It just doesn’t work. I am aware of the fact that I’ve limited the code to two objects but that’s because I would rather get two objects working than none at all. I really need help with this because I want to create a realistic simulation of orbits and gravitational movement without using PFlow since Particle Flow cannot simulate gravity accurately and when trying to it ends up not behaving in a “normal” way because there is no interparticle attraction.

If I could list what I wanted to extend this idea I think I’d be burying myself in fantasyland so I’m only limiting my endeavours to gravitational attraction.

P.S. Later on I want to add fracturing of objects upon collision by utilising the FractureVoronoi Script.

3 Replies
 3ak

Hi.
If you’re trying to mimic simple Newton’s law of universal gravitation then you have some problems.
The first one is that your G (A in your case) = 0 cause 10^-11 is too small for 3ds max (maybe there is some 64 bit float i don’t know).

Here is my simple example, just enlarge you animation range to 500-1000 frames and run it and you’ll end up with 5 spheres moving:
(video preview – http://dl.dropbox.com/u/46208032/3ak_gravity.zip


v = 0
G = 6.673*10.0^(-2)
spheresNum = 5
spheres = #()
smass = #()
svel = #()
for i  = 1 to spheresNum do 
	(	
		spheres [i] = sphere pos: (random [-50,-50,-50] [50,50,50]) radius: 5
		smass[i] = 4/3 * PI*(spheres[i].radius^3)
		svel[i] = random [-v,-v,-v] [v,v,v]
		
	)
	
animate on
for t = animationRange.start to animationRange.end do
	(	
		slidertime = t
		for i = 1 to spheresNum do
		(
			a = 0
			for j = 1 to spheresNum where i != j do
				(
					da = (G*smass[j] / ((distance spheres[i] spheres[j])^2))
					if da > (G*smass[j] / ((spheres[i].radius + spheres[j].radius)^2)) then da = (G*smass[j] / ((spheres[i].radius + spheres[j].radius)^2))
					a +=da* (normalize(spheres[j].pos - spheres[i].pos))
				)
			svel[i] += a / framerate
			
			move spheres[i] svel[i]
		)
			
	)	
	
animate off

I like the way you implemented the gravitational pull but I want it to stop the object when it contacts the other object. To do that I “tried” to put into effect that when the distance is greater than the sum of the radii of the two spheres it stops the object.

1 Reply
 3ak
(@3ak)
Joined: 11 months ago

Posts: 0

ok. here is modified version.i added bounce factor and add/substract acceleration depending on distance:

bounce = 0.4
v = 0
G = 6.673*10.0^(-2)
spheresNum = 30
spheres = #()
smass = #()
svel = #()
for i  = 1 to spheresNum do 
	(	
		spheres [i] = sphere pos: (random [-50,-50,-50] [50,50,50]) radius: 5
		smass[i] = 4/3 * PI*(spheres[i].radius^3)
		svel[i] = random [-v,-v,-v] [v,v,v]
		
	)
	
animate on
for t = animationRange.start to animationRange.end do
	(	
		slidertime = t
		for i = 1 to spheresNum do
		(
			a = 0
			for j = 1 to spheresNum where i != j do
				(
					da = (G*smass[j] / ((distance spheres[i] spheres[j])^2))
					if da >= (G*smass[j] / ((spheres[i].radius + spheres[j].radius)^2)) then  a -=da*bounce* (normalize(spheres[j].pos - spheres[i].pos)) else a +=da* (normalize(spheres[j].pos - spheres[i].pos))
				)
			svel[i] += a / framerate
			stopmoving = false
			
			move spheres[i] svel[i]
		)
			
	)	
	
animate off