0
\$\begingroup\$

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)?

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

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.
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .