# [Orbital] Gravity

## Recommended Posts

pseudobot    118
Hey all! :) I am wondering how to calculate gravity for a planet & have objects act accordingly. Basically, this is what I want to do: Have a few planets, for now lets just leave it at one. This planet has a mass of 20 (goes from 0-100). Then I have a asteroid. This asteroid should now be attracted by the planets gravity. However, this should be calculated in to a XVelocity and a YVelocity. ANd that part I am unsure of. Hope someone can help me out here, and if my post is not detailed/confusing than I will draw some pics to clarify. Thanks in advance! :)

##### Share on other sites
BEFORE edit

To calculate into a xvelocity and yvelocity, you would need to find the gravitational force that planet exerts, then find out the acceleration, and then apply the acceleration to the asteroid. The asteroid's velocity would then change.

AFTER edit

To calculate into a xvelocity and yvelocity, you would need to find the gravitational force that planet exerts, then find out the x and y components of the acceleration, then apply the acceleration components to the current velocity components.

##### Share on other sites
pseudobot    118
The problem here is, I do not know how to do these things. :-/

##### Share on other sites
http://en.wikipedia.org/wiki/Newtonian_Physics

##### Share on other sites
dmatter    4821
To find the force exerted on the asteroid use Newton's law of universal gravitation.
To find the acceleration of the asteroid towards the planet use Newton's second law of motion.
To find the new velocity (given the old velocity and the amount of time that has passed since the last frame) use the first SUVAT equation.

##### Share on other sites
h4tt3n    1974
Hi there Pseudobot,

Playing around with gravity simulation is really fun, and I've done so for a couple of years :-) To help you along, here's a very small, well commented code sample demonstrating how to simulate gravitationally interacting objects on a computer. It's written in a language called freebasic which you might not know, but it's easily readable, and once you understand the principles behind it, you should be able to port it to whatever language you use.

http://www.jernmager.dk/stuff/freebasic/gravity_02.zip

good luck with it!

Cheers,
Mike