Orbiting and Body Physics
-
- Prole
- Posts: 19
- Joined: Mon Dec 19, 2011 11:17 pm
Orbiting and Body Physics
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 566 times
- josefnpat
- Inner party member
- Posts: 955
- Joined: Wed Oct 05, 2011 1:36 am
- Location: your basement
- Contact:
Re: Orbiting and Body Physics
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
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
Re: Orbiting and Body Physics
What else did you expect? That Newton would turn out to have been wrong all along?
Re: Orbiting and Body Physics
If you hold and drag the love window for a few seconds, you throw the earth out of the suns orbit into the heavens
Re: Orbiting and Body Physics
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:
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?
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
Anybody know how to set the X and Y velocities proportionally to the angle between the two bodies?
Re: Orbiting and Body Physics
Hello FreeTom.
Just get the 'vector' between the two centers of mass:
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:
The force of gravity is inversely proportional to the distance:
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:
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:
Next, multiply the normalized vector by the acceleration:
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.
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.
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
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)
Code: Select all
force = (G*planetMass*starMass)/dist^2
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
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
Code: Select all
acceleration1x = nx*acceleration1
acceleration1y = ny*acceleration1
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
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.
Re: Orbiting and Body Physics
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.
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.
Who is online
Users browsing this forum: Ahrefs [Bot] and 1 guest