Game Development Reference
In-Depth Information
How to do it...
To simulate balancing, we will begin by creating the upper body of a stickman-shape figure
with a torso and two rigid arms. To do this, perform the following set of steps:
1. First of all, we should set up an application with
BulletAppState
.
2. In the
simpleInitApp
method, we create a small square
Box Geometry
to
be the waist of the character. It can be
0.25f
in all the axes.
3. We add
RigidBodyControl
to it with
0
in mass since it shouldn't move.
4. Then, we create an oblong box to be the torso and place it above the waist. It
should have
RigidBodyControl
with
1
in mass and
BoxCollisionShape
should be of the same size as the geometry:
torso = new Geometry("Torso", new Box(0.25f, 2f,
0.25f);
RigidBodyControl torsoRigidBody = new
RigidBodyControl(new BoxCollisionShape(...), 1f);
...
torsoRigidBody.setPhysicsLocation(new Vector3f(0,
4.25f, 0));
5. Next, we create
SixDofJoint
between the waist and torso and afterwards add it
to
physicsSpace
as follows:
SixDofJoint waistJoint = new
SixDofJoint(waistRigidBody, torsoRigidBody, new
Vector3f(0, 0.25f, 0), new Vector3f(0, -2.25f, 0f),
true);
6. We should limit the joint so that it can't rotate on any axes other than the
x
axis,
and it shouldn't be able to rotate too much. We can use the following
setAngu-
larLowerLimit
and
setAngularUpperLimit
methods for this:
waistJoint.setAngularLowerLimit(new
Vector3f(-FastMath.QUARTER_PI * 0.3f, 0, 0));
waistJoint.setAngularUpperLimit(new
Vector3f(FastMath.QUARTER_PI * 0.3f, 0, 0));
7. Next, we create one of the arms.