Game Development Reference
In-Depth Information
10. The result is normalized and then modified by a formula to get a suitable gravity.
This value is returned to the calling method, as follows:
relativePosition.normalizeLocal();
return relativePosition.multLocal(size * 1000 /
relativePosition.lengthSquared());
To test all of this, we need to add a few things to
SimpleApplication
. To do this,
perform the following set of steps:
1. First of all, we implement
AnalogListener
.
2. We add an
ArrayList<StellarBody>
list called
gravitationalBod-
ies
.
3. In the
simpleInitApp
method, we should begin by initializing
bulletAp-
pState
and set up some controls for the spaceship. We add actions to rotate the
spaceship to the left and right as well as fire the ship's thruster, as follows:
String[] mappings = new String[]{"rotateLeft",
"rotateRight", "boost"};
inputManager.addListener(this, mappings);
inputManager.addMapping("boost", new
KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addMapping("rotateLeft", new
KeyTrigger(KeyInput.KEY_LEFT));
inputManager.addMapping("rotateRight", new
KeyTrigger(KeyInput.KEY_RIGHT));
4. Since it's a 2D representation, we move the camera some distance up and make it
look as if it is at the center of the world. This can be implemented as follows:
cam.setLocation(new Vector3f(0, 300f, 0));
cam.lookAt(Vector3f.ZERO, Vector3f.UNIT_Y);
5. We create an instance called
ship
of
SpaceShip
and attach its geometry to
rootNode
and
physicsSpace
of
bulletAppState
.
6. Now we can create a number of
StellarBody
instances using the following
steps: