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)?
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It only takes a minute to sign up.Sign up to join this community
Just subtract the particle position from the cursor position to make the acceleration vector. Then multiply the norm with the gravity constant.
//pseudocode GRAVITY_NORM = 9.8; 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.