Game Development Reference
In-Depth Information
This would be enough for the arrow to follow the laws of physics; however, it will always
face the forward direction. By adding another control, we can make it face the direction of
the velocity. To do this, perform the following steps:
1. Create another class called ArrowFacingControl , extending Ab-
stractControl .
2. We add a Vector3f field called direction .
3. In the controlUpdate method, we get linearVelocity from Ri-
gidBodyControl of the spatial and normalize it. We then store it in direc-
tion as follows:
direction =
spatial.getControl(RigidBodyControl.class).getLinearVelocity().normalize();
4. Then, call the spatial and tell it to rotate to the supplied direction vector as
follows:
spatial.rotateUpTo(direction);
5. In the constructor of the Arrow class, we add an instance of this control, as fol-
lows:
addControl(new ArrowFacingControl());
The last section handles the firing of the arrow from SimpleApplication . This can
be done with the following steps:
1. First of all, we need to implement ActionListener in the application.
2. Add the ActionListener class to inputManager as a listener, together
with a key for firing arrows, as follows:
inputManager.addListener(this, "fire");
inputManager.addMapping("fire", new
KeyTrigger(KeyInput.KEY_SPACE));
3. In the onAction method, call a new method called fireArrow when the
fire button is released. This can be implemented as follows:
if (action.equals("fire") && !isPressed) fireArrow();
Search WWH ::




Custom Search