7
\$\begingroup\$

I'm making a 2d shooter ala Geometry Wars. I've got my own simple physics at work driving the background grid and all my entities. To move anything in the world I apply a Vector2d force to it. The 'engine' calculates the resulting acceleration and therefore the velocity.

I am trying to port some code I found which implements the classic 'Boids' flocking algorithm, but the code I have works by calculating the Boids' velocities directly, so If i use it as is, it bypasses my physics engine. How I can translate the velocity vectors into force vectors that I can apply to the Boids and which will result in the proper velocities via my physics engine.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ One question to your question: are your bodies capable of theoretically infinite accelerations? If yes, then if the algorithm tells an entity to change its velocity from v0 to v1, you can compute the acceleration as a = (v1 - v0 )/dTime .Then you can apply the F = m * a force to that entity and it will have this velocity at the next time step. But, there's another problem: what integration method do you really use? Depending on your update logic and integrator, this simple approach might not work. I assumed a simplistic explicit integrator logic (e.g. Euler). \$\endgroup\$
    – teodron
    Dec 7, 2012 at 16:49
  • \$\begingroup\$ If you have velocity, mass and time: Force = (mass * velocity) / time. \$\endgroup\$
    – Asakeron
    Dec 7, 2012 at 16:53

1 Answer 1

6
\$\begingroup\$

It depends how your physics engine works, but you can probably use Newton's second law:

F = m × a

Where m is the mass of the object and a is its acceleration (a vector). The acceleration is the dv/dt derivative, which can be approximated by dividing the change in velocity by the timestep:

F = m × (velocity - previous_velocity) / timestep

Of course this only works reliably if the force you're applying is the only one affecting the entity. Otherwise you may experience numerical inaccuracies.

\$\endgroup\$
1
  • 6
    \$\begingroup\$ Be careful about using × for 'times' - a lot of people will (understandably) interpret that as 'cross product'... \$\endgroup\$ Dec 7, 2012 at 19:41

You must log in to answer this question.

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