Gravity
How do I update the location and velocity (state-vector in NASA/Apollo parlance) of a spacecraft, taking into account multiple gravitational bodies and other forces such as a rocket engine?
Being more of a programmer than a mathematician, I don’t understand much of the notation normally used to answer maths questions, so it’d be great if someone could give me an idea of the solution using C/C++ code.
You’ll find the address to a header file below to show how far I’ve progressed (not far and probably wrong) - I don’t trust the GameDev SOURCE tag to preserve my code formatting.
Thanks for any help,
Justin.
www.justin.nixon.btinternet.co.uk/Physics.h
[edited by - Justin Nixon on December 11, 2002 1:48:45 PM]
[edited by - Justin Nixon on December 11, 2002 1:49:12 PM]
December 11, 2002 02:47 PM
Just add the acceleration (or force) vectors together to get the total effect on the body.
I understand that - wasn''t it clear from my code?
How do I calculate the acceleration for each force?
Here''s a site I found which explains gravity it great detail (http://hyperphysics.phy-astr.gsu.edu/hbase/grav.html), but as it''s all in mathematical notation and doesn''t explain to a maths dunce like me how it relates to time slices for use in a simulator, it''s not much use to me.
Can anyone help?
Justin.
How do I calculate the acceleration for each force?
Here''s a site I found which explains gravity it great detail (http://hyperphysics.phy-astr.gsu.edu/hbase/grav.html), but as it''s all in mathematical notation and doesn''t explain to a maths dunce like me how it relates to time slices for use in a simulator, it''s not much use to me.
Can anyone help?
Justin.
The acceleration due to gravity from a planet or moon or whatever on your rocket can be calculated from
|g| = (G Mp / r2 ) ms-2
where Mp is the mass of the planet and r is the distance from the planet''s centre. The direction of the acceleration is obviously towards said planet.
Each frame you need to add this acceleration to the rocket''s velocity. Note the above quantity is in ms-2 whereas you might be calculating it every 1/100 seconds.
|g| = (G Mp / r2 ) ms-2
where Mp is the mass of the planet and r is the distance from the planet''s centre. The direction of the acceleration is obviously towards said planet.
Each frame you need to add this acceleration to the rocket''s velocity. Note the above quantity is in ms-2 whereas you might be calculating it every 1/100 seconds.
Actually technically speaking the gravitational acceleration
between any two masses is:
F = G M1 M2 / r^2
Where:
G = 6.67 x 10^-11
M1 = Mass of the first object (kg)
M2 = Mass of the second object (kg)
r = The distance between the center of mass points of the two objects.
F = the force on both objects.
The fact that f = MA means obvious the bigger mass has an
insignificant acceleration.
Now, as to the original question: As far as I''m aware the
basic N body gravitational problem is not solvable
mathematically.
However, it can be done using a model. Say you have a bunch of
point mass values simulated in your 3D space. Every "update"
they move according to their velocity and have their velocity
updated due to the graviational attraction of all other point
mass objects.
Every update the gravitational force from every object is
applied to every other object in the simulation (N^2 order).
This generates the velocity vectors for the next update.
As you decrease the time interval of your update, the accuracy
of the model improves, however, it takes longer to calculate
the path of an object as it moves.
That''s a very basic N body gravity simulation.
There are other ways...but it''s hard. For instance if you have
only got one body moving, and the others fixed in space
(impossible, but good for a relative motion simulation say)
you can build a "gravitational map" of the space, where every
point is indexed by the magnitude and direction of the
acceleration the moving mass would get at that point
(again to increase accuracy increase number of points in
the grid). You can then map multiple paths through that
graivationaly vector space with some difficulty, but it
makes calculating a variety of different paths easier
I guess (someone was telling me about it the other day,
never done it myself).
between any two masses is:
F = G M1 M2 / r^2
Where:
G = 6.67 x 10^-11
M1 = Mass of the first object (kg)
M2 = Mass of the second object (kg)
r = The distance between the center of mass points of the two objects.
F = the force on both objects.
The fact that f = MA means obvious the bigger mass has an
insignificant acceleration.
Now, as to the original question: As far as I''m aware the
basic N body gravitational problem is not solvable
mathematically.
However, it can be done using a model. Say you have a bunch of
point mass values simulated in your 3D space. Every "update"
they move according to their velocity and have their velocity
updated due to the graviational attraction of all other point
mass objects.
Every update the gravitational force from every object is
applied to every other object in the simulation (N^2 order).
This generates the velocity vectors for the next update.
As you decrease the time interval of your update, the accuracy
of the model improves, however, it takes longer to calculate
the path of an object as it moves.
That''s a very basic N body gravity simulation.
There are other ways...but it''s hard. For instance if you have
only got one body moving, and the others fixed in space
(impossible, but good for a relative motion simulation say)
you can build a "gravitational map" of the space, where every
point is indexed by the magnitude and direction of the
acceleration the moving mass would get at that point
(again to increase accuracy increase number of points in
the grid). You can then map multiple paths through that
graivationaly vector space with some difficulty, but it
makes calculating a variety of different paths easier
I guess (someone was telling me about it the other day,
never done it myself).
quote:
Original post by Shadow Mint
Actually technically speaking the gravitational acceleration
between any two masses is:
F = G M1 M2 / r^2
Actually that is the force between the two masses.
quote:
Original post by Shadow Mint
Now, as to the original question: As far as I''m aware the
basic N body gravitational problem is not solvable
mathematically.
But he just wants the overall acceleration of the rocket, doesn''t he? In a game, you wouldn''t normally be concerned with how the planets (or other heavy bodies) are interacting with each other.
So, I think the simplest solution is to just sum the acceleration vectors, as suggested earlier. And you can use the approximate form of the general acceleration equation (provided by sQuid), neglecting the mass of the rocket.
Of course, I could be underestimating how accurate the simulation has to be.

zephyr
zephyr
December 11, 2002 09:59 PM
quote:
Actually that is the force between the two masses.
Yes, and Force = mass * acceleration . He can solve for acceleration.
quote:
Original post by Anonymous Poster
Yes, and Force = mass * acceleration . He can solve for acceleration.
Just because force is proportional to acceleration doesn''t mean you can say that force IS acceleration, as Shadow Mint suggested. Solving for acceleration gives the equation provided by sQuid, which is the correct form for Newtonian acceleration.
Cheers,
Timkin
Because you work with comparably smooth forces (as you descreabed ) , gravity and roket engines , calculate your motion correctly, dont use Euler Integration.
lets simulate some Delta Time "DT". First accumulate all your forces into one force vector by adding to this .
Body->AddForce( vector3 F) {
this->Force += F;
}
The gravity force calculated easy GravityVector*BodyMass; (On earth gravity vector act down and its length = 9.8 )
The others forces you have to calculate by yourself. To example if you engine act to center of body :
vector3 EngineForce = EngineDirection*EnginePower;
Then :
DoSimulationStep( float DT){
vector3 Acceleration = Force*InverseMass;//( = Force/BodyMass
vector3 VelocityOld = this->Velocity ;//
this->Velocity += Acceleration*DT;// after DT
this->Position += 0.5*(VelocityOld + Velocity)*DT;
}
If your acceleration don''t change unsmooth , thats is a correct solution.
lets simulate some Delta Time "DT". First accumulate all your forces into one force vector by adding to this .
Body->AddForce( vector3 F) {
this->Force += F;
}
The gravity force calculated easy GravityVector*BodyMass; (On earth gravity vector act down and its length = 9.8 )
The others forces you have to calculate by yourself. To example if you engine act to center of body :
vector3 EngineForce = EngineDirection*EnginePower;
Then :
DoSimulationStep( float DT){
vector3 Acceleration = Force*InverseMass;//( = Force/BodyMass

vector3 VelocityOld = this->Velocity ;//
this->Velocity += Acceleration*DT;// after DT
this->Position += 0.5*(VelocityOld + Velocity)*DT;
}
If your acceleration don''t change unsmooth , thats is a correct solution.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement