Archived

This topic is now archived and is closed to further replies.

craphead

gravity

Recommended Posts

what do you mean... tutorials about how to implement gravity in a game?

if so well you can check out this thread i started, only check the first post with the code at the beginning... the rest is about how to compute the rotation of an object from the torque which you dont need for simple gravity...

Note: Of course putting the x,y,z vx,vy,vz etc. in vector struct would be better... that was just a test i was making for my engine...

[edited by - sross on July 3, 2003 12:50:18 PM]

Share this post


Link to post
Share on other sites
Gravity in what sense? Do you want a constant gravity with a constant direction? Or do you want gravity like a planet?

In terms of game programming, if you want the constant gravity with a constant direction, you can simply do something like add a value like -9.8 (or whatever value feels right) to your Y velocity variable (assuming you want downward gravity).

As for planetary gravity.. or just plain accurate gravity. First, get a unit vector from the spaceship(or whatever) to the planet (This can be done by subtracting the planetx - shipx and planety - shipy. You then take the newx and newy and divide them by the length of the vector.. and you have instant unit vector). To get the force, stick whatever numbers you want into this equation..

Force of gravity = ((Gravitational Constant)(Mass A)(Mass B)) / r^2

So you take G, Ma, Mb, and divide the product of them by r squared. The mass is in Kilograms and the Force of gravity is in newtons, but it won''t really matter. G is 6.67x10 to the -11 power (pretty sure.. been awhile since physics class). Unit for that isn''t important because it all works out anyway. Again, you can put in whatever value you want for G. Now, just multiply the force scalar with the directional unit vector we made earlier. Add this vector to the positional values of the object to simulate gravity.

How do we simulate two objects attracting each other? I have no idea.. i haven''t tried that yet. I''m sure it''s a simple though. (try replacing each of the masses in the equation with 1. For example, if you want the pull of object B, chance the mass A in the formula to one. And if you want the pull of A, chance Mass B to 1. This sounds about right, and you can translate the two masses based on these two values)

Hope this answers your question.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
If you want accurate integration for a space simulation, look up fourth order Runge Kutta. You''ll get a positive radial error each orbit if you use Euler integration.

Share this post


Link to post
Share on other sites