jMonkeyEngine 3.0 Beginner’s Guide
上QQ阅读APP看书,第一时间看更新

Time for action – navigating the scene

Let's look at things from a different perspective:

  1. Run the BasicGame template once more.
  2. Press the W, A, S, and D keys to move forward, left, backwards, and right. Move the mouse to turn around. This navigates your camera away from the default location.
  3. Press the C key while the application is running to print the current camera location and rotation to the console.

In your Java code, you can access the current camera location, which is also the location of the player in a first-person game, using getters on the cam object:

Vector3f   loc = cam.getLocation();
Quaternion rot = cam.getRotation();

If you want the camera to start out at a different location in the scene, position it using the following:

cam.setLocation(x,y,z);
cam.setRotation(q);

The default navigational keys in the included samples are optimized for QWERTY keyboards. Every game has its unique navigation that you will have to define yourself, including a way to let your users with a non-QWERTY keyboard customize the keys.

Note that cam.setRotation(q) is one of the rotation methods that accepts the Quaternion data type, which we just learned about, as an argument.

Pop quiz ‑ spinning around

Q2. How far does geom.rotate(0.0f, 2.1f, 0.0f); rotate the geometry around the y axis?

  1. 2.1 times = 2.1 * 360° = 756° = 36°
  2. 2.1 degrees
  3. 2.1 radians = 120°
  4. 2.1 world units