Game Development Reference
In-Depth Information
How it works...
The attachment RigidBodyControl has no mass and will thus not be affected by ex-
ternal forces such as gravity. This means it will stick to its place in the world. The door,
however, has mass and would fall to the ground if the attachment didn't keep it up with it.
The HingeJoint class connects the two and defines how they should move in relation to
each other. Using Vector3f.UNIT_Y means the rotation will be around the y axis. We
set the limit of the joint to be a little more than half PI in each direction. This means it will
open almost 100 degrees to either side, allowing the player to step through.
When we try this out, there may be some flickering as the camera passes through the door.
To get around this, there are some tweaks that can be applied. We can change the collision
shape of the player. Making the collision shape bigger will result in the player hitting the
wall before the camera gets close enough to clip through. This has to be done considering
other constraints in the physics world.
You can consider changing the near clip distance of the camera. Decreasing it will allow
things to get closer to the camera before they are clipped through. This might have implica-
tions on the camera's projection.
One thing that will not work is making the door thicker, since the triangles on the side
closest to the player are the ones that are clipped through. Making the door thicker will
move them even closer to the player.
In DoorCloseControl , we consider the door to be open if hingeAngle deviates a bit
more from 0. We don't use 0 because we can't control the exact rotation of the joint. Instead
we use a rotational force to move it. This is what we do with joint.enableMotor .
Once the door is open for more than five seconds, we tell it to move in the opposite direc-
tion. When it's close to 0, we set the desired movement speed to 0. Simply turning off the
motor, in this case, will cause the door to keep moving until it is stopped by an external
force.
Once we enable the motor, we also need to call activate() on RigidBodyControl
or it will not move.
Search WWH ::




Custom Search