Orbiting and Body Physics

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
AlgidPlasma
Prole
Posts: 19
Joined: Mon Dec 19, 2011 11:17 pm

Orbiting and Body Physics

Post by AlgidPlasma »

Just made a small demo that shows the interaction of gravity between two objects in a friction less environment. Turned out much better than expected. The planet and star are the mass of the earth and sun respectively. Feel free to change the mass and distance multiplication to see how it affects the orbit ^^ .
Attachments
orbit.love
(841 Bytes) Downloaded 553 times
User avatar
josefnpat
Inner party member
Posts: 955
Joined: Wed Oct 05, 2011 1:36 am
Location: your basement
Contact:

Re: Orbiting and Body Physics

Post by josefnpat »

This is rather nifty. You ought to consider making it into a library where you can add multiple elements that all attract each other.
Missing Sentinel Software | Twitter

FORCIBLY IGNORED.
<leafo> when in doubt delete all of your code
<bartbes> git rm -r *
<bartbes> git commit -m "Fixed all bugs"
<bartbes> git push
iemfi
Citizen
Posts: 52
Joined: Fri Mar 30, 2012 11:03 am

Re: Orbiting and Body Physics

Post by iemfi »

What else did you expect? That Newton would turn out to have been wrong all along? :P
rdlaitila
Prole
Posts: 8
Joined: Tue Nov 01, 2011 5:26 pm

Re: Orbiting and Body Physics

Post by rdlaitila »

If you hold and drag the love window for a few seconds, you throw the earth out of the suns orbit into the heavens :ultrahappy:
FreeTom
Prole
Posts: 8
Joined: Mon Aug 17, 2015 1:08 pm

Re: Orbiting and Body Physics

Post by FreeTom »

Reviving a long-dead thread here, I know, but this isn't quite right.

I've been trying to make orbitty things and I've been doing it exactly this kind of wrong and was hoping this code would correct me. Alas, no. Here's how the velocity is updated:

Code: Select all

	if planet.position.x < star.position.x then
		planet.velocity.x = planet.velocity.x + attraction * dt
	elseif planet.position.x >= star.position.x then
		planet.velocity.x = planet.velocity.x - attraction * dt
	end	
	
	if planet.position.y < star.position.y then
		planet.velocity.y = planet.velocity.y + attraction * dt
	elseif planet.position.y >= star.position.y then
		planet.velocity.y = planet.velocity.y - attraction * dt
	end
So basically the force of gravity is only ever applied in one of the four diagonal directions instead of at angle directly towards the star's centre of gravity as it should be. Playing around with this in more complex interactions (as I have been) it is sometimes very noticeable.

Anybody know how to set the X and Y velocities proportionally to the angle between the two bodies?
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Orbiting and Body Physics

Post by ivan »

Hello FreeTom.
Just get the 'vector' between the two centers of mass:

Code: Select all

vx = star.position.x - planet.position.x
vy = star.position.y - planet.position.y
You now have a vector pointing from the planet to the star.
Negate vx, yv if you want to rotate the vector 180 degrees.
Once you know the vector between the two objects, you can gen the distance:

Code: Select all

dist = math.sqrt(vx^2 + vy^2)
-- or --
dist = math.sqrt(vx*vx + vy*vy)
The force of gravity is inversely proportional to the distance:

Code: Select all

force = (G*planetMass*starMass)/dist^2
We already know these variables except for G which is the gravitational constant.
If you assume that G = 6.673 x 10^-11 N m^2/kg^2
then make sure "dist" is in meters and "mass" is in kg.

So far so good, all we have to do is apply the force to both objects.
The next formula to remember is:

Code: Select all

force = mass*acceleration
-- so --
acceleration1 = force/planetMass
acceleration2 = force/starMass
You now know the acceleration of the two objects.

Note:Acceleration is the change in velocity per 1 second.
Once you know the acceleration caused by gravity,
you have to apply it along the axis of the vector vx, vy.
First, normalize vx, vy:

Code: Select all

nx, ny = 0, 0
if dist > 0 then 
  nx, ny = vx/dist, vy/dist
end
Next, multiply the normalized vector by the acceleration:

Code: Select all

acceleration1x = nx*acceleration1 
acceleration1y = ny*acceleration1 
This is your change in velocity per second.
So we have to multiply by "delta" for each step.

Last step:
add the change in velocity (acceleration*dt) to the current velocity of each object.

Code: Select all

planet.velocity.x = planet.velocity.x + acceleration1x*dt
planet.velocity.y = planet.velocity.y + acceleration1y*dt
Keep in mind that the two objects will move at different velocities
since the more massive object will be slower to accelerate.

TL;DR
Just get the vector between the two objects, normalize it
and multiply by the force of attraction.
Last edited by ivan on Thu Aug 20, 2015 7:09 am, edited 1 time in total.
FreeTom
Prole
Posts: 8
Joined: Mon Aug 17, 2015 1:08 pm

Re: Orbiting and Body Physics

Post by FreeTom »

Aha! Thanks, Ivan - probably should have been able to work that out myself.

For any (other) beginners who stumble upon this and wonder what exact difference is might make, here's the comparison of the final resting positions from my script in which lots of little circles gravitate towards a big one:

https://drive.google.com/file/d/0BzPXgv ... sp=sharing

'Four-angle' gravity on the left, vector-based gravity on the right.

Mmmm, circular.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 70 guests