Game Development Reference
In-Depth Information
// mirror speed and angle for the right flipper
jointDef.motorSpeed * = -1;
jointDef.lowerAngle = -jointDef.upperAngle;
jointDef.upperAngle = 0.0f;
}
// create the joint
joint = (b2RevoluteJoint*)physicsBody- > GetWorld()- > CreateJoint(&jointDef);
}
You may be wondering why the flipper's body is set as a bullet. Trust me, I'm not go-
ing to shoot flippers at you! Physics engines traditionally have a problem with detect-
ing collisions of objects moving at high speeds because such an object can travel great
distances between two collision tests, seemingly “tunneling” through other collidable
objects. That may be fine for subatomic particles, but not for our flippers and the ball.
The SetBullet method enables a special, continuous collision detection method for
fast-moving objects that takes into account the path the object must have taken between
two collision tests. Thus, the bullet mode is able to detect collisions that would have
otherwise been missed, at the expense of performance. The bullet mode should be used
judiciously and only when absolutely needed. In the pinball game, I noticed that both
the flippers and the ball would sometimes “miss” each other, so I have them both
treated as fast-moving objects to get more accurate collision detection.
The static body is created to attach the flipper to an unmovable body, to keep the flip-
per anchored in place. b2RevoluteJointDef uses lowerAngle and upper-
Angle as the rotation limits, which are in radians. I'll set the upperAngle to 70 de-
grees and convert it to radians with the CC_DEGREES_TO_RADIANS macro provided
by cocos2d.
The revolute joint also has maxMotorTorque and motorSpeed fields, which are
used to define the speed and immediacy of the movement of the flippers. However, un-
like the plunger, the motor is enabled all the time, and changes direction every time the
sign of the motorSpeed variable is changed. While the flippers are down, the motor
will force them down so that they don't bounce when the ball hits them.
In the ccTouchBegan method, the location of the touch is obtained, which is valid-
ated with the isTouchForMe method before actually reversing the motor.
Search Nedrilad ::




Custom Search