Advanced
Applying forces to many things
Use the regular Javascript constructs to apply forces to many objects.
Dynamic
At times the pattern of initialising a force once and then reusing it repeatedly isn’t appropriate. For example, if you want friction to be affected by a ‘smoothness’ property you have defined, this won’t work:
Instead, create the force function when we call apply
:
Optional
We can also opt to dynamically apply a force or not, based on some state using a ternary operator.
In this example, we assume gravityApplies
is a property of the thing. If the value is true, we apply a gravity force, if not a ‘null’ force which does nothing:
This technique is used in the basic forces demo to selectively apply forces depending on whether the thing is in ‘air’ or ‘water’