Archived

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

stickman

gravity

Recommended Posts

i need help, im kinda stupid and i need help with gravity for my ship. what i have is a ship that can fly around... and there is a planet. What i want is the ship to get sucked into the planet if they dont orbit it properly. i got a # for the force between the ship and the planet that i planned on using but i dont really know how to make them work together. what i did was use the distance formula and used a constant divided by my distance sqaured. so it gets stronger the closer i go. so right now my ship has an xspeed and a yspeed, and i have a force between the ship and the planet. whats giving me the biggest problem is using agles on the force... im getting stuck and i dont know how to add the ships speed with the force to make it go closer to the planet. pwease halp me!

Share this post


Link to post
Share on other sites
Gravity is an acceleration toward the center of a body. So, get a vector from the ship to the core of the planet, use its magnitude as your distance. Apply your "forward" velocity as you normally would, and orbiting happens naturally. Where''s the breakdown?

Share this post


Link to post
Share on other sites
You need to do something called numerical integration. There are lots of university courses and such on the subject, but this specific case doesn't really require such general knowledge. Like ZealousElixir said, you need to get a vector from your ship to the centre of the planet. Then get the distance by determining the magnitude of the vector (if you haven't already).

Now, the interesting part. The formula for the force of gravity is like so:

F = G*M*m/r^2

Where F is the force, G is the gravitational constant (pick a number that gives you the results you want), M is the mass of the planet, m is the mass of the ship and r is the separation of the centres of mass of the planet and ship.

You may also know the formulaic representation of Newton's 2nd Law:

F = m*a

where a is the acceleration of a free body. F and m are the same as above.

Combining these two formulas gives

m*a = G*M*m/r^2

You can emliminate m from both sides, giving

a = G*M/r^2

This is a differential equation. The acceleration a is second derivative of the position of the ship. The acceleration points towards the object that's causing it, namely the planet. What you need to do is integrate this differential equation numerically to determine where your ship will move. Note: If this was the extent of the situation, you could possibly analytically solve this, but since you probably want the ship to be controllable (rockets firing or somesuch) you need to solve numerically.

The simplest way to do this is using the Backwards Euler integration scheme. Each "frame" of animation (location of the ship) is determined like so:

You start with the "initial conditions" for the integration step:

The ship's x and y (and z?) positions, and the ship's x and y velocities. You get these numbers by picking them to fit what you want to simulate, or based on the results of a previous integration step.

You then calculate the vector from the ship to the planet, and then convert this vector into a unit vector. Now calculate the acceleration (as above) giving you a total acceleration due to gravity. You may want to add some sort of thrusters acceleration to this in addition. Also, you could add various forms of drag, but this can be added later. Multiply this total acceleration by each of the components of the unit vector pointing from the ship to the planet. This will give you the acceleration of the ship in each dimention of its motion.

You then do the following.

newVelX = oldVelX + accX * stepSize;
newVelY = oldVelY + accY * stepSize;
newPosX = oldPosX + newVelX * stepSize;
newPosY = oldPosY + newVelY * stepSize;

where stepSize is some (fairly small) number that you pick to control the speed (and accuracy) of the simulation. Faster simulations (bigger stepSize) generally means much less accurate and less stable integration.

You now have a new set of positions and velocities for the ship, and can repeat the whole process over and over to determine where the ship goes over time.

Edit: Wrong Newton's Law #

[edited by - Geoff the Medio on July 27, 2003 10:36:20 PM]

Share this post


Link to post
Share on other sites