Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (576)
games submitted by our members
Games in WIP (498)
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  
  XithTerrainTest walkaround  (Read 1637 times)
0 Members and 1 Guest are viewing this topic.
Offline megatron

Senior Newbie




Java games rock!


« Posted 2004-04-26 18:12:00 »

I'm new to xith3D. I have gone through the GSG to get to learn the basic stuff and I'm playing around with the demos at the moment.

One of the things that I am trying to do is to create a walkaround for the TerrainTest demo. I can get the camera to move up and back along the X and Z axes using the keyboard as input, however i can't seem to get the camera to rotate around the Y axis (so i can turn using the keyboard)

I've tried many different things to get it going. My brains just a bit fried from trying to figure it out !

Just wondering if anyone can show this noob the light and point me in the right direction  Roll Eyes
Offline megatron

Senior Newbie




Java games rock!


« Reply #1 - Posted 2004-04-27 01:40:52 »

I am finally able to rotate around Y axis using the keboard, but whenever I do and try to walk forwards or strafe in that direction the view resets (stays in the same position but faces the original direction)

In my main render loop all I am doing is checking for keyboard events..

Here is what I have so far:

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  
float ROT_AMT = (float)Math.PI / 36.0f;

//The following methods are called when the user press the keys

private void Up(){                                                                      
      position.z += speed*direction;                                                      
      position.y = terrain.getY(position.x, position.z) + 200;                    
                                                                                   
      lookat.set(position);                                                        
      lookat.x += distance*direction;                                              
      lookat.z += distance*direction;
      lookat.y = terrain.getY(lookat.x, lookat.z) + 200;
     
      view.getTransform(viewT);                                                    
      viewT.lookAt(position,lookat,new Point3f(0,1,0));                                    
      view.setTransform(viewT);                                                        
}                                                                                        
                                                                                   
private void Down(){                                                                    
      position.z -= speed*direction;                                                      
      position.y = terrain.getY(position.x, position.z) + 200;                    
                                                                                   
      lookat.set(position);                                                                
      lookat.x += distance*direction;                                                      
      lookat.z += distance*direction;                                                      
      lookat.y = terrain.getY(lookat.x, lookat.z) + 200;                          
                                                                                           
      view.getTransform(viewT);                                                            
      viewT.lookAt(position,lookat,new Point3f(0,1,0));                                    
      view.setTransform(viewT);                                                            
}                                                                                        
                                                                                   
private void StrafeLeft(){                                                              
      position.x += speed*direction;                                                      
      position.z += speed*direction;                                                      
      position.y = terrain.getY(position.x, position.z) + 200;                    
                                                                                   
      lookat.set(position);                                                                
      lookat.x += distance*direction;                                                      
      lookat.z += distance*direction;                                                      
      lookat.y = terrain.getY(lookat.x, lookat.z) + 200;                          
                                                                                   
      view.getTransform(viewT);                                                            
      viewT.lookAt(position,lookat,new Point3f(0,1,0));                                    
      view.setTransform(viewT);                                                            
}                                                                                        
                                                                                   
private void StrafeRight(){                                                              
      position.x -= speed*direction;                                                      
      position.z -= speed*direction;                                                      
      position.y = terrain.getY(position.x, position.z) + 200;                    
                                                                                   
      lookat.set(position);                                                                
      lookat.x += distance*direction;                                                      
      lookat.z += distance*direction;                                                      
      lookat.y = terrain.getY(lookat.x, lookat.z) + 200;                          
                                                                                           
      view.getTransform(viewT);                                                            
      viewT.lookAt(position,lookat,new Point3f(0,1,0));                                    
      view.setTransform(viewT);                                                            
}                                                                                        
                                                                                   
                                                                                   
private void performRotation(float amt){                                                
      position.y = terrain.getY(position.x, position.z) + 200;                    
                                                                                   
      lookat.set(position);                                                                
      lookat.x = distance*direction;                                                      
      lookat.z = distance*direction;                                                      
      lookat.y = terrain.getY(lookat.x, lookat.z) + 200;                          
                                                                                   
    view.getTransform(viewT);                                                          
      testRotateY.rotY(amt);                                                              
      viewT.mul(testRotateY);                                                              
      view.setTransform(viewT);                                                            
      viewT.lookAt(position,lookat,new Point3f(0,1,0));                                    
}                                                                                      


Please Help!
Offline megatron

Senior Newbie




Java games rock!


« Reply #2 - Posted 2004-04-28 13:36:00 »

Ah success!  Cool

I finally got the walkaround for the terrain demo going..

I incorporated Java Cool Dude's excellent FPS camera system with a few modifications to ensure terrain following and no flying up into the air.

Now - how to set the bounds of the world so I can't leave the terrain arena ??
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline rmdire

Junior Member





« Reply #3 - Posted 2006-06-15 23:24:21 »

Hey

any chance you could show the JCD's FPS camera with your code changes - to follow a given Y height..

I've been trying to do this exact thing - with little success.

thanks

Rmdire
Offline Amos Wenger

Senior Member




Everything's possible, but not everything's fun...


« Reply #4 - Posted 2006-06-16 13:56:23 »

Ah success!  Cool

I finally got the walkaround for the terrain demo going..

I incorporated Java Cool Dude's excellent FPS camera system with a few modifications to ensure terrain following and no flying up into the air.

Now - how to set the bounds of the world so I can't leave the terrain arena ??
Look for a getScale() or getWidth() or getSize() or something like that and do tests :
1  
2  
3  
if(x > 0 && x < width && y > 0 && y < height) {
  // Do the move
}


"Once you start working on something, don't be afraid of failure and don't abandon it. People who work sincerely are the happiest"
Offline rmdire

Junior Member





« Reply #5 - Posted 2006-06-16 14:41:47 »

Thanks for your answer - but I meant the terrain following part.

the camera class has 3 vector3f  - positionVector , viewVector and upVector (which conforms to our standard Xith View )

I have tried modifying the Y on the positionVector , viewVector  - in accordance with terrain height

but I get some really wierd results -

I have also tried modifying tye Matrx4f  - but again get same strange results

any help ?
Offline Amos Wenger

Senior Member




Everything's possible, but not everything's fun...


« Reply #6 - Posted 2006-06-16 14:58:02 »

Thanks for your answer - but I meant the terrain following part.

the camera class has 3 vector3f  - positionVector , viewVector and upVector (which conforms to our standard Xith View )

I have tried modifying the Y on the positionVector , viewVector  - in accordance with terrain height

but I get some really wierd results -

I have also tried modifying tye Matrx4f  - but again get same strange results

any help ?
Really strange.. You're sure positionVector != viewVector??

"Once you start working on something, don't be afraid of failure and don't abandon it. People who work sincerely are the happiest"
Offline rmdire

Junior Member





« Reply #7 - Posted 2006-06-16 21:04:58 »

the class:

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  
public class SceneCamera {
   
   private TransformGroup cameraGroup ;
   
   numberConstants cons = new numberConstants();

     private Vector3f       upVector          = new Vector3f(0f, 0f, 1f),
                      tempVector        = new Vector3f(0f, 0f,.0f),
                      viewVector        = new Vector3f(0f, 1f,.5f),
                      axisVector        = new Vector3f(0f, 0f, 0f),
                      strafeVector      = new Vector3f(0f, 0f, 0f),
                      positionVector    = new Vector3f(0f, 0f,.0f);
     public Vector3f        moveCameraVector  = new Vector3f(0f, 0f, 0f);

     private boolean        keys[]            = null,
                      frameActive       = false;

     private Insets         insets            = null;

     private Point          mousePosition     = null,
                      componentPosition = null;

     private Robot          mouseRobot        = null;

     private float          currentRotX       =   0;
                     

     private int            screenWidth       =   0,
                      screenHeight      =   0,
                      xOffSet           =   0,
                      yOffSet           =   0;
    
     private OpusOne gameBoss;
    
     public float angleY  = 0f,angleZ  = 0f;
     public boolean mouseHasMoved =false;

     public SceneCamera(OpusOne boss,
                boolean keys[],
               TransformGroup cameraGroup,
               javax.swing.JFrame parentFrame){

      this.gameBoss=boss;
      insets           = parentFrame.getInsets();
      this.keys        = keys;
      this.cameraGroup = cameraGroup;
      setComponentPosition(parentFrame.getLocation().x,
                      parentFrame.getLocation().y);

      Toolkit.getDefaultToolkit().addAWTEventListener( new EventListener(),
                                           AWTEvent.KEY_EVENT_MASK        |
                                           AWTEvent.MOUSE_EVENT_MASK      |
                                           AWTEvent.MOUSE_MOTION_EVENT_MASK);
      try{
        mouseRobot = new Robot();
      }
      catch(Exception e){}
     }

     public void setScreenSize(int width, int height){
      screenWidth   = width;
      screenHeight  = height;
      mousePosition = new Point(width>>1,height>>1);
     }

     public void positionCamera(float xPosition, float yPosition, float zPosition,
                         float xView    , float yView    , float zView    ,
                         float xUpVector, float yUpVector, float zUpVector){
      positionVector.set( xPosition, yPosition, zPosition);
      viewVector.set(     xView    , yView    , zView    );
      upVector.set(       xUpVector, yUpVector, zUpVector);
     }

     public void moveCamera(float speed){
       
      moveCameraVector.sub(viewVector, positionVector);
      moveCameraVector.normalize();
      positionVector.scaleAdd(speed, moveCameraVector, positionVector);
      viewVector.scaleAdd(    speed, moveCameraVector, viewVector    );
     
     }
    
   


     public void setViewByMouse(){
     

      int   middleX = (screenWidth  >> 1) ,
           middleY = (screenHeight >> 1) ;

      if((mousePosition.x == middleX) &&
    (mousePosition.y == middleY) )
        return;

      angleY = (float)( (middleX - mousePosition.x) ) / 500.0f;
      angleZ = (float)( (middleY - mousePosition.y) ) / 500.0f;

      if(frameActive)
        mouseRobot.mouseMove(componentPosition.x + middleX, componentPosition.y + middleY);

      currentRotX -= angleZ;

      if(currentRotX > 1.0f)
        currentRotX = 1.0f;
      else
        if(currentRotX < -1.0f)
         currentRotX = -1.0f;
        else{
         axisVector.subviewVector, positionVector);
         axisVector.cross(axisVector, upVector      );
         axisVector.normalize();
         rotateView(angleZ, axisVector.x, axisVector.y, axisVector.z);
         rotateView(angleY, 0, 1, 0);
        }

     }

     private void rotateView(float angle, float x, float y, float z){
      tempVector.sub(viewVector, positionVector);

      float cosTheta         = FastTrig.cos(angle),
           sinTheta         = FastTrig.sin(angle),
           oneMinusCosTheta = 1f     -   cosTheta;

      viewVector.x  = (cosTheta + oneMinusCosTheta * x * x)      * tempVector.x;
      viewVector.x += (oneMinusCosTheta * x * y - z * sinTheta)  * tempVector.y;
      viewVector.x += (oneMinusCosTheta * x * z + y * sinTheta)  * tempVector.z;

      viewVector.y  = (oneMinusCosTheta * x * y + z * sinTheta)  * tempVector.x;
      viewVector.y += (cosTheta + oneMinusCosTheta * y * y)      * tempVector.y;
      viewVector.y += (oneMinusCosTheta * y * z - x * sinTheta)  * tempVector.z;

      viewVector.z  = (oneMinusCosTheta * x * z - y * sinTheta)  * tempVector.x;
      viewVector.z += (oneMinusCosTheta * y * z + x * sinTheta)  * tempVector.y;
      viewVector.z += (cosTheta + oneMinusCosTheta * z * z)      * tempVector.z;

      viewVector.add(positionVector);
     }

     private void strafeCamera(float speed){
      positionVector.x += strafeVector.x * speed;
      positionVector.z += strafeVector.z * speed;

      viewVector.x     += strafeVector.x * speed;
      viewVector.z     += strafeVector.z * speed;
     }

     private void elevateCamera(float speed){
      positionVector.y += upVector.y*speed;
      viewVector.y     += upVector.y*speed;
     }

     private void checkForMovement(float frameInterval){
      float mspeed = cons.moveSpeed * frameInterval;
      float rspeed = cons.raiseSpeed * frameInterval;
     
      if(keys[KeyEvent.VK_UP       ] || keys['W']) moveCamera(    mspeed);
      if(keys[KeyEvent.VK_DOWN     ] || keys['S']) moveCamera(   -mspeed);
      if(keys[KeyEvent.VK_LEFT     ] || keys['A']) strafeCamera( -mspeed);
      if(keys[KeyEvent.VK_RIGHT    ] || keys['D']) strafeCameramspeed);
      if(keys[KeyEvent.VK_PAGE_UP  ] || keys['E']) elevateCamera( rspeed);
      if(keys[KeyEvent.VK_PAGE_DOWN] || keys['Q']) elevateCamera(-rspeed);
     }

     public void update(float frameInterval){
      strafeVector.sub(  viewVector  , positionVector);
      strafeVector.cross(strafeVector, upVector);
      strafeVector.normalize();

      setViewByMouse();
      checkForMovement(frameInterval);
      cameraGroup.getTransform().lookAt(new Vector3f(positionVector),
                                new Vector3f(viewVector    ),
                                new Vector3f(upVector      ));
     }

     public Vector3f getPositionVector() { return positionVector;}
     public Vector3f getStrafeVector()   { return strafeVector  ;}
     public Vector3f getViewVector()     { return viewVector    ;}
     public Vector3f getUpVector()       { return upVector      ;}

     public boolean  getFrameState(){
      return frameActive;
     }

     public void setComponentPosition(int x, int y){
      if(componentPosition == null)
        componentPosition = new Point();

      componentPosition.setLocation(x,y);
     }

     public void setFrameState(boolean state){
      frameActive = state;
     }

     public void mouseMoved(MouseEvent me){
       mouseHasMoved =true;
       mousePosition = me.getPoint();
       mousePosition.x +=insets.left;
       mousePosition.y +=insets.top;
     }

     public class EventListener implements AWTEventListener{

      public void eventDispatched(AWTEvent event){
        if(event instanceof MouseEvent){
         MouseEvent e = (MouseEvent) event;
         switch(e.getID()){
           case MouseEvent.MOUSE_MOVED: mouseMoved(e); break;
           case MouseEvent.MOUSE_DRAGGED: mouseMoved(e); break;
          
           case MouseEvent.MOUSE_PRESSED: gameBoss.fireBullet1(); gameBoss.fireBullet2();break;
         }
        }
      }
     }

}


maybe i have this all wrong but I thought that if I changed the Y (in accordance with the Y value of the terrain ) of positionVector and viewVector the camera should maintain a constant view level and position would follow the terrain. But what happens is the camera gets stuck at the y view and will not follow the mouse up or down while terrain following . when the camera stops - then normal mouse movement  come back.
Offline rmdire

Junior Member





« Reply #8 - Posted 2006-06-17 15:01:12 »

got it sorted - used the elevateCamera method and changed the frameInterval to const: 1

then updated the positionVector and viewVector by the difference between current y height and new y height (+/-)

tsk  - what a whale omlette

rmdire
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.

xsi3rr4x (16 views)
2014-04-15 18:08:23

BurntPizza (14 views)
2014-04-15 03:46:01

UprightPath (27 views)
2014-04-14 17:39:50

UprightPath (12 views)
2014-04-14 17:35:47

Porlus (29 views)
2014-04-14 15:48:38

tom_mai78101 (51 views)
2014-04-10 04:04:31

BurntPizza (110 views)
2014-04-08 23:06:04

tom_mai78101 (211 views)
2014-04-05 13:34:39

trollwarrior1 (180 views)
2014-04-04 12:06:45

CJLetsGame (185 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
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!