   Planets gravitional pull.
  I have a top down space-ship game, flying in a solar system. I want to have when the ship flies near a planet it effects its acceleration/velocity so it slowly pulls the ship into the planets orbit. And if you don't pull away you'll find yourself orbitting around the planet.I have no idea how to do this.Any help would be appreciated.   
  The equation for the acceleration due to gravity is (Gravitational Constant * Planet Mass) / (Distance from center of planet)^2. If you feed some appropriete values into that equation you sould get what you need with a bit of tweaking.   ;  Use Newton's Law of Gravity: F = m1 m2/r2. Set m1 to the mass of the ship = 1 and m2 = 1000 or so. Ignore the computation when r > d, where d is some significant distance away from the planet. On each tick, apply the vector F to the current vector of the ship. (F is in the direction of the planet.)If you want the ship to automatically orbit around the planet in a perfect circle once it gets close enough, you'll need to insert a cutoff point k. That is, once r <= k, the ship's vector is automatically set to orbit the planet at a distance of k.You can ignore the gravitational constant G that's in the real Newton's equation because it's a constant. You can make up your own G.   ;  You can do this with the general gravitational force equation for the attraction between two bodies, Fg = (G * M1 * M2) / (R^2), where M1 and M2 are the two masses, R is the radius, and G is the gravity constant. Just make a unit vector from the spaceship to the planet and multiply it by this force above. If you care less about realism you can probably make a much more simple force pulling the spaceship in the direction of the planet, but I guess you will get less characteristic results than of real simulated gravity. Then again, in a game this can be desireable (myself I am guilty of increasing the gravity constant in order to speed up acceleration).EDIT: Diving away 15 minutes before finishing message and then return before hitting the Reply-button is not that great; now you already got perfectly valid replies above. :)   ;  I made this in my gravity & collision demo. 5 planetary objects attracting eachother.You will need to calculate the force of the planets pull on the ship which is as the previous poster pointed out (M1*M2)/R^2.When you have the force you can calculate the change in velocity of the ship, which is A = F/M Where A is the acceleration in the direction of the planet, F the force that of planets pull, and M the mass of the ship.I use a direction vector for my objects velocity. Thus I'd do newVelocity = oldVelocity+AIf you use one scalar for velocity and an euler angle to indicate direction the calculation will be a tad more difficult because the force will change your angle AND your actual velocity.   ;Quote:Original post by StructuralWhen you have the force you can calculate the change in velocity of the ship, which is A = F/M Where A is the acceleration in the direction of the planet, F the force that of planets pull, and M the mass of the ship.I forgot about this, of course one has to calculate the acceleration from F/M... It's hard to remember when I accidentally think it is obvious.Also, you should probably multiply the change in velocity and position by the time between each frame here referred to as t, unless you have some fixed time-step function, like this:a = F / mv = v + atp = p + vt   ;  No-one's pointed out how to do it with 2D vectors yet.If your sun has mass M1 and is at (x1,y1), your ship has mass M2 and is at (x2,y2), then dx = x2-x1, dy = y2-y1.Start with Newton:F = G*M1*M2 /(r^2), where r = sqrt(dx*dx + dy*dy)F = ma, so the linear acceleration is:a = G*M1/(r^2)Split it into two dimensions:ax = G*M1*dx/(r^3)ay = G*M1*dy/(r^3)If you know the angle T from the ship to the sun, then you can replace the nasty sqrt with sin/cos.ax = G*M1*cos(T)/(dx*dx+dy*dy)ax = G*M1*sin(T)/(dx*dx+dy*dy)Shouldn't this be in the Physics section?   ;  By the time you're dealing with multiple planets and such, isn't relativity getting into this and making everything nice and messy?   ;  No; relativistic effects are only relevant at extremely high speeds and don't have anything to do with the number of things in your system.In general the n-body problem isn't particularly nice (possible? don't remember) to solve exactly, but games do numeric integration (which is approximate to various degrees, depending on the timestep size) anyway.   ;Quote:Original post by ZahlmanIn general the n-body problem isn't particularly nice (possible? don't remember) to solve exactly, but games do numeric integration (which is approximate to various degrees, depending on the timestep size) anyway.Can't be solved exactly, but very good (yet complicated) approximations are available. I would ignore it - the effects are negligible in this context and are of no value in terms of gameplay.   