Class ChaseCamera

java.lang.Object
com.jme3.input.ChaseCamera
All Implemented Interfaces:
Savable, ActionListener, AnalogListener, InputListener, Control, JmeCloneable, Cloneable

public class ChaseCamera extends Object implements ActionListener, AnalogListener, Control, JmeCloneable
A camera that follows a spatial and can turn around it by dragging the mouse
  • Field Details

    • target

      protected Spatial target
    • minVerticalRotation

      protected float minVerticalRotation
    • maxVerticalRotation

      protected float maxVerticalRotation
    • minDistance

      protected float minDistance
    • maxDistance

      protected float maxDistance
    • distance

      protected float distance
    • rotationSpeed

      protected float rotationSpeed
    • rotation

      protected float rotation
    • trailingRotationInertia

      protected float trailingRotationInertia
    • zoomSensitivity

      protected float zoomSensitivity
    • rotationSensitivity

      protected float rotationSensitivity
    • chasingSensitivity

      protected float chasingSensitivity
    • trailingSensitivity

      protected float trailingSensitivity
    • vRotation

      protected float vRotation
    • smoothMotion

      protected boolean smoothMotion
    • trailingEnabled

      protected boolean trailingEnabled
    • rotationLerpFactor

      protected float rotationLerpFactor
    • trailingLerpFactor

      protected float trailingLerpFactor
    • rotating

      protected boolean rotating
    • vRotating

      protected boolean vRotating
    • targetRotation

      protected float targetRotation
    • inputManager

      protected InputManager inputManager
    • initialUpVec

      protected Vector3f initialUpVec
    • targetVRotation

      protected float targetVRotation
    • vRotationLerpFactor

      protected float vRotationLerpFactor
    • targetDistance

      protected float targetDistance
    • distanceLerpFactor

      protected float distanceLerpFactor
    • zooming

      protected boolean zooming
    • trailing

      protected boolean trailing
    • chasing

      protected boolean chasing
    • veryCloseRotation

      protected boolean veryCloseRotation
    • canRotate

      protected boolean canRotate
    • offsetDistance

      protected float offsetDistance
    • prevPos

      protected Vector3f prevPos
    • targetMoves

      protected boolean targetMoves
    • enabled

      protected boolean enabled
    • cam

      protected Camera cam
    • targetDir

      protected final Vector3f targetDir
    • previousTargetRotation

      protected float previousTargetRotation
    • pos

      protected final Vector3f pos
    • targetLocation

      protected Vector3f targetLocation
    • dragToRotate

      protected boolean dragToRotate
    • lookAtOffset

      protected Vector3f lookAtOffset
    • leftClickRotate

      protected boolean leftClickRotate
    • rightClickRotate

      protected boolean rightClickRotate
    • temp

      protected Vector3f temp
    • invertYaxis

      protected boolean invertYaxis
    • invertXaxis

      protected boolean invertXaxis
    • ChaseCamDown

      @Deprecated public static final String ChaseCamDown
      Deprecated.
      See Also:
    • ChaseCamUp

      @Deprecated public static final String ChaseCamUp
      Deprecated.
      See Also:
    • ChaseCamZoomIn

      @Deprecated public static final String ChaseCamZoomIn
      See Also:
    • ChaseCamZoomOut

      @Deprecated public static final String ChaseCamZoomOut
      See Also:
    • ChaseCamMoveLeft

      @Deprecated public static final String ChaseCamMoveLeft
      See Also:
    • ChaseCamMoveRight

      @Deprecated public static final String ChaseCamMoveRight
      See Also:
    • ChaseCamToggleRotate

      @Deprecated public static final String ChaseCamToggleRotate
      See Also:
    • zoomin

      protected boolean zoomin
    • hideCursorOnRotate

      protected boolean hideCursorOnRotate
  • Constructor Details

    • ChaseCamera

      public ChaseCamera(Camera cam, Spatial target)
      Constructs the chase camera
      Parameters:
      cam - the application camera
      target - the spatial to follow
    • ChaseCamera

      public ChaseCamera(Camera cam)
      Constructs the chase camera if you use this constructor you have to attach the cam later to a spatial doing spatial.addControl(chaseCamera);
      Parameters:
      cam - the application camera
    • ChaseCamera

      public ChaseCamera(Camera cam, InputManager inputManager)
      Constructs the chase camera, and registers inputs if you use this constructor you have to attach the cam later to a spatial doing spatial.addControl(chaseCamera);
      Parameters:
      cam - the application camera
      inputManager - the inputManager of the application to register inputs
    • ChaseCamera

      public ChaseCamera(Camera cam, Spatial target, InputManager inputManager)
      Constructs the chase camera, and registers inputs
      Parameters:
      cam - the application camera
      target - the spatial to follow
      inputManager - the inputManager of the application to register inputs
  • Method Details

    • onAction

      public void onAction(String name, boolean keyPressed, float tpf)
      Description copied from interface: ActionListener
      Called when an input to which this listener is registered to is invoked.
      Specified by:
      onAction in interface ActionListener
      Parameters:
      name - The name of the mapping that was invoked
      keyPressed - True if the action is "pressed", false otherwise
      tpf - The time per frame value.
    • onAnalog

      public void onAnalog(String name, float value, float tpf)
      Description copied from interface: AnalogListener
      Called to notify the implementation that an analog event has occurred. The results of KeyTrigger and MouseButtonTrigger events will have tpf == value.
      Specified by:
      onAnalog in interface AnalogListener
      Parameters:
      name - The name of the mapping that was invoked
      value - Value of the axis, from 0 to 1.
      tpf - The time per frame value.
    • registerWithInput

      public final void registerWithInput(InputManager inputManager)
      Registers inputs with the input manager
      Parameters:
      inputManager - (alias created)
    • cleanupWithInput

      public void cleanupWithInput(InputManager mgr)
      Cleans up the input mappings from the input manager. Undoes the work of registerWithInput().
      Parameters:
      mgr - the InputManager to clean up
    • setToggleRotationTrigger

      public void setToggleRotationTrigger(Trigger... triggers)
      Sets custom triggers for toggling the rotation of the cam default are new MouseButtonTrigger(MouseInput.BUTTON_LEFT) left mouse button new MouseButtonTrigger(MouseInput.BUTTON_RIGHT) right mouse button
      Parameters:
      triggers - the triggers to assign
    • setZoomInTrigger

      public void setZoomInTrigger(Trigger... triggers)
      Sets custom triggers for zooming in the cam default is new MouseAxisTrigger(MouseInput.AXIS_WHEEL, true) mouse wheel up
      Parameters:
      triggers - the triggers to assign
    • setZoomOutTrigger

      public void setZoomOutTrigger(Trigger... triggers)
      Sets custom triggers for zooming out the cam default is new MouseAxisTrigger(MouseInput.AXIS_WHEEL, false) mouse wheel down
      Parameters:
      triggers - the triggers to assign
    • computePosition

      protected void computePosition()
    • rotateCamera

      protected void rotateCamera(float value)
    • zoomCamera

      protected void zoomCamera(float value)
    • vRotateCamera

      protected void vRotateCamera(float value)
    • updateCamera

      protected void updateCamera(float tpf)
      Updates the camera, should only be called internally
      Parameters:
      tpf - time per frame (in seconds)
    • isEnabled

      public boolean isEnabled()
      Return the enabled/disabled state of the camera
      Returns:
      true if the camera is enabled
    • setEnabled

      public void setEnabled(boolean enabled)
      Enable or disable the camera
      Parameters:
      enabled - true to enable
    • getMaxDistance

      public float getMaxDistance()
      Returns the max zoom distance of the camera (default is 40)
      Returns:
      maxDistance the configured distance (in world units)
    • setMaxDistance

      public void setMaxDistance(float maxDistance)
      Sets the max zoom distance of the camera (default is 40)
      Parameters:
      maxDistance - the desired distance (in world units, default=40)
    • getMinDistance

      public float getMinDistance()
      Returns the min zoom distance of the camera (default is 1)
      Returns:
      minDistance the configured distance (in world units)
    • setMinDistance

      public void setMinDistance(float minDistance)
      Sets the min zoom distance of the camera (default is 1)
      Parameters:
      minDistance - the desired distance (in world units, default=1)
    • cloneForSpatial

      @Deprecated public Control cloneForSpatial(Spatial spatial)
      Deprecated.
      clone this camera for a spatial
      Specified by:
      cloneForSpatial in interface Control
      Parameters:
      spatial - ignored
      Returns:
      never
    • jmeClone

      public Object jmeClone()
      Description copied from interface: JmeCloneable
      Performs a regular shallow clone of the object. Some fields may also be cloned but generally only if they will never be shared with other objects. (For example, local Vector3fs and so on.)

      This method is separate from the regular clone() method so that objects might still maintain their own regular java clone() semantics (perhaps even using Cloner for those methods). However, because Java's clone() has specific features in the sense of Object's clone() implementation, it's usually best to have some path for subclasses to bypass the public clone() method that might be cloning fields and instead get at the superclass protected clone() methods. For example, through super.jmeClone() or another protected clone method that some base class eventually calls super.clone() in.

      Specified by:
      jmeClone in interface JmeCloneable
      Returns:
      a new instance
    • cloneFields

      public void cloneFields(Cloner cloner, Object original)
      Description copied from interface: JmeCloneable
      Implemented to perform deep cloning for this object, resolving local cloned references using the specified cloner. The object can call cloner.clone(fieldValue) to deep clone any of its fields.

      Note: during normal clone operations the original object will not be needed as the clone has already had all of the fields shallow copied.

      Specified by:
      cloneFields in interface JmeCloneable
      Parameters:
      cloner - The cloner that is performing the cloning operation. The cloneFields method can call back into the cloner to make clones of its subordinate fields.
      original - The original object from which this object was cloned. This is provided for the very rare case that this object needs to refer to its original for some reason. In general, all of the relevant values should have been transferred during the shallow clone, and this object need only clone what it wants.
    • setSpatial

      public void setSpatial(Spatial spatial)
      Sets the spatial for the camera control, should only be used internally
      Specified by:
      setSpatial in interface Control
      Parameters:
      spatial - the desired camera target, or null for none
    • update

      public void update(float tpf)
      update the camera control, should only be used internally
      Specified by:
      update in interface Control
      Parameters:
      tpf - time per frame (in seconds)
    • render

      public void render(RenderManager rm, ViewPort vp)
      renders the camera control, should only be used internally
      Specified by:
      render in interface Control
      Parameters:
      rm - ignored
      vp - ignored
    • write

      public void write(JmeExporter ex) throws IOException
      Write the camera
      Specified by:
      write in interface Savable
      Parameters:
      ex - the exporter
      Throws:
      IOException - from the exporter
    • read

      public void read(JmeImporter im) throws IOException
      Read the camera
      Specified by:
      read in interface Savable
      Parameters:
      im - the importer (not null)
      Throws:
      IOException - from the importer
    • getMaxVerticalRotation

      public float getMaxVerticalRotation()
      Returns:
      The maximal vertical rotation angle in radian of the camera around the target
    • setMaxVerticalRotation

      public void setMaxVerticalRotation(float maxVerticalRotation)
      Sets the maximal vertical rotation angle in radian of the camera around the target. Default is Pi/2;
      Parameters:
      maxVerticalRotation - the desired angle (in radians, default=Pi/2)
    • getMinVerticalRotation

      public float getMinVerticalRotation()
      Returns:
      The minimal vertical rotation angle in radian of the camera around the target
    • setMinVerticalRotation

      public void setMinVerticalRotation(float minHeight)
      Sets the minimal vertical rotation angle in radian of the camera around the target default is 0;
      Parameters:
      minHeight - the desired angle (in radians, default=0)
    • isSmoothMotion

      public boolean isSmoothMotion()
      Returns:
      True is smooth motion is enabled for this chase camera
    • setSmoothMotion

      public void setSmoothMotion(boolean smoothMotion)
      Enables smooth motion for this chase camera
      Parameters:
      smoothMotion - true to enable, false to disable (default=false)
    • getChasingSensitivity

      public float getChasingSensitivity()
      returns the chasing sensitivity
      Returns:
      the sensitivity
    • setChasingSensitivity

      public void setChasingSensitivity(float chasingSensitivity)
      Sets the chasing sensitivity, the lower the value the slower the camera will follow the target when it moves default is 5 Only has an effect if smoothMotion is set to true and trailing is enabled
      Parameters:
      chasingSensitivity - the desired value (default=5)
    • getRotationSensitivity

      public float getRotationSensitivity()
      Returns the rotation sensitivity
      Returns:
      the sensitivity
    • setRotationSensitivity

      public void setRotationSensitivity(float rotationSensitivity)
      Sets the rotation sensitivity. The lower the value, the slower the camera will rotate around the target when dragging with the mouse. default is 5, values over 5 should have no effect. If you want a significant slow down try values below 1. Only has an effect if smoothMotion is set to true
      Parameters:
      rotationSensitivity - the desired value (default=5)
    • isTrailingEnabled

      public boolean isTrailingEnabled()
      returns true if the trailing is enabled
      Returns:
      true if enabled, otherwise false
    • setTrailingEnabled

      public void setTrailingEnabled(boolean trailingEnabled)
      Enable the camera trailing : The camera smoothly go in the targets trail when it moves. Only has an effect if smoothMotion is set to true
      Parameters:
      trailingEnabled - true to enable, false to disable (default=true)
    • getTrailingRotationInertia

      public float getTrailingRotationInertia()
      returns the trailing rotation inertia
      Returns:
      the inertia
    • setTrailingRotationInertia

      public void setTrailingRotationInertia(float trailingRotationInertia)
      Sets the trailing rotation inertia : default is 0.1. This causes the camera to stop roughly when the target stops moving before the camera reaches the trail position. Only has an effect if smoothMotion is set to true and trailing is enabled
      Parameters:
      trailingRotationInertia - the desired value (default=0.05)
    • getTrailingSensitivity

      public float getTrailingSensitivity()
      returns the trailing sensitivity
      Returns:
      the sensitivity
    • setTrailingSensitivity

      public void setTrailingSensitivity(float trailingSensitivity)
      Only has an effect if smoothMotion is set to true and trailing is enabled Sets the trailing sensitivity, the lower the value, the slower the camera will go in the target trail when it moves. default is 0.5;
      Parameters:
      trailingSensitivity - the desired value (default=0.5)
    • getZoomSensitivity

      public float getZoomSensitivity()
      returns the zoom sensitivity
      Returns:
      the sensitivity
    • setZoomSensitivity

      public void setZoomSensitivity(float zoomSensitivity)
      Sets the zoom sensitivity, the lower the value, the slower the camera will zoom in and out. default is 2.
      Parameters:
      zoomSensitivity - the desired factor (default=2)
    • getRotationSpeed

      public float getRotationSpeed()
      Returns the rotation speed when the mouse is moved.
      Returns:
      the rotation speed when the mouse is moved.
    • setRotationSpeed

      public void setRotationSpeed(float rotationSpeed)
      Sets the rotate amount when user moves his mouse. The lower the value, the slower the camera will rotate. Default is 1.
      Parameters:
      rotationSpeed - Rotation speed on mouse movement, default is 1.
    • setDefaultDistance

      public void setDefaultDistance(float defaultDistance)
      Sets the default distance at start of application
      Parameters:
      defaultDistance - the desired distance (in world units, default=20)
    • setDefaultHorizontalRotation

      public void setDefaultHorizontalRotation(float angleInRad)
      sets the default horizontal rotation in radian of the camera at start of the application
      Parameters:
      angleInRad - the desired angle (in radians, default=0)
    • setDefaultVerticalRotation

      public void setDefaultVerticalRotation(float angleInRad)
      sets the default vertical rotation in radian of the camera at start of the application
      Parameters:
      angleInRad - the desired angle (in radians, default=Pi/6)
    • isDragToRotate

      public boolean isDragToRotate()
      Returns:
      If drag to rotate feature is enabled.
      See Also:
    • setDragToRotate

      public void setDragToRotate(boolean dragToRotate)
      Parameters:
      dragToRotate - When true, the user must hold the mouse button and drag over the screen to rotate the camera, and the cursor is visible until dragged. Otherwise, the cursor is invisible at all times and holding the mouse button is not needed to rotate the camera. This feature is disabled by default.
    • setDownRotateOnCloseViewOnly

      public void setDownRotateOnCloseViewOnly(boolean rotateOnlyWhenClose)
      Parameters:
      rotateOnlyWhenClose - When this flag is set to false the chase camera will always rotate around its spatial independently of their distance to one another. If set to true, the chase camera will only be allowed to rotated below the "horizon" when the distance is smaller than minDistance + 1.0f (when fully zoomed-in).
    • getDownRotateOnCloseViewOnly

      public boolean getDownRotateOnCloseViewOnly()
      Returns:
      True if rotation below the vertical plane of the spatial tied to the camera is allowed only when zoomed in at minDistance + 1.0f. False if vertical rotation is always allowed.
    • getDistanceToTarget

      public float getDistanceToTarget()
      return the current distance from the camera to the target
      Returns:
      the distance
    • getHorizontalRotation

      public float getHorizontalRotation()
      returns the current horizontal rotation around the target in radians
      Returns:
      the angle
    • getVerticalRotation

      public float getVerticalRotation()
      returns the current vertical rotation around the target in radians.
      Returns:
      the angle in radians
    • getLookAtOffset

      public Vector3f getLookAtOffset()
      returns the offset from the target's position where the camera looks at
      Returns:
      the pre-existing vector
    • setLookAtOffset

      public void setLookAtOffset(Vector3f lookAtOffset)
      Sets the offset from the target's position where the camera looks at
      Parameters:
      lookAtOffset - the desired offset (alias created)
    • setUpVector

      public void setUpVector(Vector3f up)
      Sets the up vector of the camera used for the lookAt on the target
      Parameters:
      up - the desired direction (alias created)
    • getUpVector

      public Vector3f getUpVector()
      Returns the up vector of the camera used for the lookAt on the target
      Returns:
      the pre-existing vector
    • isHideCursorOnRotate

      public boolean isHideCursorOnRotate()
    • setHideCursorOnRotate

      public void setHideCursorOnRotate(boolean hideCursorOnRotate)
    • setInvertVerticalAxis

      public void setInvertVerticalAxis(boolean invertYaxis)
      invert the vertical axis movement of the mouse
      Parameters:
      invertYaxis - true→invert, false→don't invert
    • setInvertHorizontalAxis

      public void setInvertHorizontalAxis(boolean invertXaxis)
      invert the Horizontal axis movement of the mouse
      Parameters:
      invertXaxis - true→invert, false→don't invert