Java Reference
In-Depth Information
Figure 6-1 shows a ball that is above two walls. When the application is run, the ball will fall and
bounce off the top wall first, then the bottom wall. To understand how this animation works, consider
the variable
worldUpdater
in Main.fx. This variable is of type
Timeline
and is used to drive the animation;
it does this by calling the function
update
30 times a second. The function
update
does two things—first it
asks the
world
object to advance the location of all of the
bodies
by one step, then it asks each
WorldNode
to update itself based on any changes to their associated
bodies
.
Listing 6-2 illustrates how
bodies
in the
world
drive
nodes
in the
scene
.
Listing 6-2.
WorldNode.fx
public mixin class WorldNode {
public var bodies:Body[];
public var joints:Joint[];
public abstract function update():Void;
}
Listing 6-3.
Ball.fx
var lighting = Lighting {
light: DistantLight { azimuth: -135 elevation: 85 }
surfaceScale: 5
}
public class Ball extends Group, WorldNode{
public var radius = 10.0;
var arcs = Group{};
init{
var arc1 = Arc{
radiusX: radius
radiusY: radius
startAngle: 0;
length: 180
fill: Color.WHITE
}
var arc2 = Arc{
radiusX: radius
radiusY: radius
startAngle: 180;
length: 180
fill: Color.BLUE
}
insert arc2 into arcs.content;
insert arc1 into arcs.content;
effect = lighting;
bodies[0] = new Body(new net.phys2d.raw.shapes.Circle(radius), radius);
bodies[0].setPosition(translateX, translateY);