Game Development Reference
In-Depth Information
private static String LEFT_CLICK = "Left Click";
private List<Spatial> selectables = new
ArrayList<Spatial>();
7. The
initialize
method should look familiar if you've read any of the other
game control recipes. We add a mapping for
LEFT_CLICK
and register it with
the application's
InputManager
to ensure it listens for it.
8. The only thing the
onAction
method will currently do is to trigger the
onClick
method when the left mouse button is pressed.
9. Mouse selection (or picking) works by shooting
Ray
from the position of the
mouse cursor into the screen. We begin by getting the position of the mouse curs-
or on the screen as follows:
private void onClick() {
Vector2f mousePos2D =
inputManager.getCursorPosition();
10. Then, we get the position this represents in the game world as follows:
Vector3f mousePos3D =
app.getCamera().getWorldCoordinates(mousePos2D, 0f);
11. Now, we can see what direction this would be by extending the position deeper
into the camera's projection, as follows:
Vector3f clickDir =
mousePos3D.add(app.getCamera().getWorldCoordinates(mousePos2D,
1f)).normalizeLocal();
The following figure shows you how
BoundingVolume
, in the shape of a box,
can enclose the character: