Implementing Full Gravity

Posts: 64
Joined: 2005.06
Post: #1
Does anyone have any examples, tutorials, or even a full write-up of how to implement gravity, and everything that can affect it (essentially recreating real-world gravity)?

So far, I've found the equation that deals with the force of gravity
F = (G�m1�m2) / r^2
, but I've been unsuccessful in implementing and calculating it correctly in Cocoa.
Quote this message in a reply
Posts: 92
Joined: 2002.04
Post: #2
If you are talking in a 2D world just apply a 9.8metres per sec accelerating vector down.
However when you say it doesn't work what sort of results are you getting? you'd have to remember that the earth is massive compaired to objects on it which are close so it has some force. If you are giving 3d objects little mass and putting a fair about of space between them and expecting gravity to do much that might be a problem.
Quote this message in a reply
Post: #3
Proper gravity only really has a place in space games. If that's what you are doing:

The gravitational force one object exerts upon another is indeed given by the formula you quote.

To get the total gravitational force vector on a given object (theObject), you need to sum the forces of each object that exerts a pull. To get the force vector exerted by someObject:

* calculate a unit vector pointing from theObject to someObject.
* calculate the gravitational force using your formula.
* multiply the two together.

As you go through the other objects in the scene, add their contributions to a running total, and then apply the total force to the object in question.

Please note that the above is crying out for optimisation. Two main things:

1: it's probably faster to calculate the gravitational field at the point, since one doesn't have to worry about the affected object's mass at any point, and

2: you certainly don't want to bother summing the contributions of individual spaceships, unless they're very very massy spacecraft very close to one another indeed. I'd suggest some cutoff point, at a large number of kilograms (say 10,000,000Ö at least), where anything massing less is not considered in the calculations.

hope this helps - otherwise try an A-level physics text.
Quote this message in a reply
Post Reply