need math help

BlitzPlus Forums/BlitzPlus Programming/need math help

Schragnasher(Posted 2005) [#1]
Ok, im sure this is easy but i didnt pay attention in math too much :)


ok currently i have a ship flying around, inertia and all works fine. What im trying to add is a planet in the center that has a gravatational effect ont he ships. Can anyone help me out with the equations i need to use to find out how much in each direction i need to change the ships x/y?

Hopefully that makes sense :) ill draw a picture if noone understands.


Grey Alien(Posted 2005) [#2]
OK first you have to work out the distance of the centre of the spaceship from the centre of the planet. Use pythagorus for this e.g. radius = squareroot(x*x + y*y).

Then give the planet a Mass (M), make up a gravitational constant G, then the gravitational acceleration on the ship is (G*M)/(radius*radius). This assumes that the ship has no (or negligable) mass.

Now you have the gravitational acceleration it must be applied to the x and y intertia of your ship. But NOT just added on, it must be added on proportially to x and y. I think you can use Pythagorus for this too. You need to work out the ratio of x and y to the radius and multiply the gravitational acceleration by the ratio (which will be less than or equal to 1) . You might even be able to use sin and cos somehow (check this forum for a recent post on intertia EDIT: http://www.blitzbasic.com/Community/posts.php?topic=45268 but you need to know the angle so not much good!), but I have to go shopping now (cop out).

Good luck


Who was John Galt?(Posted 2005) [#3]
I assume you're doing something like this for inertia:
x#=x#+vx#
y#=y#+vy#
x,y is ship positions, vx,vy is ship speed in x and y directions.

Any force on the ship doesn't directly change it position, it changes its speed. For gravity, the force falls off as 1/(r^2) , where r is the distance between your ship and the planet centre.

r_squared=(x-planet\x)^2 + (y-planet\y)^2
force#=constant/r_squared

The force is pointing in a direction from the ship towards the centre of the planet. The force in x and y directions is:
fx#=force*(planet\x-x)/r
fy#=force*(planet\y-y)/r

(r=sqrt(r_squared))

I THINK that lot's right - you'll have to try it - may have some wrong. You'll get problems if r goes to zero - so keep your ship away from the centre of the planet or put a case in that skips the force if r is zero or v small. You could try a force of 1/r oor 1/sqrt(r) - not technically correct but whatever gives the game the best feel. You will have to play with the size of 'constant' aswell.


Schragnasher(Posted 2005) [#4]
ok i think i can go from there, well see :) thanks