Java Reference
In-Depth Information
addWorldNode(Pendulum{translateX: x+seperation*4, translateY: y});
}
Listing 6-6 shows that adding the new nodes is pretty easy—you simply create each one and add it
with the function addWorldNode . It is the implementations of the new class Pendulum that shows how the
Bodies are created to behave like pendulums.
Listing 6-7. Pendulum.fx
public class Pendulum extends Group, WorldNode{
public-init var distance = 150.0;
public-init var angle = 0.0;
var topNode = Circle{
radius: 4
fill: Color.AQUA;
}
var bottomNode = Circle{
radius: 16
fill: Color.SILVER;
effect: lighting;
}
var shaft = Line{
fill: Color.CORAL;
stroke: Color.CORAL;
}
init{
bodies[0] = new StaticBody( new net.phys2d.raw.shapes.Circle(topNode.radius));
bodies[0].setPosition(translateX, translateY);
bodies[1] = new Body(new net.phys2d.raw.shapes.Circle(bottomNode.radius), 10);
bodies[1].setRestitution(.99);
var a = Math.sqrt((distance*distance)/2.0);
var x = Math.sin(Math.toRadians(angle))*distance;
var y = Math.cos(Math.toRadians(angle))*distance;
bodies[1].setPosition(translateX+x, translateY+y);
joints[0] = new DistanceJoint(bodies[0], bodies[1], new Vector2f(0,0), new
Vector2f(0,0), distance);
shaft.startX = bodies[0].getPosition().getX() - translateX;
shaft.startY = bodies[0].getPosition().getY() - translateY;
insert shaft into content;
insert topNode into content;
insert bottomNode into content;
update();
}
Search WWH ::




Custom Search