FlyByCamera: comments, annotations, & imports; address GitHub issue #697
This commit is contained in:
parent
0137670487
commit
083f21d6a2
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2009-2012 jMonkeyEngine
|
* Copyright (c) 2009-2017 jMonkeyEngine
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
@ -32,7 +32,11 @@
|
|||||||
package com.jme3.input;
|
package com.jme3.input;
|
||||||
|
|
||||||
import com.jme3.collision.MotionAllowedListener;
|
import com.jme3.collision.MotionAllowedListener;
|
||||||
import com.jme3.input.controls.*;
|
import com.jme3.input.controls.ActionListener;
|
||||||
|
import com.jme3.input.controls.AnalogListener;
|
||||||
|
import com.jme3.input.controls.KeyTrigger;
|
||||||
|
import com.jme3.input.controls.MouseAxisTrigger;
|
||||||
|
import com.jme3.input.controls.MouseButtonTrigger;
|
||||||
import com.jme3.math.FastMath;
|
import com.jme3.math.FastMath;
|
||||||
import com.jme3.math.Matrix3f;
|
import com.jme3.math.Matrix3f;
|
||||||
import com.jme3.math.Quaternion;
|
import com.jme3.math.Quaternion;
|
||||||
@ -40,12 +44,13 @@ import com.jme3.math.Vector3f;
|
|||||||
import com.jme3.renderer.Camera;
|
import com.jme3.renderer.Camera;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A first person view camera controller.
|
* A first-person camera controller.
|
||||||
* After creation, you must register the camera controller with the
|
*
|
||||||
* dispatcher using #registerWithDispatcher().
|
* After creation, you (or FlyCamAppState) must register the controller using
|
||||||
|
* {@link #registerWithInput(com.jme3.input.InputManager)}.
|
||||||
*
|
*
|
||||||
* Controls:
|
* Controls:
|
||||||
* - Move the mouse to rotate the camera
|
* - Move (or, in drag-to-rotate mode, drag) the mouse to rotate the camera
|
||||||
* - Mouse wheel for zooming in or out
|
* - Mouse wheel for zooming in or out
|
||||||
* - WASD keys for moving forward/backward and strafing
|
* - WASD keys for moving forward/backward and strafing
|
||||||
* - QZ keys raise or lower the camera
|
* - QZ keys raise or lower the camera
|
||||||
@ -53,41 +58,62 @@ import com.jme3.renderer.Camera;
|
|||||||
public class FlyByCamera implements AnalogListener, ActionListener {
|
public class FlyByCamera implements AnalogListener, ActionListener {
|
||||||
|
|
||||||
private static String[] mappings = new String[]{
|
private static String[] mappings = new String[]{
|
||||||
CameraInput.FLYCAM_LEFT,
|
CameraInput.FLYCAM_LEFT,
|
||||||
CameraInput.FLYCAM_RIGHT,
|
CameraInput.FLYCAM_RIGHT,
|
||||||
CameraInput.FLYCAM_UP,
|
CameraInput.FLYCAM_UP,
|
||||||
CameraInput.FLYCAM_DOWN,
|
CameraInput.FLYCAM_DOWN,
|
||||||
|
|
||||||
CameraInput.FLYCAM_STRAFELEFT,
|
CameraInput.FLYCAM_STRAFELEFT,
|
||||||
CameraInput.FLYCAM_STRAFERIGHT,
|
CameraInput.FLYCAM_STRAFERIGHT,
|
||||||
CameraInput.FLYCAM_FORWARD,
|
CameraInput.FLYCAM_FORWARD,
|
||||||
CameraInput.FLYCAM_BACKWARD,
|
CameraInput.FLYCAM_BACKWARD,
|
||||||
|
|
||||||
CameraInput.FLYCAM_ZOOMIN,
|
CameraInput.FLYCAM_ZOOMIN,
|
||||||
CameraInput.FLYCAM_ZOOMOUT,
|
CameraInput.FLYCAM_ZOOMOUT,
|
||||||
CameraInput.FLYCAM_ROTATEDRAG,
|
CameraInput.FLYCAM_ROTATEDRAG,
|
||||||
|
|
||||||
CameraInput.FLYCAM_RISE,
|
CameraInput.FLYCAM_RISE,
|
||||||
CameraInput.FLYCAM_LOWER,
|
CameraInput.FLYCAM_LOWER,
|
||||||
|
|
||||||
CameraInput.FLYCAM_INVERTY
|
|
||||||
};
|
|
||||||
|
|
||||||
|
CameraInput.FLYCAM_INVERTY
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* camera controlled by this controller (not null)
|
||||||
|
*/
|
||||||
protected Camera cam;
|
protected Camera cam;
|
||||||
|
/**
|
||||||
|
* normalized "up" direction (a unit vector)
|
||||||
|
*/
|
||||||
protected Vector3f initialUpVec;
|
protected Vector3f initialUpVec;
|
||||||
|
/**
|
||||||
|
* rotation-rate multiplier (1=default)
|
||||||
|
*/
|
||||||
protected float rotationSpeed = 1f;
|
protected float rotationSpeed = 1f;
|
||||||
|
/**
|
||||||
|
* translation speed (in world units per second)
|
||||||
|
*/
|
||||||
protected float moveSpeed = 3f;
|
protected float moveSpeed = 3f;
|
||||||
|
/**
|
||||||
|
* zoom-rate multiplier (1=default)
|
||||||
|
*/
|
||||||
protected float zoomSpeed = 1f;
|
protected float zoomSpeed = 1f;
|
||||||
protected MotionAllowedListener motionAllowed = null;
|
protected MotionAllowedListener motionAllowed = null;
|
||||||
|
/**
|
||||||
|
* enable flag for controller (false→ignoring input)
|
||||||
|
*/
|
||||||
protected boolean enabled = true;
|
protected boolean enabled = true;
|
||||||
|
/**
|
||||||
|
* drag-to-rotate mode flag
|
||||||
|
*/
|
||||||
protected boolean dragToRotate = false;
|
protected boolean dragToRotate = false;
|
||||||
protected boolean canRotate = false;
|
protected boolean canRotate = false;
|
||||||
protected boolean invertY = false;
|
protected boolean invertY = false;
|
||||||
protected InputManager inputManager;
|
protected InputManager inputManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new FlyByCamera to control the given Camera object.
|
* Creates a new FlyByCamera to control the specified camera.
|
||||||
* @param cam
|
*
|
||||||
|
* @param cam camera to be controlled (not null)
|
||||||
*/
|
*/
|
||||||
public FlyByCamera(Camera cam){
|
public FlyByCamera(Camera cam){
|
||||||
this.cam = cam;
|
this.cam = cam;
|
||||||
@ -96,10 +122,11 @@ public class FlyByCamera implements AnalogListener, ActionListener {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the up vector that should be used for the camera.
|
* Sets the up vector that should be used for the camera.
|
||||||
|
*
|
||||||
* @param upVec
|
* @param upVec
|
||||||
*/
|
*/
|
||||||
public void setUpVector(Vector3f upVec) {
|
public void setUpVector(Vector3f upVec) {
|
||||||
initialUpVec.set(upVec);
|
initialUpVec.set(upVec);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setMotionAllowedListener(MotionAllowedListener listener){
|
public void setMotionAllowedListener(MotionAllowedListener listener){
|
||||||
@ -107,56 +134,68 @@ public class FlyByCamera implements AnalogListener, ActionListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the move speed. The speed is given in world units per second.
|
* Set the translation speed.
|
||||||
* @param moveSpeed
|
*
|
||||||
|
* @param moveSpeed new speed (in world units per second)
|
||||||
*/
|
*/
|
||||||
public void setMoveSpeed(float moveSpeed){
|
public void setMoveSpeed(float moveSpeed){
|
||||||
this.moveSpeed = moveSpeed;
|
this.moveSpeed = moveSpeed;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the move speed. The speed is given in world units per second.
|
* Read the translation speed.
|
||||||
* @return moveSpeed
|
*
|
||||||
|
* @return current speed (in world units per second)
|
||||||
*/
|
*/
|
||||||
public float getMoveSpeed(){
|
public float getMoveSpeed(){
|
||||||
return moveSpeed;
|
return moveSpeed;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the rotation speed.
|
* Set the rotation-rate multiplier. The bigger the multiplier, the more
|
||||||
* @param rotationSpeed
|
* rotation for a given movement of the mouse.
|
||||||
|
*
|
||||||
|
* @param rotationSpeed new rate multiplier (1=default)
|
||||||
*/
|
*/
|
||||||
public void setRotationSpeed(float rotationSpeed){
|
public void setRotationSpeed(float rotationSpeed){
|
||||||
this.rotationSpeed = rotationSpeed;
|
this.rotationSpeed = rotationSpeed;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the move speed. The speed is given in world units per second.
|
* Read the rotation-rate multiplier. The bigger the multiplier, the more
|
||||||
* @return rotationSpeed
|
* rotation for a given movement of the mouse.
|
||||||
|
*
|
||||||
|
* @return current rate multiplier (1=default)
|
||||||
*/
|
*/
|
||||||
public float getRotationSpeed(){
|
public float getRotationSpeed(){
|
||||||
return rotationSpeed;
|
return rotationSpeed;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the zoom speed.
|
* Set the zoom-rate multiplier. The bigger the multiplier, the more zoom
|
||||||
* @param zoomSpeed
|
* for a given movement of the mouse wheel.
|
||||||
|
*
|
||||||
|
* @param zoomSpeed new rate multiplier (1=default)
|
||||||
*/
|
*/
|
||||||
public void setZoomSpeed(float zoomSpeed) {
|
public void setZoomSpeed(float zoomSpeed) {
|
||||||
this.zoomSpeed = zoomSpeed;
|
this.zoomSpeed = zoomSpeed;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the zoom speed. The speed is a multiplier to increase/decrease
|
* Read the zoom-rate multiplier. The bigger the multiplier, the more zoom
|
||||||
* the zoom rate.
|
* for a given movement of the mouse wheel.
|
||||||
* @return zoomSpeed
|
*
|
||||||
|
* @return current rate multiplier (1=default)
|
||||||
*/
|
*/
|
||||||
public float getZoomSpeed() {
|
public float getZoomSpeed() {
|
||||||
return zoomSpeed;
|
return zoomSpeed;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param enable If false, the camera will ignore input.
|
* Enable or disable this controller. When disabled, the controller ignored
|
||||||
|
* input.
|
||||||
|
*
|
||||||
|
* @param enable true to enable, false to disable
|
||||||
*/
|
*/
|
||||||
public void setEnabled(boolean enable){
|
public void setEnabled(boolean enable){
|
||||||
if (enabled && !enable){
|
if (enabled && !enable){
|
||||||
@ -168,32 +207,36 @@ public class FlyByCamera implements AnalogListener, ActionListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return If enabled
|
* Test whether this controller is enabled.
|
||||||
* @see FlyByCamera#setEnabled(boolean)
|
*
|
||||||
|
* @return true if enabled, otherwise false
|
||||||
|
* @see #setEnabled(boolean)
|
||||||
*/
|
*/
|
||||||
public boolean isEnabled(){
|
public boolean isEnabled(){
|
||||||
return enabled;
|
return enabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Test whether drag-to-rotate mode is enabled.
|
||||||
|
*
|
||||||
* @return If drag to rotate feature is enabled.
|
* @return If drag to rotate feature is enabled.
|
||||||
*
|
*
|
||||||
* @see FlyByCamera#setDragToRotate(boolean)
|
* @see #setDragToRotate(boolean)
|
||||||
*/
|
*/
|
||||||
public boolean isDragToRotate() {
|
public boolean isDragToRotate() {
|
||||||
return dragToRotate;
|
return dragToRotate;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set if drag to rotate mode is enabled.
|
* Enable or disable drag-to-rotate mode.
|
||||||
*
|
*
|
||||||
* When true, the user must hold the mouse button
|
* When drag-to-rotate mode is enabled, the user must hold the mouse button
|
||||||
* and drag over the screen to rotate the camera, and the cursor is
|
* and drag over the screen to rotate the camera, and the cursor is visible
|
||||||
* visible until dragged. Otherwise, the cursor is invisible at all times
|
* until dragged. When drag-to-rotate mode is disabled, the cursor is
|
||||||
* and holding the mouse button is not needed to rotate the camera.
|
* invisible at all times and holding the mouse button is not needed to
|
||||||
* This feature is disabled by default.
|
* rotate the camera. This mode is disabled by default.
|
||||||
*
|
*
|
||||||
* @param dragToRotate True if drag to rotate mode is enabled.
|
* @param dragToRotate true to enable, false to disable
|
||||||
*/
|
*/
|
||||||
public void setDragToRotate(boolean dragToRotate) {
|
public void setDragToRotate(boolean dragToRotate) {
|
||||||
this.dragToRotate = dragToRotate;
|
this.dragToRotate = dragToRotate;
|
||||||
@ -203,8 +246,9 @@ public class FlyByCamera implements AnalogListener, ActionListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers the FlyByCamera to receive input events from the provided
|
* Register this controller to receive input events from the specified input
|
||||||
* Dispatcher.
|
* manager.
|
||||||
|
*
|
||||||
* @param inputManager
|
* @param inputManager
|
||||||
*/
|
*/
|
||||||
public void registerWithInput(InputManager inputManager){
|
public void registerWithInput(InputManager inputManager){
|
||||||
@ -212,16 +256,16 @@ public class FlyByCamera implements AnalogListener, ActionListener {
|
|||||||
|
|
||||||
// both mouse and button - rotation of cam
|
// both mouse and button - rotation of cam
|
||||||
inputManager.addMapping(CameraInput.FLYCAM_LEFT, new MouseAxisTrigger(MouseInput.AXIS_X, true),
|
inputManager.addMapping(CameraInput.FLYCAM_LEFT, new MouseAxisTrigger(MouseInput.AXIS_X, true),
|
||||||
new KeyTrigger(KeyInput.KEY_LEFT));
|
new KeyTrigger(KeyInput.KEY_LEFT));
|
||||||
|
|
||||||
inputManager.addMapping(CameraInput.FLYCAM_RIGHT, new MouseAxisTrigger(MouseInput.AXIS_X, false),
|
inputManager.addMapping(CameraInput.FLYCAM_RIGHT, new MouseAxisTrigger(MouseInput.AXIS_X, false),
|
||||||
new KeyTrigger(KeyInput.KEY_RIGHT));
|
new KeyTrigger(KeyInput.KEY_RIGHT));
|
||||||
|
|
||||||
inputManager.addMapping(CameraInput.FLYCAM_UP, new MouseAxisTrigger(MouseInput.AXIS_Y, false),
|
inputManager.addMapping(CameraInput.FLYCAM_UP, new MouseAxisTrigger(MouseInput.AXIS_Y, false),
|
||||||
new KeyTrigger(KeyInput.KEY_UP));
|
new KeyTrigger(KeyInput.KEY_UP));
|
||||||
|
|
||||||
inputManager.addMapping(CameraInput.FLYCAM_DOWN, new MouseAxisTrigger(MouseInput.AXIS_Y, true),
|
inputManager.addMapping(CameraInput.FLYCAM_DOWN, new MouseAxisTrigger(MouseInput.AXIS_Y, true),
|
||||||
new KeyTrigger(KeyInput.KEY_DOWN));
|
new KeyTrigger(KeyInput.KEY_DOWN));
|
||||||
|
|
||||||
// mouse only - zoom in/out with wheel, and rotate drag
|
// mouse only - zoom in/out with wheel, and rotate drag
|
||||||
inputManager.addMapping(CameraInput.FLYCAM_ZOOMIN, new MouseAxisTrigger(MouseInput.AXIS_WHEEL, false));
|
inputManager.addMapping(CameraInput.FLYCAM_ZOOMIN, new MouseAxisTrigger(MouseInput.AXIS_WHEEL, false));
|
||||||
@ -277,10 +321,9 @@ public class FlyByCamera implements AnalogListener, ActionListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unregisters the FlyByCamera from the event Dispatcher.
|
* Unregister this controller from its input manager.
|
||||||
*/
|
*/
|
||||||
public void unregisterInput(){
|
public void unregisterInput(){
|
||||||
|
|
||||||
if (inputManager == null) {
|
if (inputManager == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -296,12 +339,16 @@ public class FlyByCamera implements AnalogListener, ActionListener {
|
|||||||
|
|
||||||
Joystick[] joysticks = inputManager.getJoysticks();
|
Joystick[] joysticks = inputManager.getJoysticks();
|
||||||
if (joysticks != null && joysticks.length > 0){
|
if (joysticks != null && joysticks.length > 0){
|
||||||
Joystick joystick = joysticks[0];
|
// No way to unassign axis
|
||||||
|
|
||||||
// No way to unassing axis
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rotate the camera by the specified amount around the specified axis.
|
||||||
|
*
|
||||||
|
* @param value rotation amount
|
||||||
|
* @param axis direction of rotation (a unit vector)
|
||||||
|
*/
|
||||||
protected void rotateCamera(float value, Vector3f axis){
|
protected void rotateCamera(float value, Vector3f axis){
|
||||||
if (dragToRotate){
|
if (dragToRotate){
|
||||||
if (canRotate){
|
if (canRotate){
|
||||||
@ -329,6 +376,11 @@ public class FlyByCamera implements AnalogListener, ActionListener {
|
|||||||
cam.setAxes(q);
|
cam.setAxes(q);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Zoom the camera by the specified amount.
|
||||||
|
*
|
||||||
|
* @param value zoom amount
|
||||||
|
*/
|
||||||
protected void zoomCamera(float value){
|
protected void zoomCamera(float value){
|
||||||
// derive fovY value
|
// derive fovY value
|
||||||
float h = cam.getFrustumTop();
|
float h = cam.getFrustumTop();
|
||||||
@ -338,7 +390,7 @@ public class FlyByCamera implements AnalogListener, ActionListener {
|
|||||||
float near = cam.getFrustumNear();
|
float near = cam.getFrustumNear();
|
||||||
|
|
||||||
float fovY = FastMath.atan(h / near)
|
float fovY = FastMath.atan(h / near)
|
||||||
/ (FastMath.DEG_TO_RAD * .5f);
|
/ (FastMath.DEG_TO_RAD * .5f);
|
||||||
float newFovY = fovY + value * 0.1f * zoomSpeed;
|
float newFovY = fovY + value * 0.1f * zoomSpeed;
|
||||||
if (newFovY > 0f) {
|
if (newFovY > 0f) {
|
||||||
// Don't let the FOV go zero or negative.
|
// Don't let the FOV go zero or negative.
|
||||||
@ -354,6 +406,11 @@ public class FlyByCamera implements AnalogListener, ActionListener {
|
|||||||
cam.setFrustumRight(w);
|
cam.setFrustumRight(w);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Translate the camera upward by the specified amount.
|
||||||
|
*
|
||||||
|
* @param value translation amount
|
||||||
|
*/
|
||||||
protected void riseCamera(float value){
|
protected void riseCamera(float value){
|
||||||
Vector3f vel = new Vector3f(0, value * moveSpeed, 0);
|
Vector3f vel = new Vector3f(0, value * moveSpeed, 0);
|
||||||
Vector3f pos = cam.getLocation().clone();
|
Vector3f pos = cam.getLocation().clone();
|
||||||
@ -366,6 +423,12 @@ public class FlyByCamera implements AnalogListener, ActionListener {
|
|||||||
cam.setLocation(pos);
|
cam.setLocation(pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Translate the camera left or forward by the specified amount.
|
||||||
|
*
|
||||||
|
* @param value translation amount
|
||||||
|
* @param sideways true→left, false→forward
|
||||||
|
*/
|
||||||
protected void moveCamera(float value, boolean sideways){
|
protected void moveCamera(float value, boolean sideways){
|
||||||
Vector3f vel = new Vector3f();
|
Vector3f vel = new Vector3f();
|
||||||
Vector3f pos = cam.getLocation().clone();
|
Vector3f pos = cam.getLocation().clone();
|
||||||
@ -385,6 +448,14 @@ public class FlyByCamera implements AnalogListener, ActionListener {
|
|||||||
cam.setLocation(pos);
|
cam.setLocation(pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Callback to notify this controller of an analog input event.
|
||||||
|
*
|
||||||
|
* @param name name of the input event
|
||||||
|
* @param value value of the axis (from 0 to 1)
|
||||||
|
* @param tpf time per frame (in seconds)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
public void onAnalog(String name, float value, float tpf) {
|
public void onAnalog(String name, float value, float tpf) {
|
||||||
if (!enabled)
|
if (!enabled)
|
||||||
return;
|
return;
|
||||||
@ -416,6 +487,14 @@ public class FlyByCamera implements AnalogListener, ActionListener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Callback to notify this controller of an action input event.
|
||||||
|
*
|
||||||
|
* @param name name of the input event
|
||||||
|
* @param value true if the action is "pressed", false otherwise
|
||||||
|
* @param tpf time per frame (in seconds)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
public void onAction(String name, boolean value, float tpf) {
|
public void onAction(String name, boolean value, float tpf) {
|
||||||
if (!enabled)
|
if (!enabled)
|
||||||
return;
|
return;
|
||||||
@ -424,11 +503,10 @@ public class FlyByCamera implements AnalogListener, ActionListener {
|
|||||||
canRotate = value;
|
canRotate = value;
|
||||||
inputManager.setCursorVisible(!value);
|
inputManager.setCursorVisible(!value);
|
||||||
} else if (name.equals(CameraInput.FLYCAM_INVERTY)) {
|
} else if (name.equals(CameraInput.FLYCAM_INVERTY)) {
|
||||||
// Toggle on the up.
|
// Invert the "up" direction.
|
||||||
if( !value ) {
|
if( !value ) {
|
||||||
invertY = !invertY;
|
invertY = !invertY;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user