Java Reference
In-Depth Information
public override function update():Void{
shaft.endX = bodies[1].getPosition().getX() - translateX;
shaft.endY = bodies[1].getPosition().getY() - translateY;
bottomNode.translateX = bodies[1].getPosition().getX() - translateX;
bottomNode.translateY = bodies[1].getPosition().getY() - translateY;
}
}
In the
init
method of the class
Pendulum
in Listing 6-7, two bodies are created. The first
Body
stored
in
bodies[0]
is a
StaticBody
that defines the top, fixed circle. The second
Body
, stored in
bodies[1]
is the
big ball at the bottom. Since the class
Pendulum
allows the caller to specify the distance between the top
and bottom bodies and the starting angle for the bottom
Body
, a little math is required to find the starting
position of the bottom
Body
, as shown in Figure 6-5.
Figure 6-5.
Starting positions
Here we introduce the concept of a
Joint
. A
Joint
in Phys2D is an object that defines a constraint
between two Bodies. In this case, we want the two bodies to stay a particular distance from each other to
simulate that the lower body is hung from the upper body by a string or wire. In this way, the lower
Body
will swing around the upper
Body
when it is pulled by gravity or impacted by another object.
There are three JavaFX
Nodes
that are used to draw each
Pendulum
, one for each
body
and a
Line
used
to draw the connection between the two. The position of these
Nodes
is updated in much the same way
as the other
WorldBodies
defined in this chapter—simply update the location for each step. For this class
we are ignoring rotation.