Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have particles, with a velocity (x and y) per tick. I would like to to make my cursor a gravity force, so particles fly to the cursor's position.

How can I apply gravity (that is towards my cursor position) on my velocity (x and y)?

share|improve this question
up vote 2 down vote accepted

Just subtract the particle position from the cursor position to make the acceleration vector. Then multiply the norm with the gravity constant.

accVector = normalize(cursor.pos - particle.pos);
//Apply acceleration: S = S0 + v0*dt + 0.5*g*dt*dt;
accNorm = particle.vel*dt + 0.5*GRAVITY_NORM*dt*dt; 
particle.pos = particle.pos + accVector*accNorm;

This computation actually doesn't calculates the gravity force, but the gravity acceleration (in Earth). If you want to get the real gravity force for a more general case you should attribute your cursor and particle with mass. However this system implies that you cursor should also be affected by the force and I'm assuming that it isn't your intention. Anyway, the equation for the force is

f = G * ( m1 * m2 ) * 1/r*r, where G is the gravity constant,
m1 and m2 are the masses and r is the distance between the bodies.
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.