bounds of the Group nodeToReplace . When all the Controls have fallen, the transition FadeReplace from
the transitions chapter is used to fade in the replacementNode . Lastly, the Timeline cleanup is started and
the Timeline checkCleanup is stopped.
The Timeline cleanup stops the Timeline addBodies and worldUpdater , then restores the location of
the each Control as if nothing ever happened.
The class ControlHelper is basically an implementation of the class WorldNode taken from earlier in
the chapter. It keeps track of a JavaFX node and two Physics Bodies. This implementation is different
from the previous implementation as it switches from representing a StaticBody to a regular Body .
Adding a physics engine to an application can save a lot of work for a developer creating animations that
borrow from the physical world. The sense of weight and motion helps draw the eye, while animations
that that fail to convince the user can be distracting. However, a physics engine is a general-purpose
tool, and as such, it can be complicated both in implementation and in the amount of work needed to
calculate each frame. Sometimes it is better to simply tweak more traditional animations until they look
right, rather than trying to make them mathematically correct with a physics engine.
Remember that with a physics engine, the application must check to see if each body has collided
with every other body in the world. This means that the cost of calculating collisions grows exponentially
as bodies are added to the scene. Of course smart engines reduce this cost as best they can, but
eventually there is no escaping the math as bodies are added. It is best to include only the objects in your
calculations that might actually interact. If a component on the screen will never effect the animation,
don't include it your physics calculations.
It is rare when a physics engine gets used outside of a game. It might be interesting, however, to
produce some UI controls with physics elements, such as a progress bar that fills with balls and tips over
when it is full.
This chapter looked at what a physics engine is and how you can use the open source engine Phys2D to
add animations to an application. The examples showed how to use JavaFX to display the bodies within
a simulation. Another example showed how to create a UI transition that took advantage of the physics
engine to create a playful animation.