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.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; } } } }
} |