Game Development Reference
In-Depth Information
6. Then, we set the limit for the rotation of the door and add it to physicsSpace
as follows:
joint.setLimit(-FastMath.HALF_PI - 0.1f,
FastMath.HALF_PI + 0.1f);
bulletAppState.getPhysicsSpace().add(joint);
Now, we have a door that can be opened by walking into it. It is primitive but effective.
Normally, you want doors in games to close after a while. However, here, once it is
opened, it remains opened. In order to implement an automatic closing mechanism, per-
form the following steps:
1. Create a new class called DoorCloseControl extending AbstractCon-
trol .
2. Add a HingeJoint field called joint along with a setter for it and a float
variable called timeOpen .
3. In the controlUpdate method, we get hingeAngle from HingeJoint
and store it in a float variable called angle , as follows:
float angle = joint.getHingeAngle();
4. If the angle deviates a bit more from zero, we should increase timeOpen using
tpf . Otherwise, timeOpen should be reset to 0 , as shown in the following code
snippet:
if(angle > 0.1f || angle < -0.1f) timeOpen += tpf;
else timeOpen = 0f;
5. If timeOpen is more than 5 , we begin by checking whether the door is still
open. If it is, we define a speed to be the inverse of the angle and enable the
door's motor to make it move in the opposite direction of its angle, as follows:
if(timeOpen > 5) {
float speed = angle > 0 ? -0.9f : 0.9f;
joint.enableMotor(true, speed, 0.1f);
spatial.getControl(RigidBodyControl.class).activate();
}
6. If timeOpen is less than 5 , we should set the speed of the motor to 0 :
Search WWH ::




Custom Search