I am writing a little game engine in LWJGL. I want to have a gravity, but I don't know correct equations. Let's assume that I've got a 3D vector marking gravity force in each axis(e.g. Vector3f(0.0, -9.8, 0.0) would be Earth's gravity), object's mass and object's CdA(coefficient of drag multiplied by approximated object area). I wanna know how to correctly calculate object's gravity!
If you want to write a fully functional physics engine I'd recommend learning some basic newtonian physics. Wikipedia is a good starting place for those kind of things. That being said I'll try to provide some information about your "problem" and beyond that.
At every point in space you have a local gravity, usually named g. g is an acceleration with m/s² as unit (you could use other units like km/h² or something like this, but they're usually useless). In 3 dimensional space acceleration is a vector with 3 components (x, y, z).
Accelerations simply represent the increase of velocity per time frame, usually per second. So if you want to calculate your velocity at time t you simply have to do v = a * t + v0, of course assuming a is constant over your time t. v0 is the velocity of your object at t = 0 (If you have a car going 130 km/h and accelerate it by 5 (km/h) / s over the time of 6 seconds you have a final speed of 160 km/h).
Using it in your game
Long story short: If you want to have your velocity you simply have to add a * dt to your former velocity vector, where dt is the time that passed since the last frame (At fixed 60 fps 0,0166666... s). Keep in mind that your units have to match. If g is in m/s² your dt also has to be in s.
velocity += g * dt; //Assuming your math library allows * operator for vectors
Further physical explanation for multiple gravitational pulls: Of course in reality the local gravity is not equal everywhere. The equation for gravity force is:
|F| = G * m1 * m2 / r² where G = 6.67e-11, m1/m2 = the masses of your objects, and r = distance between the center of mass of both objects
Important fact about this force: It affects both objects, but usually you only care about one of them, because the effect on the other one is neglegible. The direction of the force is always towards the mass center of the other object.
If you have multiple masses with relevant gravitational pull you have to add you force vectors F together and finally use the following equation to calculate your final acceleration:
a = F/m
Now, you could want to include air resistance. This makes the matter much more complex as you have multiple factors to consider: Density and pressure of your medium (Maybe it changes depending on your current height). Or your area covered by the falling object, which again decreases acceleration (in this case mass of your falling object also matters again, without air resistance it's not relevant). Covering all possible cases is way to much to do in a simple answer here so I'd recommend starting further research here.
Disclaimer: Of course this explanation is based solely on newtonian physics. Einstein's relativity is not taken into account, but it only matters significantly if v is in scale of 0,1c or higher
The figure of 9.8 metres/second/second is only accurate at a distance of approximately 6371 km from the Earth's centre, and only for a planetary mass of approximately 5.972 * 10^24 kg.
However, if you use those calibration figures above, thus measuring distance in Earth Radii (Re) and mass in Earth Masses (Me), then a general gravitation equation for the magnitude of the gravitational acceleration (Ga) can be obtained as
Ga = - (9.8 * Me / Re / Re)
Note that in a 3D world the direction of the gravitational acceleration (or force) is always exactly opposite to the displacement of the object from the centre of the planet.
As long as you only have one very massive object in your system it will be sufficient to omit the gravitational force between the moving objects of your system, and to keep this single massive object stationary. However as soon as you have a second very massive object, be it moon, planet, or star, this approximation will not be accurate.
I believe you are wanting to simulate a simplified, uniform gravitational field where the following assumptions are true:
- The mass of the object in question is negligible compared to the mass of the object it is being gravitationally attracted to.
- Any change in distance between the object in question and the other object is negligible compared to the total distance between them.
If this is the case, then these simplifications allow us to simulate gravitation very easily by just using an acceleration equation. At each time step, increment the object's velocity vector by the acceleration * duration of time step dt:
velocity += acceleration * dt
Where, in the Earth surface gravity case,
acceleration would be the Vector (0, -9.81, 0) where positive y represents the upwards direction.