camera orbit with zoom

and got a nice orbit and zoom object - camera running?

bump ;D

any chance of a look at your class ;D have a camera class myself that rotates and tilts around a central point plus zooms in and out but it inverts if you zoom out too far or tilt too far.

Would love too get a look at another approach:)

if i had anything to look at :slight_smile: i havent got that far… LOL

Well if your interested i can give you what i have so far if your interested :slight_smile:

result - thanks dude :slight_smile:

i was thinking of just haveing a node that has a set of transforms than look up the location of that node and set lookat to it, then just point the eye at the object im orbiting… the up vector may get messed up mind.

fire yours over and i will see if we can extend it :slight_smile:
ant@tomato.co.uk

Right heres the my whole camera class. Im using it for a chess game at the mo, so it,s pointed towards the origin and orbits around the board. To rotate left right bring the mouse to the either side of the screen and click and drag. same to tilt but click mouse at top or bottom of the screen. to zoom either scoll the mouse wheel or click the mouse wheel button and drag forward or back.

This is a beta version of a camera for my chess project and it needs to be revised for exactly what i want.

i’m gonna have to split the class up into multible messages it’s to long



/**      A moveable camera for the game
 *
 *      @author Brian Heylin
 */
package com.bnjtech.bnjui;

import edu.itcarlow.bnjchess.*;

// Java3D native packages
import javax.vecmath.*;

// use Xith3D
import com.xith3d.scenegraph.*;

// use Jogl
import com.xith3d.render.*;
import com.xith3d.render.jogl.*;

//      Java packages
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class Camera implements KeyListener, MouseInputListener, MouseWheelListener{
      
      // scenegraph references
      private View view = null;
      private Canvas3D canvas = null;
      
      //      current rotation coords
      private float rotX = 0;
      private float rotY = 0;
      
      //       temp rotation coords while dragging
      private float rotXDisplacement = 0;
      private float rotYDisplacement = 0;
      
      // zoom coords
      private float zoomDistance = 0;                        // current zoom distance
      private float zoomDisplacement = 0;        // tempory zoom distance while dragging
      
      //       saves cursor position when user starts to drag
      private int startDragX = 0;
      private int startDragY = 0;
      
      //      defaults
      private final float defaultDistance = 200;
      private final float defaultY = 100;
  private final float defaultX = 0;
      
      // camera vectors
      private Vector3f centerVector = null;
      private Vector3f upVector = null;
      
      // flags to track camera state
      private boolean isCameraRotating = false;            // true if camera rotating
      private boolean isCameraTilting = false;            // true if camera tilting
      private boolean isCameraZooming = false;            // true if camera zooming
      private boolean isUpdateScheduled = false;      // true if camera needs updating
      private boolean isMoving = false;                                    // true if camera moving
      
      // canvas dimensions
      private int screenWidth = 0;
      private int screenHeight = 0;
      
      // active margins 
      private float bigMargin = 0.98f;   // percentage of screen 
      private float smallMargin = 0.02f;
      
      // rotation, tilting and zoom speed
      private float rotationSpeed = 0.001f;
      private float tiltingSpeed = 0.001f;
      private float zoomSpeed = 1f;
      
      // utility objects
      private Toolkit toolkit = Toolkit.getDefaultToolkit();
      private String rootDirectory = null;
      
      // three cursors
      Cursor handIdle = null;
      Cursor handRotate = null;
      Cursor handTilt = null;


right thats the variable defs :slight_smile:

and heres the methods, i use three .png images to represent the diffrent camera states e.g normal and rotating and tilting, at the moment i just have a pick of the hand from black and white for normal (idle) and the smae pick with a rotating arrow for rotate and a tiliting arrow for tilt


      /**      <Constructor> create a new camera object
       *      @param reference to scenegraph Canvas3D node
       */
      public Camera(Canvas3D canvas){
            
            this.canvas = canvas;             // get scenegraphs canvas node
            view = canvas.getView();      // get scenegraphs view node
            
            // add listeners to canvas
            canvas.get3DPeer().getComponent().addMouseListener(this);
            canvas.get3DPeer().getComponent().addKeyListener(this);
            canvas.get3DPeer().getComponent().addMouseMotionListener(this);
            canvas.get3DPeer().getComponent().addMouseWheelListener(this);
            
            //      define rendering boundaries
            view.setFrontClipDistance(10f);
            view.setBackClipDistance(500f);      
            
            zoomDisplacement = defaultDistance;      // set default distance of camera
            rotYDisplacement = defaultY;                         // set default y coord of camera
            rotXDisplacement = defaultX;                        // set default x coord of camera
            
            centerVector = new Vector3f(0, 0, 0);
            upVector = new Vector3f(0, 1, 0);
            
            updateCamera();      //      set camera position using defaults
            
            screenWidth = toolkit.getScreenSize().width;
            screenHeight = toolkit.getScreenSize().height;
            
            getCursors();
            
            setCursor(handIdle);
            
      }
      
      /** loads the images for each type of cursor and creates
       *  the cursors.
       */
      private void getCursors(){
            
            // load images from this jar file for cursor            
            ImageIcon imgHandIdle = 
                  new ImageIcon(getClass().getResource("/img/HandIdle.png")); 
            ImageIcon imgHandRotate      = 
                  new ImageIcon(getClass().getResource("/img/HandRotate.png"));
            ImageIcon imgHandTilt = 
                  new ImageIcon(getClass().getResource("/img/HandTilt.png"));
            
            // create <Cursors>
            handIdle = toolkit.createCustomCursor(imgHandIdle.getImage(), 
                                                                                                                              new Point(0, 0), 
                                                                                                                              "HandIdle");
            
            handRotate = toolkit.createCustomCursor(imgHandRotate.getImage(), 
                                                                                                                                    new Point(0, 0), 
                                                                                                                                    "HandRotate"); 
            handTilt =  toolkit.createCustomCursor(      imgHandTilt.getImage(), 
                                                                                                                                    new Point(0, 0), 
                                                                                                                                    "HandTilt");

            // release ImageIcon resources
            imgHandIdle = null;
            imgHandRotate = null;
            imgHandTilt = null;
            
      }
      
      /** Sets the cursor to cursor param
       *      @param Cursor to change cursor to.
       */
      private void setCursor(Cursor cursor){
            
            canvas.get3DPeer().getComponent().setCursor(cursor);      
            
      }
      
      /**      Calculates the camera coordinates and sets the lookat transform
       *       of the scenegraphs view node
       */
      public void updateCamera(){
            
            float cos = (float)Math.cos(rotXDisplacement);
            float x = cos*(float)Math.cos(rotYDisplacement) * zoomDisplacement;
            float y =             (float)Math.sin(rotXDisplacement) * zoomDisplacement;
            float z = cos*(float)Math.sin(rotYDisplacement) * zoomDisplacement; 
            
            view.getTransform().lookAt(new Vector3f( x, y, z),  // location of eye
                                 centerVector,                           // center of view
                                 upVector);                                      // vector pointing up      
      }
      
      /** If the mouse is clicked check what region of the screen the mouse
       *  was in when it happend. If it was in one of the movment regions
       *      flag the appropiate movement mode, and save the mouse starting point
       *  in case it's a drag movement.
       */
      public void mousePressed(MouseEvent e){
            
            if((e.getX() > screenWidth*bigMargin) || (e.getX() < screenWidth*smallMargin)){
                  
                  isCameraRotating = true;
                  
            }else if((e.getY() > screenHeight*bigMargin) || (e.getY() < screenHeight*smallMargin)){
                  
                  isCameraTilting = true;
                  
            }
            
            // save start position of cursor
            startDragX = e.getX();
            startDragY = e.getY();
            
      }
      
      /** When the mouse is released, write bakc all the displacement values
       *  and reset the flags
       */
      public void mouseReleased(MouseEvent e){
            
            // update current coords using temp values
            rotX = rotYDisplacement;
    rotY = rotXDisplacement;
            zoomDistance = zoomDisplacement;
            
            setCursor(handIdle);
            
            // reset camera states
            isCameraRotating = false;      
            isCameraTilting = false;
            
      }
      
      /** If the mouse is moved check what region of the screen
       *  it's in and assign the appropiate cursor.
       */
      public void mouseMoved(MouseEvent e){
            
            if((e.getX() > screenWidth*bigMargin) || (e.getX() < screenWidth*smallMargin)){
                  
                  setCursor(handRotate);
                  
            }else if((e.getY() > screenHeight*bigMargin) || (e.getY() < screenHeight*smallMargin)){
                  
                  setCursor(handTilt);
                  
            }else{
                  
                  setCursor(handIdle);
                  
            }
            
      }
      
      /** If the mouse is dragged determine what button is pressed 
       *      and call the appropiate method  
   */
      public void mouseDragged(MouseEvent e){
            
            switch(e.getModifiers()){
                  
        case MouseEvent.BUTTON1_MASK: leftDrag(e); break;
        case MouseEvent.BUTTON2_MASK: middleDrag(e); break;
        case MouseEvent.BUTTON3_MASK: rightDrag(e); break;
        
    }
    
      }
      
      public void mouseWheelMoved(MouseWheelEvent e){
            
            if(e.getWheelRotation() < 0){
                  
                  zoomDisplacement = zoomDistance + (e.getScrollAmount() * zoomSpeed);
          isUpdateScheduled = true;
          
            }else if(e.getWheelRotation() > 0){
                  
                  zoomDisplacement = zoomDistance - (e.getScrollAmount() * zoomSpeed);
          isUpdateScheduled = true;
                  
            }
            
            zoomDistance = zoomDisplacement;
            
      }
      
      // unused methods but have to be here to satisfy listener interfaces
      public void mouseClicked(MouseEvent e){}
      public void mouseEntered(MouseEvent e){}
      public void mouseExited(MouseEvent e){}
      
      /** If the left button is pressed and dragged
       *      check what movement mode the camera is in and 
       *  calculate the relavent displacement and schedule a
       *  camera update.
       */
      private void leftDrag(MouseEvent e){
            
            if(isCameraRotating == true){
                  
                  rotYDisplacement = rotX + (e.getX() - startDragX) * rotationSpeed;
                  isUpdateScheduled = true;
                  
            }else if(isCameraTilting == true){
                  
                  rotXDisplacement = rotY + (e.getY() - startDragY) * tiltingSpeed;
                  isUpdateScheduled = true;
                  
            }
      }
      
      /** If the middle button is pressed and dragged
       *      calculate the zoomDisplacement and schedule a camera update
       */
      private void middleDrag(MouseEvent e){
            
            zoomDisplacement = zoomDistance - (e.getY() - startDragY) * zoomSpeed;
    isUpdateScheduled = true;
    
      }
      
      /** No function for right mouse button at the moment
       *  
       */
      private void rightDrag(MouseEvent e){      }
      
      /** 
       *  
       */
      public void keyTyped(KeyEvent e){
            
            switch(e.getKeyChar()){
                  case KeyEvent.VK_SPACE: ; break;      //      On Space 
            }
            
      }
      
      public void keyPressed(KeyEvent e){}
      public void keyReleased(KeyEvent e){}
      
      /** Check if the camera needs to be updated 
       *      @return true if the camera needs to be updated
       */
      public boolean isUpdateScheduled(){ return isUpdateScheduled; }
      
      /** Set the camera to dirty or clean e.g. needs to be updated or not
       *      @param 
       */ 
      public void setUpdateScheduled(boolean isUpdateScheduled){
            this.isUpdateScheduled = isUpdateScheduled;
      }      
      
      /**      Get the      cameras current zoom distance
       *      @return zoom distance
       */
      public float getZoomDistance(){ return zoomDistance; }
      
      /** Get the cameras current X coord
       *      @return x coord of camera
       */
      public float getRotX(){ return rotX; }
      
      /** Get the camera current Y coord 
       *      @return Y coord of camera
       */
      public float getRotY(){ return rotY; }
      
      /** Check if the camera is currently moving
       *  @return true if camera is moving
       */
      public boolean isMoving(){ return isMoving; }      
      
      public void setActive(boolean active){
            
      }
      
}



The camera is checked using the main rendering loop to see if it’s dirty e.g. needs to be updated and if so its update() method is called.



 while(renderActive){
          
      view.renderOnce();
      
      if(camera.isUpdateScheduled()){
            
            camera.updateCamera();
                        camera.setUpdateScheduled(false);
      }

      // other rendering stuff
                        
    }// end of while render loop



hope that gives you a start, i’ll be looking over this soon enough, i hope to add a method that enables me to press the space bar and return to a default location in a smooth movement, if you have any ideas on that it would be great :slight_smile:

nice

i just added this so i could move the origin around but i seems to behave in a strage way… i would have thought lookAt tracked the canter? i want the camera to point at the moving center. maybe im doing it wrong? heres my code:


public void setCameraPointAt(Vector3f pointAt) {
    centerVector.x = centerVector.x + pointAt.x;
    centerVector.y = centerVector.y + pointAt.y;
    centerVector.z = centerVector.z + pointAt.z;
  }

  public void keyTyped(KeyEvent e) {
    if ( (e.getID() == KeyEvent.KEY_TYPED) && (e.getKeyChar() == 'w')) {
      setCameraPointAt(new Vector3f(0f, 0f, -0.1f));
    }

    if ( (e.getID() == KeyEvent.KEY_TYPED) && (e.getKeyChar() == 's')) {
      setCameraPointAt(new Vector3f(0f, 0f, 0.1f));
    }

    if ( (e.getID() == KeyEvent.KEY_TYPED) && (e.getKeyChar() == 'a')) {
      setCameraPointAt(new Vector3f( -0.1f, 0f, 0f));
    }

    if ( (e.getID() == KeyEvent.KEY_TYPED) && (e.getKeyChar() == 'd')) {
      setCameraPointAt(new Vector3f(0.1f, 0f, 0f));
    }

    if ( (e.getID() == KeyEvent.KEY_TYPED) && (e.getKeyChar() == 'd')) {
      setCameraPointAt(new Vector3f(0.1f, 0f, 0f));
    }
  }

any ideas- i just effected your ‘centerVector’ didnt change anything else.

added ‘isUpdateScheduled = true;’ to my ‘setCameraPointAt’ at the end and it seems to work :slight_smile:

now only one thing. if u use my ‘w’ and ‘s’ keys to move forwards and backwards. strange things happen to the ‘center’. if i move the ‘center’ forwards will the camera location come with me?

as in i would like to move the ‘center’ and the ‘eye’ at the same time. but i dont want to bust yer nice rotation code…

ex: if i had a car on a road that i want to orbit and at the same time track the car moving along the road.

check out this post if you want an example of a Third Person camera.

http://www.java-gaming.org/cgi-bin/JGNetForums/YaBB.cgi?board=xith3d;action=display;num=1075638718

as i tried moving the origin before mself and ran into the same problams, but since i don’t really need to move my origin for what i want and i’m trying to finished a college project at the mo so i don’t have the time to figure out why this isn’t working.

Sorry couldn’t be more of a help :slight_smile:

Sorry to dig up an old thread, but I just had to say thankyou to Preacher.
I’ve spend all day trolling through this forum trying to find ways of doing camera interaction, and the code provided here by Preacher was almost exactly what I needed. I’ve made a few minor alterations to make it suit my universe, but it works beautifuly ;D. Just one problem though, I get a ‘cannot invert matrix’ error when I zoom in too close, but I’ve solved that by limiting the zoom factor (I didn’t need/want to be able to zoom through the center anyway). Overall, an excellent piece of code and very helpful.
Thankyou very much. You have saved me many days of headaches trying to nut through the problem by myself.