Game Development Reference
In-Depth Information
How to do it...
This recipe consists of two sections. The first will deal with the actual creation of the door
and the functionality to open it. This will be made in the following six steps:
1. Create a new
RigidBodyControl
object called
attachment
with a small
BoxCollisionShape
. The
CollisionShape
should normally be placed in-
side the wall where the player can't run into it. It should have a mass of 0, to pre-
vent it from being affected by gravity.
2. We move it some distance away and add it to the
physicsSpace
instance, as
shown in the following code snippet:
attachment.setPhysicsLocation(new Vector3f(-5f, 1.52f,
0f));
bulletAppState.getPhysicsSpace().add(attachment);
3. Now, create a
Geometry
class called
doorGeometry
with a
Box
shape with
dimensions that are suitable for a door, as follows:
Geometry doorGeometry = new Geometry("Door", new
Box(0.6f, 1.5f, 0.1f));
4. Similarly, create a
RigidBodyControl
instance with the same dimensions, that
is,
1
in
mass
; add it as a control to the
doorGeometry
class first and then add it
to
physicsSpace
of
bulletAppState
. The following code snippet shows
you how to do this:
RigidBodyControl doorPhysicsBody = new
RigidBodyControl(new BoxCollisionShape(new
Vector3f(.6f, 1.5f, .1f)), 1);
bulletAppState.getPhysicsSpace().add(doorPhysicsBody);
doorGeometry.addControl(doorPhysicsBody);
5. Now, we're going to connect the two with
HingeJoint
. Create a new
HingeJoint
instance called
joint
, as follows:
new HingeJoint(attachment, doorPhysicsBody, new
Vector3f(0f, 0f, 0f), new Vector3f(-1f, 0f, 0f),
Vector3f.UNIT_Y, Vector3f.UNIT_Y);