Java Reference
In-Depth Information
bodies[0].setRestitution(1.0);
bodies[0].setCanRest(true);
bodies[0].setDamping(0.2);
insert arcs into content;
}
public override function update():Void{
translateX = bodies[0].getPosition().getX();
translateY = bodies[0].getPosition().getY();
arcs.rotate = Math.toDegrees(bodies[0].getRotation());
}
}
Listing 6-4.
Wall.fx
public class Wall extends Group, WorldNode{
public var width:Number;
public var height:Number;
init{
var rectangle = Rectangle{
width: width;
height: height;
translateX: width/-2.0;
translateY: height/-2.0;
fill: Color.RED
}
effect = lighting;
var shape = new net.phys2d.raw.shapes.Box(width,height);
bodies[0] = new StaticBody(shape);
bodies[0].setRotation(Math.toRadians(rotate));
bodies[0].setPosition(translateX, translateY);
bodies[0].setRestitution(0.5);
insert rectangle into content;
}
public override function update():Void{
//do nothing, walls don't move.
}
}
The class
Ball
shown in Listing 6-3 implements the classes
Group
and
WorldNode
. In this way,
instances of
Ball
will contain both the Phys2D representation and the JavaFX representation of a ball.
(
WorldNode
is shown in Listing 6-2.) As you can see in the
init
function of
Ball
, two
Arcs
are used to
represent a ball; we use two arcs of different color instead of a JavaFX
Circle
so rotation can be seen. A
Body
is also created with the shape of a circle, with the same radius as the two arcs. Phys2D has its own
representation of shapes outside of JavaFX, so some work must be done to coordinate the two different
APIs. The
update
method of
Ball
shows how the location of the
Ball
in the scene is updated based on the