XithTerrainTest walkaround

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 ::slight_smile:

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:


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!

Ah success! 8)

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 ??

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

Look for a getScale() or getWidth() or getSize() or something like that and do tests :


if(x > 0 && x < width && y > 0 && y < height) {
  // Do the move
}

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??

the class:



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.sub(  viewVector, 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']) strafeCamera(  mspeed);
		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.

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