Advertisement
Guest User

Untitled

a guest
Jan 10th, 2011
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 23.95 KB | None | 0 0
  1. package mygame.cameras;
  2.  
  3. import com.jme3.bullet.PhysicsSpace;
  4. import com.jme3.bullet.collision.PhysicsRayTestResult;
  5. import com.jme3.bullet.nodes.PhysicsNode;
  6. import com.jme3.export.InputCapsule;
  7. import com.jme3.export.JmeExporter;
  8. import com.jme3.export.JmeImporter;
  9. import com.jme3.export.OutputCapsule;
  10. import com.jme3.input.*;
  11. import com.jme3.input.controls.ActionListener;
  12. import com.jme3.input.controls.AnalogListener;
  13. import com.jme3.input.controls.MouseAxisTrigger;
  14. import com.jme3.input.controls.MouseButtonTrigger;
  15. import com.jme3.math.FastMath;
  16. import com.jme3.math.Vector3f;
  17. import com.jme3.renderer.Camera;
  18. import com.jme3.renderer.RenderManager;
  19. import com.jme3.renderer.ViewPort;
  20. import com.jme3.scene.Spatial;
  21. import com.jme3.scene.control.Control;
  22. import java.io.IOException;
  23. import java.util.LinkedList;
  24.  
  25. /**
  26.  * this is an extended version of ChaseCamera.
  27.  * @see ChaseCamera for original
  28.  * @author Mark
  29.  */
  30. public class PhysicalChaseCamera implements ActionListener, AnalogListener, Control {
  31.  
  32.     private Spatial target = null;
  33.     private float minVerticalRotation = 0.00f;
  34.     private float maxVerticalRotation = FastMath.PI / 2;
  35.     private float minDistance = 1.0f;
  36.     private float maxDistance = 40.0f;
  37.     private float maxDistanceByZoom = 20.0f;
  38.     private float distance = 20;
  39.     private float zoomSpeed = 2f;
  40.     private float rotationSpeed = 1.0f;
  41.     private float rotation = 0;
  42.     private float trailingRotationInertia = 0.05f;
  43.     private float zoomSensitivity = 5f;
  44.     private float rotationSensitivity = 5f;
  45.     private float chasingSensitivity = 5f;
  46.     private float trailingSensitivity = 0.5f;
  47.     private float vRotation = FastMath.PI / 6;
  48.     private boolean smoothMotion = false;
  49.     private boolean trailingEnabled = true;
  50.     private float rotationLerpFactor = 0;
  51.     private float trailingLerpFactor = 0;
  52.     private boolean rotating = false;
  53.     private boolean vRotating = false;
  54.     private float targetRotation = rotation;
  55.     private InputManager inputManager;
  56.     private Vector3f initialUpVec;
  57.     private float targetVRotation = vRotation;
  58.     private float vRotationLerpFactor = 0;
  59.     private float targetDistance = distance;
  60.     private float distanceLerpFactor = 0;
  61.     private boolean zooming = false;
  62.     private boolean trailing = false;
  63.     private boolean chasing = false;
  64.     private boolean canRotate;
  65.     private float offsetDistance = 0.002f;
  66.     private Vector3f prevPos;
  67.     private boolean targetMoves = false;
  68.     private boolean enabled = true;
  69.     private Camera cam = null;
  70.     private Vector3f targetDir;
  71.     private float previousTargetRotation;
  72.     private Vector3f pos;
  73.     private Vector3f maxPos = new Vector3f();
  74.     private PhysicsSpace myPhysicsSpace;
  75.     boolean zoomin;
  76.  
  77.  
  78.     /**
  79.      * Constructs the physical chase camera, registers inputs and physics
  80.      * @param cam the application camera
  81.      * @param target the spatial to follow
  82.      * @param inputManager the inputManager of the application to register inputs
  83.      * @param physicsSpace the PhysicsSpace, the cam shall be active in
  84.      */
  85.     public PhysicalChaseCamera(Camera cam, final Spatial target,
  86.             InputManager inputManager, PhysicsSpace physicsSpace) {
  87.         //chase cam constructor stuff
  88.         this.target = target;
  89.         this.cam = cam;
  90.         initialUpVec = cam.getUp().clone();
  91.  
  92.         //add PhysicsNode to world physicsSpace
  93.         myPhysicsSpace = physicsSpace;
  94.  
  95.         //chase cam stuff.
  96.         computePosition();
  97.         target.addControl(this);
  98.         prevPos = new Vector3f(target.getWorldTranslation());
  99.         cam.setLocation(pos);
  100.         registerWithInput(inputManager);
  101.     }
  102.  
  103.     public void onAction(String name, boolean keyPressed, float tpf) {
  104.         if (name.equals("toggleRotate") && enabled) {
  105.             if (keyPressed) {
  106.                 canRotate = true;
  107.                 inputManager.setCursorVisible(false);
  108.             } else {
  109.                 canRotate = false;
  110.                 inputManager.setCursorVisible(true);
  111.             }
  112.         }
  113.     }
  114.  
  115.     public void onAnalog(String name, float value, float tpf) {
  116.  
  117.         if (name.equals("mouseLeft")) {
  118.             rotateCamera(-value);
  119.         } else if (name.equals("mouseRight")) {
  120.             rotateCamera(value);
  121.         } else if (name.equals("Up")) {
  122.             vRotateCamera(value);
  123.         } else if (name.equals("Down")) {
  124.             vRotateCamera(-value);
  125.         } else if (name.equals("ZoomIn")) {
  126.             zoomCamera(value);
  127.             if (zoomin == false) {
  128.                 distanceLerpFactor = 0;
  129.             }
  130.             zoomin = true;
  131.         } else if (name.equals("ZoomOut")) {
  132.             zoomCamera(-value);
  133.             if (zoomin == true) {
  134.                 distanceLerpFactor = 0;
  135.             }
  136.             zoomin = false;
  137.         }
  138.  
  139.     }
  140.  
  141.     /**
  142.      * Registers inputs with the input manager
  143.      * @param inputManager
  144.      */
  145.     public void registerWithInput(InputManager inputManager) {
  146.         String[] inputs = {"toggleRotate", "Down", "Up", "mouseLeft", "mouseRight", "ZoomIn", "ZoomOut"};
  147.  
  148.         this.inputManager = inputManager;
  149.  
  150.         inputManager.addMapping("Up", new MouseAxisTrigger(1, true));
  151.         inputManager.addMapping("Down", new MouseAxisTrigger(1, false));
  152.         inputManager.addMapping("ZoomIn", new MouseAxisTrigger(2, true));
  153.         inputManager.addMapping("ZoomOut", new MouseAxisTrigger(2, false));
  154.         inputManager.addMapping("mouseLeft", new MouseAxisTrigger(0, true));
  155.         inputManager.addMapping("mouseRight", new MouseAxisTrigger(0, false));
  156.         //inputManager.addMapping("toggleRotate", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
  157.         inputManager.addMapping("toggleRotate", new MouseButtonTrigger(MouseInput.BUTTON_RIGHT));
  158.  
  159.         inputManager.addListener(this, inputs);
  160.  
  161.  
  162.     }
  163.  
  164.     private void computePosition() {
  165.         float hDistance = (distance) * FastMath.sin((FastMath.PI / 2) - vRotation);
  166.         pos = new Vector3f(hDistance * FastMath.cos(rotation), (distance) * FastMath.sin(vRotation), hDistance * FastMath.sin(rotation));
  167.         pos = pos.add(target.getWorldTranslation());
  168.  
  169.         hDistance = (maxDistanceByZoom) * FastMath.sin((FastMath.PI / 2) - vRotation);
  170.         maxPos = new Vector3f(hDistance * FastMath.cos(rotation), (maxDistanceByZoom) * FastMath.sin(vRotation), hDistance * FastMath.sin(rotation));
  171.         maxPos = maxPos.add(target.getWorldTranslation());
  172.         this.collide();
  173.     }
  174.  
  175.     //rotate the camera around the target on the horizontal plane
  176.     private void rotateCamera(float value) {
  177.         if (!canRotate || !enabled) {
  178.             return;
  179.         }
  180.         rotating = true;
  181.         targetRotation += value * rotationSpeed;
  182.  
  183.  
  184.     }
  185.  
  186.     //move the camera toward or away the target
  187.     private void zoomCamera(float value) {
  188.         if (!enabled) {
  189.             return;
  190.         }
  191.  
  192.         zooming = true;
  193.         maxDistanceByZoom += value * zoomSpeed;
  194.         if (maxDistanceByZoom > maxDistance) {
  195.             maxDistanceByZoom = maxDistance;
  196.         }
  197.         if (maxDistanceByZoom < minDistance) {
  198.             maxDistanceByZoom = minDistance;
  199.         }
  200.         if ((targetVRotation < minVerticalRotation) && (targetDistance > (minDistance + 1.0f))) {
  201.             targetVRotation = minVerticalRotation;
  202.         }
  203.     }
  204.  
  205.     //rotate the camera around the target on the vertical plane
  206.     private void vRotateCamera(float value) {
  207.         if (!canRotate || !enabled) {
  208.             return;
  209.         }
  210.         vRotating = true;
  211.         targetVRotation += value * rotationSpeed;
  212.         if (targetVRotation > maxVerticalRotation) {
  213.             targetVRotation = maxVerticalRotation;
  214.         }
  215.         if ((targetVRotation < minVerticalRotation) && (targetDistance > (minDistance + 1.0f))) {
  216.             targetVRotation = minVerticalRotation;
  217.         }
  218.     }
  219.  
  220.     /**
  221.      * Updates the camera, should only be called internally
  222.      */
  223.     protected void updateCamera(float tpf) {
  224.         if (enabled) {
  225.             if (smoothMotion) {
  226.                 //computation of target direction
  227.                 targetDir = target.getWorldTranslation().subtract(prevPos);
  228.                 float dist = targetDir.length();
  229.  
  230.                 //Low pass filtering on the target postition to avoid shaking when physics are enabled.
  231.                 if (offsetDistance < dist) {
  232.                     //target moves, start chasing.
  233.                     chasing = true;
  234.                     //target moves, start trailing if it has to.
  235.                     if (trailingEnabled) {
  236.                         trailing = true;
  237.                     }
  238.                     //target moves...
  239.                     targetMoves = true;
  240.                 } else {
  241.                     //if target was moving, we compute a slight offset in rotation to avoid a rought stop of the cam
  242.                     //We do not if the player is rotationg the cam
  243.                     if (targetMoves && !canRotate) {
  244.                         if (targetRotation - rotation > trailingRotationInertia) {
  245.                             targetRotation = rotation + trailingRotationInertia;
  246.                         } else if (targetRotation - rotation < -trailingRotationInertia) {
  247.                             targetRotation = rotation - trailingRotationInertia;
  248.                         }
  249.                     }
  250.                     //Target stops
  251.                     targetMoves = false;
  252.                 }
  253.  
  254.                 //the user is rotating the cam by dragging the mouse
  255.                 if (canRotate) {
  256.                     //reseting the trailing lerp factor
  257.                     trailingLerpFactor = 0;
  258.                     //stop trailing user has the control
  259.                     trailing = false;
  260.                 }
  261.  
  262.  
  263.                 if (trailingEnabled && trailing) {
  264.                     if (targetMoves) {
  265.                         //computation if the inverted direction of the target
  266.                         Vector3f a = targetDir.negate().normalizeLocal();
  267.                         //the x unit vector
  268.                         Vector3f b = Vector3f.UNIT_X;
  269.                         //2d is good enough
  270.                         a.y = 0;
  271.                         //computation of the rotation angle between the x axis and the trail
  272.                         if (targetDir.z > 0) {
  273.                             targetRotation = FastMath.TWO_PI - FastMath.acos(a.dot(b));
  274.                         } else {
  275.                             targetRotation = FastMath.acos(a.dot(b));
  276.                         }
  277.                         if (targetRotation - rotation > FastMath.PI || targetRotation - rotation < -FastMath.PI) {
  278.                             targetRotation -= FastMath.TWO_PI;
  279.                         }
  280.  
  281.                         //if there is an important change in the direction while trailing reset of the lerp factor to avoid jumpy movements
  282.                         if (targetRotation != previousTargetRotation && FastMath.abs(targetRotation - previousTargetRotation) > FastMath.PI / 8) {
  283.                             trailingLerpFactor = 0;
  284.                         }
  285.                         previousTargetRotation = targetRotation;
  286.                     }
  287.                     //computing lerp factor
  288.                     trailingLerpFactor = Math.min(trailingLerpFactor + tpf * tpf * trailingSensitivity, 1);
  289.                     //computing rotation by linear interpolation
  290.                     rotation = FastMath.interpolateLinear(trailingLerpFactor, rotation, targetRotation);
  291.  
  292.                     //if the rotation is near the target rotation we're good, that's over
  293.                     if (targetRotation + 0.01f >= rotation && targetRotation - 0.01f <= rotation) {
  294.                         trailing = false;
  295.                         trailingLerpFactor = 0;
  296.                     }
  297.                 }
  298.  
  299.                 //linear interpolation of the distance while chasing
  300.                 if (chasing) {
  301.                     distance = target.getWorldTranslation().subtract(cam.getLocation()).length();
  302.                     distanceLerpFactor = Math.min(distanceLerpFactor + (tpf * tpf * chasingSensitivity * 0.05f), 1);
  303.                     distance = FastMath.interpolateLinear(distanceLerpFactor, distance, targetDistance);
  304.                     if (targetDistance + 0.01f >= distance && targetDistance - 0.01f <= distance) {
  305.                         distanceLerpFactor = 0;
  306.                         chasing = false;
  307.                     }
  308.                 }
  309.  
  310.                 //linear interpolation of the distance while zooming
  311.                 if (zooming) {
  312.                     distanceLerpFactor = Math.min(distanceLerpFactor + (tpf * tpf * zoomSensitivity), 1);
  313.                     distance = FastMath.interpolateLinear(distanceLerpFactor, distance, targetDistance);
  314.                     if (targetDistance + 0.1f >= distance && targetDistance - 0.1f <= distance) {
  315.                         zooming = false;
  316.                         distanceLerpFactor = 0;
  317.                     }
  318.                 }
  319.  
  320.                 //linear interpolation of the rotation while rotating horizontally
  321.                 if (rotating) {
  322.                     rotationLerpFactor = Math.min(rotationLerpFactor + tpf * tpf * rotationSensitivity, 1);
  323.                     rotation = FastMath.interpolateLinear(rotationLerpFactor, rotation, targetRotation);
  324.                     if (targetRotation + 0.01f >= rotation && targetRotation - 0.01f <= rotation) {
  325.                         rotating = false;
  326.                         rotationLerpFactor = 0;
  327.                     }
  328.                 }
  329.  
  330.                 //linear interpolation of the rotation while rotating vertically
  331.                 if (vRotating) {
  332.                     vRotationLerpFactor = Math.min(vRotationLerpFactor + tpf * tpf * rotationSensitivity, 1);
  333.                     vRotation = FastMath.interpolateLinear(vRotationLerpFactor, vRotation, targetVRotation);
  334.                     if (targetVRotation + 0.01f >= vRotation && targetVRotation - 0.01f <= vRotation) {
  335.                         vRotating = false;
  336.                         vRotationLerpFactor = 0;
  337.                     }
  338.                 }
  339.                 //computing the position
  340.                 computePosition();
  341.                 //setting the position at last
  342.                 cam.setLocation(pos);
  343.             } else {
  344.                 //easy no smooth motion
  345.                 vRotation = targetVRotation;
  346.                 rotation = targetRotation;
  347.                 distance = targetDistance;
  348.                 computePosition();
  349.                 cam.setLocation(pos);
  350.             }
  351.             //keeping track on the previous position of the target
  352.             prevPos = new Vector3f(target.getWorldTranslation());
  353.  
  354.             //the cam looks at the target
  355.             cam.lookAt(target.getWorldTranslation(), initialUpVec);
  356.         }
  357.     }
  358.  
  359.     /**
  360.      * Return the enabled/disabled state of the camera
  361.      * @return true if the camera is enabled
  362.      */
  363.     public boolean isEnabled() {
  364.         return enabled;
  365.     }
  366.  
  367.     /**
  368.      * Enable or disable the camera
  369.      * @param enabled true to enable
  370.      */
  371.     public void setEnabled(boolean enabled) {
  372.         this.enabled = enabled;
  373.         if (!enabled) {
  374.             canRotate = false; // reset this flag in-case it was on before
  375.         }
  376.     }
  377.  
  378.     /**
  379.      * Returns the max zoom distance of the camera (default is 40)
  380.      * @return maxDistance
  381.      */
  382.     public float getMaxDistance() {
  383.         return maxDistance;
  384.     }
  385.  
  386.     /**
  387.      * Sets the max zoom distance of the camera (default is 40)
  388.      * @param maxDistance
  389.      */
  390.     public void setMaxDistance(float maxDistance) {
  391.         this.maxDistance = maxDistance;
  392.     }
  393.  
  394.     /**
  395.      * Returns the min zoom distance of the camera (default is 1)
  396.      * @return minDistance
  397.      */
  398.     public float getMinDistance() {
  399.         return minDistance;
  400.     }
  401.  
  402.     /**
  403.      * Sets the min zoom distance of the camera (default is 1)
  404.      * @return minDistance
  405.      */
  406.     public void setMinDistance(float minDistance) {
  407.         this.minDistance = minDistance;
  408.     }
  409.  
  410.     /**
  411.      * clone this camera for a spatial
  412.      * @param spatial
  413.      * @return
  414.      */
  415.     public Control cloneForSpatial(Spatial spatial) {
  416.         ChaseCamera cc = new ChaseCamera(cam, spatial, inputManager);
  417.         cc.setMaxDistance(getMaxDistance());
  418.         cc.setMinDistance(getMinDistance());
  419.         return cc;
  420.     }
  421.  
  422.     /**
  423.      * Sets the spacial for the camera control, should only be used internally
  424.      * @param spatial
  425.      */
  426.     public void setSpatial(Spatial spatial) {
  427.         target = spatial;
  428.     }
  429.  
  430.     /**
  431.      * update the camera control, should on ly be used internally
  432.      * @param tpf
  433.      */
  434.     public void update(float tpf) {
  435.         updateCamera(tpf);
  436.     }
  437.  
  438.     /**
  439.      * renders the camera control, should on ly be used internally
  440.      * @param rm
  441.      * @param vp
  442.      */
  443.     public void render(RenderManager rm, ViewPort vp) {
  444.         //nothing to render
  445.     }
  446.  
  447.     /**
  448.      * Write the camera
  449.      * @param ex the exporter
  450.      * @throws IOException
  451.      */
  452.     public void write(JmeExporter ex) throws IOException {
  453.         OutputCapsule capsule = ex.getCapsule(this);
  454.         capsule.write(maxDistance, "maxDistance", 40);
  455.         capsule.write(minDistance, "minDistance", 1);
  456.     }
  457.  
  458.     /**
  459.      * Read the camera
  460.      * @param im
  461.      * @throws IOException
  462.      */
  463.     public void read(JmeImporter im) throws IOException {
  464.         InputCapsule ic = im.getCapsule(this);
  465.         maxDistance = ic.readFloat("maxDistance", 40);
  466.         minDistance = ic.readFloat("minDistance", 1);
  467.     }
  468.  
  469.     /**
  470.      *
  471.      * @deprecated use getMaxVerticalRotation()
  472.      */
  473.     @Deprecated
  474.     public float getMaxHeight() {
  475.         return getMaxVerticalRotation();
  476.     }
  477.  
  478.     /**
  479.      *
  480.      * @deprecated use setMaxVerticalRotation()
  481.      */
  482.     @Deprecated
  483.     public void setMaxHeight(float maxHeight) {
  484.         setMaxVerticalRotation(maxHeight);
  485.     }
  486.  
  487.     /**
  488.      *
  489.      * @deprecated use getMinVerticalRotation()
  490.      */
  491.     @Deprecated
  492.     public float getMinHeight() {
  493.         return getMinVerticalRotation();
  494.     }
  495.  
  496.     /**
  497.      *
  498.      * @deprecated use setMinVerticalRotation()
  499.      */
  500.     @Deprecated
  501.     public void setMinHeight(float minHeight) {
  502.         setMinVerticalRotation(minHeight);
  503.     }
  504.  
  505.     /**
  506.      * returns the maximal vertical rotation angle of the camera around the target
  507.      * @return
  508.      */
  509.     public float getMaxVerticalRotation() {
  510.         return maxVerticalRotation;
  511.     }
  512.  
  513.     /**
  514.      * sets the maximal vertical rotation angle of the camera around the target default is Pi/2;
  515.      * @param maxVerticalRotation
  516.      */
  517.     public void setMaxVerticalRotation(float maxVerticalRotation) {
  518.         this.maxVerticalRotation = maxVerticalRotation;
  519.     }
  520.  
  521.     /**
  522.      * returns the minimal vertical rotation angle of the camera around the target
  523.      * @return
  524.      */
  525.     public float getMinVerticalRotation() {
  526.         return minVerticalRotation;
  527.     }
  528.  
  529.     /**
  530.      * sets the minimal vertical rotation angle of the camera around the target default is 0;
  531.      * @param minHeight
  532.      */
  533.     public void setMinVerticalRotation(float minHeight) {
  534.         this.minVerticalRotation = minHeight;
  535.     }
  536.  
  537.     /**
  538.      * returns true is smmoth motion is enabled for this chase camera
  539.      * @return
  540.      */
  541.     public boolean isSmoothMotion() {
  542.         return smoothMotion;
  543.     }
  544.  
  545.     /**
  546.      * Enables smooth motion for this chase camera
  547.      * @param smoothMotion
  548.      */
  549.     public void setSmoothMotion(boolean smoothMotion) {
  550.         this.smoothMotion = smoothMotion;
  551.     }
  552.  
  553.     /**
  554.      * returns the chasing sensitivity
  555.      * @return
  556.      */
  557.     public float getChasingSensitivity() {
  558.         return chasingSensitivity;
  559.     }
  560.  
  561.     /**
  562.      * Sets the chasing sensitivity, the lower the value the slower the camera will go in the trail of the target when it moves
  563.      * @param chasingSensitivity
  564.      */
  565.     public void setChasingSensitivity(float chasingSensitivity) {
  566.         this.chasingSensitivity = chasingSensitivity;
  567.     }
  568.  
  569.     /**
  570.      * Returns the rotation sensitivity
  571.      * @return
  572.      */
  573.     public float getRotationSensitivity() {
  574.         return rotationSensitivity;
  575.     }
  576.  
  577.     /**
  578.      * Sets the rotation sensitivity, the lower the value the slower the camera will rotates around the target when draging with the mouse
  579.      * default is 5
  580.      * @param rotationSensitivity
  581.      */
  582.     public void setRotationSensitivity(float rotationSensitivity) {
  583.         this.rotationSensitivity = rotationSensitivity;
  584.     }
  585.  
  586.     /**
  587.      * returns true if the trailing is enabled
  588.      * @return
  589.      */
  590.     public boolean isTrailingEnabled() {
  591.         return trailingEnabled;
  592.     }
  593.  
  594.     /**
  595.      * Enable the camera trailing : The camera smoothly go in the targets trail when it moves.
  596.      * @param trailingEnabled
  597.      */
  598.     public void setTrailingEnabled(boolean trailingEnabled) {
  599.         this.trailingEnabled = trailingEnabled;
  600.     }
  601.  
  602.     /**
  603.      * returns the trailing rotation inertia
  604.      * @return
  605.      */
  606.     public float getTrailingRotationInertia() {
  607.         return trailingRotationInertia;
  608.     }
  609.  
  610.     /**
  611.      * Sets the trailing rotation inertia : default is 0.1. This prevent the camera to roughtly stop when the target stops moving
  612.      * before the camera reached the trail position.
  613.      * @param trailingRotationInertia
  614.      */
  615.     public void setTrailingRotationInertia(float trailingRotationInertia) {
  616.         this.trailingRotationInertia = trailingRotationInertia;
  617.     }
  618.  
  619.     /**
  620.      * returns the trailing sensitivity
  621.      * @return
  622.      */
  623.     public float getTrailingSensitivity() {
  624.         return trailingSensitivity;
  625.     }
  626.  
  627.     /**
  628.      * Sets the trailing sensitivity, the lower the value, the slower the camera will go in the target trail when it moves.
  629.      * default is 0.5;
  630.      * @param trailingSensitivity
  631.      */
  632.     public void setTrailingSensitivity(float trailingSensitivity) {
  633.         this.trailingSensitivity = trailingSensitivity;
  634.     }
  635.  
  636.     /**
  637.      * returns the zoom sensitivity
  638.      * @return
  639.      */
  640.     public float getZoomSensitivity() {
  641.         return zoomSensitivity;
  642.     }
  643.  
  644.     /**
  645.      * Sets the zoom sensitivity, the lower the value, the slower the camera will zoom in and out.
  646.      * default is 5.
  647.      * @param zoomSensitivity
  648.      */
  649.     public void setZoomSensitivity(float zoomSensitivity) {
  650.         this.zoomSensitivity = zoomSensitivity;
  651.     }
  652.  
  653.     /**
  654.      * Sets the default distance at start of applicaiton
  655.      * @param defaultDistance
  656.      */
  657.     public void setDefaultDistance(float defaultDistance) {
  658.         distance = defaultDistance;
  659.         targetDistance = distance;
  660.     }
  661.  
  662.     /**
  663.      * sets the default horizontal rotation of the camera at start of the application
  664.      * @param angle
  665.      */
  666.     public void setDefaultHorizontalRotation(float angle) {
  667.         rotation = angle;
  668.         targetRotation = angle;
  669.     }
  670.  
  671.     /**
  672.      * sets the current zoom distance for the chase camera
  673.      * @param new distance
  674.      */
  675.     public void alterDistance(float alterBy) {
  676.         this.zoomCamera(alterBy);
  677.     }
  678.  
  679.     /**
  680.      * sets the default vertical rotation of the camera at start of the application
  681.      * @param angle
  682.      */
  683.     public void setDefaultVerticalRotation(float angle) {
  684.         vRotation = angle;
  685.         targetVRotation = angle;
  686.     }
  687.  
  688.     public void collide() {
  689.         LinkedList<PhysicsRayTestResult> testResults;
  690.         testResults = (LinkedList) myPhysicsSpace.rayTest(target.getWorldTranslation(), maxPos);
  691.         float hitFraction = 1f;
  692.         if(testResults != null && testResults.size() > 0) {
  693.             hitFraction = testResults.getFirst().getHitFraction();
  694.         }
  695.         targetDistance = ((float)((int)(hitFraction*100)))/100 * maxDistanceByZoom;
  696.     }
  697. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement