Game Development Reference
In-Depth Information
How to do it...
We're going to start by creating two methods that will help us add new bridge segments to
the game:
1. We define a method called
createSegment
that takes a
Vector3f
parameter
called
location
as the input.
2. The first thing we do is set the
z
value of
location
to
0
. This is because we're
making a 2D game.
3. Then, we create a new
RigidBodyControl
instance called
newSegment
. We
add
SphereCollisionShape
to it and then add
newSegment
to
physic-
sSpace
. It's important that it has some mass. This can be implemented as follows:
RigidBodyControl newSegment = new RigidBodyControl(new
SphereCollisionShape(1f), 5);
bulletAppState.getPhysicsSpace().add(newSegment);
4. Now, we create a
Geometry
instance based on a
Sphere
shape with the same
radius as
RigidBodyControl
. We will use this as a target for mouse clicks.
5. The
Geometry
object needs
modelBound
for which we'll use
Boundin-
gSphere
. The radius may be bigger than
RigidBodyControl
.
6. The
RigidBodyControl
object is added to
Geometry
as a control and we use
the
setPhysicsLocation
method to move it to the to the supplied location, as
follows:
geometry.addControl(newSegment);
newSegment.setPhysicsLocation(location);
7. The
Geometry
object is then added to the segments list we defined earlier and
then it is attached to
rootNode
.
8. If
selectedSegment
is not null, we will call a method we will define next:
createJoint(selectedJoint, newSegment);
9. Lastly, in the
createJoint
method, we set
selectedSegment
to be
newSegment
.
10. Now, we can define the
createJoint
method. It takes two
RigidBodyCon-
trol
parameters as the input, as shown in the following code: