Java Reference
In-Depth Information
Bodies have:
Angular Velocity
Once the bodies and the world are defined in a suitable data structure, the rules of motion are
applied to each body. The location and orientation of each is advanced for a small amount of time,
usually called a step . A step is often the target frame rate of the application, 1/30th of a second. So bodies
with a velocity are moved, bodies with an angular velocity are rotated, and all objects are moved in the
direction of gravity, but only as much as they would for the amount of time in a single step.
When the new location of the bodies is determined, each body is checked to see if it now overlaps
any other body. If bodies overlap, that is to say, if a collision is detected, then the velocity and angular
velocity are adjusted based on how the bodies collided. After each body has its location and rotation
updated, the application applies any other changes to the world that makes sense for that app—perhaps
bodies that collide with each other are removed or change color. Lastly, the scene that is displayed to the
user is updated to reflect these changes. Once the new scene is drawn, the world is advanced by another
Third-Party Implementation
As noted earlier, implementing your own physics engine is a lot of work. Not to dissuade an inspired
developer, but the intricacy of handling complex shapes, efficiently checking for collisions, and generally
getting the math right are all good reasons to use someone else's hard work. The following examples use
a physics engine called Phys2D, an open source physics library implemented in Java. Phys2D is the work
of Kevin Glass, an active member of the Java Gaming Community who ported the Box2D library from C
to Java. Phys2D can be downloaded from . It is available under the
terms of the New BSD License, which is pretty liberal—but worth reading up on to make sure you
conform to the terms.
Phys2D works much as described above—a World object is created and a number of Bod y objects are
added. Then the function step is called on the World object and the location and rotation of each body is
updated. Phys2D is a Java library, not a JavaFX library, but this is of little concern as Phys2D is easily
called from JavaFX. In general there will be one JavaFX node for each Body in the world, and the location
and rotation of the Node will be updated based on the location and rotation of its Body object after each
step. The application will do something like the following:
Add Bodies to the World and corresponding Nodes to the Scene .
Call World.step() .
Apply application logic.
Search WWH ::

Custom Search