- jMonkeyEngine 3.0 Beginner’s Guide
- Ruth Kusterer
- 260字
- 2025-04-04 22:38:53
Time for action – pick a brick (using the mouse pointer)
Aiming fixed crosshairs is one way to pick objects in the scene. Another option is to make the mouse pointer visible, and allow free clicks.
- Make a copy of the previous exercise,
TargetPickCenter.java
. You can keep the code that handles the mouse click actions and the key press actions for inspiration. - Rename the copy of the class to
TargetPickCursor.java
. Remember to also refactor the first line of themain()
method to the following:TargetPickCursor app = new TargetPickCursor();
- Keep the
myBox()
method, the constants, theanalogListener
object, and the two cubes. Remove theattachCenterMark()
method, and theAnalogListener
object implementation. - By default, the mouse pointer is hidden. To make it visible, add the following to the
simpleInitApp()
method:flyCam.setDragToRotate(true); inputManager.setCursorVisible(true);
Run TargetPickCursor
to see the intermediate result.
What just happened?
Again, you see the red cube above the blue cube. When you move the mouse, you notice that the pointer is a visible arrow now. But you also notice something else; earlier, the view rotated when you moved the mouse to the sides. This feature is part of the default camera behavior and is called mouse look, or free look. Mouse look does not get along with a visible mouse pointer. You can use the mouse either for navigating, or for pointing and clicking. Both at the same time is quite confusing for the player.
You deactivated mouse look when you set flyCam.setDragToRotate(true)
;. To rotate the camera and look around now, keep the left mouse button pressed while moving.