Game Development Reference
In-Depth Information
jointDef.motorSpeed = 40.0f;
jointDef.enableMotor = false;
joint = (b2PrismaticJoint*)physicsBody- > GetWorld()- > CreateJoint(&jointDef);
First, a static body is created at the same location as the plunger's dynamic body. It acts
as the larger pipe of a telescope bar held in place so that only the inner pipe may spring
upward when released. Remember physics (or Mythbusters , for that matter): every ac-
tion has an equal and opposite reaction. To avoid the opposite reaction—a downwards-
oriented movement of the other body of the prismatic joint—it's turned into a static,
unmovable body holding the plunger in place.
The worldAxis restricts the prismatic joint's movement to the y-axis (in other
words, up and down). The worldAxis is expressed as a normal vector with values
from 0.0f to 1.0f; when the y-axis is set to 1.0f, the worldAxis becomes parallel to
the y-axis. If you were to set both x- and y-axes to 0.5f, the worldAxis would be a
45-degree angle. b2PrismaticJointDef is initialized with the staticBody and
the world center position of the plunger's dynamic body. As the anchor point for the
joint the worldAxis is used, which restricts motion of the prismatic joint along the y-
Now follows a set of parameters. The lower and upper translations define how far
along the axis the plunger is allowed to move. In this case, it's allowed to move 0.35f
meters upward, which is exactly 42 points or 84 pixels on Retina display devices and
42 pixels on non-Retina devices. The enableLimit field is set to true so that this
movement limit is actually adhered to by the connected bodies. Because the static body
doesn't move, the plunger moves the full extent. If both were dynamic bodies, both
bodies would be able to move, which would be undesirable in this case, as mentioned
Next, set maxMotorForce to 80.0f (the unit in this case is Newton-meters, which
is a unit of torque). In Chipmunk this is called the body's moment . The maxMotor-
Force value limits the torque, or energy, of the joint's movement. The motorSpeed
then determines how quickly, if at all, this maxMotorForce is reached. I determined
both values merely by trial and error until it felt about right. The ball is now catapulted
up and around with just about the right speed. The motor is initially disabled because I
only want the plunger to go off when there's a ball touching it.
Search Nedrilad ::

Custom Search