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.