Game Development Reference
In-Depth Information
9. Now, we create another
Node
, which will be our thruster. Give it the name
Thruster
to be able to identify it more easily later, as follows:
Node thruster = new Node("Thruster");
10. We set
localTranslation
of this so that it will end up at the bottom of the
spaceship, as shown in the following line of code:
thruster.setLocalTranslation(0, -1, 0);
11. Then, we attach it to the
spaceShip
node.
12. Now, we have to attach the
spaceShip
node to both the
rootNode
and
physicsSpace
of
bulletAppState
.
13. To control the thruster and make it more reusable, we will create a class called
ThrusterControl
, extending
AbstractControl
.
14. It'll have one field, a
Spatial
field called
thruster
, that will store the
thruster
node.
15. We will override the
setSpatial
method and set it by calling
getChild("Thruster")
on the supplied spatial.
16. Lastly, we define a new method called
fireBooster()
.
17. Inside this, we subtract the thruster's location from the spaceship's location and
store it in a new
Vector3f
field called
direction
as follows:
Vector3f direction =
spatial.getWorldTranslation().subtract(thruster.getWorldTranslation());
18. Then, we find the
RigidBodyControl
class in the spatial and call
applyIm-
pulse
with the direction vector. We use the inverted direction as the relative po-
sition that the impulse should originate from. This can be implemented as fol-
lows:
spatial.getControl(RigidBodyControl.class).applyImpulse(direction,
direction.negate());
19. In the application class, we have to make it call the
fireBooster
method. We
do this in the
onAnalog
method that was added when we implemented the
An-
alogListener
interface:
if(name.equals("boost") && value > 0){