Applying physics to arrows can greatly improve the appearance and gameplay of a medieval or fantasy game. Setting up arrows that are affected by gravity is fairly simple; this recipe, however, will also set the arrows up in a way that they always face the direction they're traveling in, making them more realistic. The following figure shows one of the arrows in flight:
For this recipe, we need to make sure that we see the debug shapes of physics. To do this, we need to call the bulletAppState.setDebugEnabled(true);
statement.
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:
We create a new class called
Arrow
, extendingNode
.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...