Skip to content

Velocity & Acceleration

Before getting too fancy, a good place to start is moving something around. Rather than set the velocity directly, it’s better to affect the acceleration via accelerationForce and let Forces.apply integrate that into an updated velocity and position.

This demo computes an acceleration based on the movement of the pointer (via point tracker). Since we want to apply a dynamic acceleration force depending on the pointer movement, it’s not possible to initialise the force function and re-use it. Rather, we call Forces.accelerationForce each time we apply.

thing = Forces.apply(
thing,
Forces.accelerationForce(avg, `dampen`)
);

(don’t forget you can click the ‘Source’ button in the demo titlebar to see the full code of a demo)

Note how the acceleration force has the ‘dampen’ parameter. A few different forces use the thing’s mass field. With this parameter, we can choose for the mass to ‘dampen’ (ie. reduce), ‘multiply’ (ie. increase) or be ‘ignored’.

Along with the basic demo of acceleration above, there’s a more complicated example that applies the force to an arbitrary number of things: