Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mygame;
- import com.jme3.export.InputCapsule;
- import com.jme3.export.JmeExporter;
- import com.jme3.export.JmeImporter;
- import com.jme3.export.OutputCapsule;
- import com.jme3.input.InputManager;
- import com.jme3.math.FastMath;
- import com.jme3.math.Quaternion;
- import com.jme3.math.Vector3f;
- import com.jme3.renderer.Camera;
- import com.jme3.renderer.RenderManager;
- import com.jme3.renderer.ViewPort;
- import com.jme3.scene.Node;
- import com.jme3.scene.Spatial;
- import com.jme3.scene.control.Control;
- import java.io.IOException;
- /**
- * A camera that follows a spatial and can turn around it by dragging the mouse
- * @author nehon (Modified by Taibhse to fit needs)
- *
- */
- public class OrbitCamera implements Control {
- public static boolean printCamLocation = false;
- public static Node target = null; //was spatial
- private static float minHeight = -1.57f; //max height rotation 90deg
- private static float maxHeight = 1.57f; //min height rotation 90deg
- private static float minDistance = 1f; //closest zoom can get
- private static float maxDistance = 500.0f; //furthest distance camera will zoom out
- //public to allow control later, ie for controls
- //allowing user to adjust sensitivity
- public static float zoomSpeed = 2.0f; //speed of zoom
- public static float rotationSpeed = 5f; //speed of rotation
- public static float distance = 20; //starting distance of camera
- public static float maxOrbitDistance = 20;
- /**
- * the camera.
- */
- public static Camera cam = null;
- private InputManager inputManager;
- private static Vector3f initialUpVec;
- public static boolean canRotate;
- public static float rotation = FastMath.PI / 2;
- public static float vRotation = 0;
- public static boolean enabled = false; //should be false by default
- /**
- * Constructs the chase camera
- * @param cam the application camera
- * @param target the spatial to follow
- */
- public OrbitCamera(Camera cam, Vector3f focus) {
- this.target = new Node();
- this.target.setLocalTranslation(focus.x, focus.y, focus.z);
- // target.addControl(this);
- this.cam = cam;
- initialUpVec = cam.getUp().clone();
- }
- /**
- * Constructs the chase camera, and registers inputs
- * @param cam the application camera
- * @param target the spatial to follow
- * @param inputManager the inputManager of the application to register inputs
- */
- public OrbitCamera(Vector3f focus, Camera cam, final Spatial target, InputManager inputManager) {
- this.target = new Node();
- target.setLocalTranslation(focus.x, focus.y, focus.z);
- target.addControl(this);
- this.cam = cam;
- initialUpVec = cam.getUp().clone();
- // registerWithInput(inputManager);
- }
- public void updateFocus(Vector3f focus)
- {
- this.target.setLocalTranslation(focus.x, focus.y, focus.z);
- updateCamera();
- }
- //rotate the camera around the target on the horizontal plane
- public static void rotateCamera(float value) {
- if (!canRotate || !enabled) {
- return;
- }
- rotation += value * rotationSpeed;
- updateCamera();
- }
- //move the camera toward or away the target
- public static void zoomCamera(float value) {
- //commented out as I want to allow zooming even if rotation is disabled
- // if (!enabled) {
- // return;
- // }
- distance += value * zoomSpeed;
- if (distance > maxDistance) {
- distance = maxDistance;
- }
- if (distance < minDistance) {
- distance = minDistance;
- }
- if ((vRotation < minHeight) && (distance > (minDistance + 1.0f))) {
- vRotation = minHeight;
- }
- updateCamera();
- }
- //rotate the camera around the target on the vertical plane
- public static void vRotateCamera(float value) {
- if (!canRotate || !enabled) {
- return;
- }
- vRotation += value * rotationSpeed;
- if (vRotation > maxHeight) {
- vRotation = maxHeight;
- }
- if ((vRotation < minHeight) && (distance > (minDistance + 1.0f))) {
- vRotation = minHeight;
- }
- updateCamera();
- }
- /**
- * Update the camera, should only be called internally
- */
- public static void updateCamera() {
- float hDistance = (distance) * FastMath.sin((FastMath.PI / 2) - vRotation);
- Vector3f pos = new Vector3f(hDistance * FastMath.cos(rotation), distance * FastMath.sin(vRotation), hDistance * FastMath.sin(rotation));
- pos = pos.add(target.getLocalTranslation());
- cam.setLocation(pos);
- cam.lookAt(target.getWorldTranslation(), initialUpVec);
- Canvas.n.setLocalRotation(new Quaternion().set(-cam.getRotation().getX(), -cam.getRotation().getY(), -cam.getRotation().getZ(), cam.getRotation().getW()));
- }
- public void moveCamera(float vertical, float horizontal) {
- distance = 20;
- vRotation = vertical;
- rotation = horizontal;
- }
- public static Vector3f getCameraLocation()
- {
- Vector3f loc = new Vector3f(cam.getLocation().x, cam.getLocation().y, cam.getLocation().z);
- return loc;
- }
- /**
- * Return the enabled/disabled state of the camera
- * @return true if the camera is enabled
- */
- public boolean isEnabled() {
- return enabled;
- }
- /**
- * Enable or disable the camera
- * @param enabled true to enable
- */
- public void setEnabled(boolean enabled) {
- this.enabled = enabled;
- if (!enabled) {
- canRotate = false; // reset this flag in-case it was on before
- }
- }
- /**
- * Returns the max zoom distance of the camera (default is 40)
- * @return maxDistance
- */
- public float getMaxDistance() {
- return maxDistance;
- }
- /**
- * Sets the max zoom distance of the camera (default is 40)
- * @param maxDistance
- */
- public void setMaxDistance(float maxDistance) {
- this.maxDistance = maxDistance;
- }
- /**
- * Returns the min zoom distance of the camera (default is 1)
- * @return minDistance
- */
- public float getMinDistance() {
- return minDistance;
- }
- /**
- * Sets the min zoom distance of the camera (default is 1)
- * @return minDistance
- */
- public void setMinDistance(float minDistance) {
- this.minDistance = minDistance;
- }
- /**
- * clone this camera for a spatial
- * @param spatial
- * @return
- */
- public Control cloneForSpatial(Spatial spatial) {
- // OrbitCamera cc = new OrbitCamera(cam, spatial, inputManager);
- // cc.setMaxDistance(getMaxDistance());
- // cc.setMinDistance(getMinDistance());
- return null;
- }
- /**
- * Sets the spacial for the camera control, should only be used internally
- * @param spatial
- */
- public void setSpatial(Spatial spatial) {
- // target = spatial;
- }
- /**
- * update the camera control, should only be used internally
- * @param tpf
- */
- public void update(float tpf) {
- updateCamera();
- if(printCamLocation == true)
- {
- System.out.println("V-ROT: " + vRotation + " H-ROT: " + rotation + " D: " + distance);
- }
- }
- /**
- * renders the camera control, should on ly be used internally
- * @param rm
- * @param vp
- */
- public void render(RenderManager rm, ViewPort vp) {
- //nothing to render
- }
- /**
- * Write the camera
- * @param ex the exporter
- * @throws IOException
- */
- public void write(JmeExporter ex) throws IOException {
- OutputCapsule capsule = ex.getCapsule(this);
- capsule.write(maxDistance, "maxDistance", 40);
- capsule.write(minDistance, "minDistance", 1);
- }
- /**
- * Read the camera
- * @param im
- * @throws IOException
- */
- public void read(JmeImporter im) throws IOException {
- InputCapsule ic = im.getCapsule(this);
- maxDistance = ic.readFloat("maxDistance", 40);
- minDistance = ic.readFloat("minDistance", 1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement