Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  camera orbit with zoom  (Read 1602 times)
0 Members and 1 Guest are viewing this topic.
Offline aNt

Senior Member




AFK


« Posted 2004-02-24 12:06:55 »

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

Senior Member




AFK


« Reply #1 - Posted 2004-03-21 16:27:32 »

bump  Grin
Offline Preacher

Junior Member




Java games rock!


« Reply #2 - Posted 2004-03-22 07:38:48 »

any chance of a look at your class Grin 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:)
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline aNt

Senior Member




AFK


« Reply #3 - Posted 2004-03-22 12:30:47 »

if i had anything to look at Smiley i havent got that far... LOL
Offline Preacher

Junior Member




Java games rock!


« Reply #4 - Posted 2004-03-24 07:18:46 »

Well if your interested i can give you what i have so far if your interested Smiley
Offline aNt

Senior Member




AFK


« Reply #5 - Posted 2004-03-24 15:29:44 »

result - thanks dude Smiley

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 Smiley
ant@tomato.co.uk
Offline Preacher

Junior Member




Java games rock!


« Reply #6 - Posted 2004-03-25 07:43:56 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
/**      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 Smiley
Offline Preacher

Junior Member




Java games rock!


« Reply #7 - Posted 2004-03-25 07:46:48 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245  
246  
247  
248  
249  
250  
251  
252  
253  
254  
255  
256  
257  
258  
259  
260  
261  
262  
263  
264  
265  
266  
267  
268  
269  
270  
271  
272  
273  
274  
275  
276  
277  
278  
      /**      <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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
 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 Smiley
Offline aNt

Senior Member




AFK


« Reply #8 - Posted 2004-03-26 10:20:18 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
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.
Offline aNt

Senior Member




AFK


« Reply #9 - Posted 2004-03-26 10:55:20 »

added 'isUpdateScheduled = true;' to my 'setCameraPointAt' at the end and it seems to work Smiley

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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Preacher

Junior Member




Java games rock!


« Reply #10 - Posted 2004-03-29 10:38:52 »

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 <stress> 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 Smiley
Offline Trizzunkz

Junior Newbie





« Reply #11 - Posted 2005-12-07 08:05:54 »

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  Grin. 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.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (24 views)
2014-07-24 01:59:36

Riven (24 views)
2014-07-23 21:16:32

Riven (18 views)
2014-07-23 21:07:15

Riven (21 views)
2014-07-23 20:56:16

ctomni231 (50 views)
2014-07-18 06:55:21

Zero Volt (45 views)
2014-07-17 23:47:54

danieldean (36 views)
2014-07-17 23:41:23

MustardPeter (39 views)
2014-07-16 23:30:00

Cero (55 views)
2014-07-16 00:42:17

Riven (55 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!