Game Development Reference
In-Depth Information
How to do it...
In this recipe, we'll create three classes. Let's begin by looking at the
Arrow
class, which
contains most of the new functionalities. This will be done in the following eight steps:
1. We create a new class called
Arrow
, extending
Node
.
2. Its constructor takes two
Vector3f
variables as parameters. One of these is for
the starting location of the arrow and one for the initial velocity, as shown in the
following line of code:
public Arrow(Vector3f location, Vector3f velocity)
3. Inside the constructor, we define a
Geometry
instance for the body of the arrow
with a
box
mesh as follows:
Box arrowBody = new Box(0.3f, 4f, 0.3f);
Geometry geometry = new Geometry("bullet", arrowBody);
4. Then, we set
localTranslation
of
Geometry
so that one of its ends touches
the center point of the node as follows:
geometry.setLocalTranslation(0f, -4f, 0f);
5. We set
localTranslation
of this
Arrow
as the supplied location.
6. Next, we create
CollisionShape
. This will represent the head of the arrow and
can be
SphereCollisionShape
, as follows:
SphereCollisionShape arrowHeadCollision = new
SphereCollisionShape(0.5f);
7. Now, we define
RigidBodyControl
based on
CollisionShape
, as fol-
lows:
RigidBodyControl rigidBody = new
RigidBodyControl(arrowHeadCollision, 1f);
8. We set
LinearVelocity
of
RigidBodyControl
to be the supplied velocity
and add it as a Control to Arrow, as follows:
rigidBody.setLinearVelocity(velocity);
addControl(rigidBody);