| | import sun.awt.motif.X11CNS11643; |
---|
| | |
---|
| | import java.awt.*; |
---|
| | import java.awt.event.KeyEvent; |
---|
| | import java.awt.event.MouseEvent; |
---|
| | import java.awt.image.BufferedImage; |
---|
| |
---|
| | |
---|
| | // image that represents the player's position on the board |
---|
| | private BufferedImage image; |
---|
| | // current position of the player on the board grid |
---|
| | private Point3D position; |
---|
| | private Point3D rotation; |
---|
| | |
---|
| | public Matrix camMatrix; |
---|
| | public double Fpdis = 0.4; |
---|
| | private Point3D position = new Point3D(-30,0,0); |
---|
| | private Point3D rotation = new Point3D(0,Math.PI / 2, 0); |
---|
| | private Point3D direction = new Point3D(0,0,0); |
---|
| | public double mouseSensitivity = 0.005; // radians per pixel |
---|
| | // called when the player is initialised |
---|
| | public Player() { |
---|
| | } |
---|
| | |
---|
| |
---|
| | g.drawImage(img, 0, 0, observer); |
---|
| | } |
---|
| | |
---|
| | public void keyPressed(int key) { |
---|
| | if (key == KeyEvent.VK_UP) { |
---|
| | } |
---|
| | if (key == KeyEvent.VK_RIGHT) { |
---|
| | } |
---|
| | if (key == KeyEvent.VK_DOWN) { |
---|
| | } |
---|
| | if (key == KeyEvent.VK_LEFT) { |
---|
| | if (key == KeyEvent.VK_W) { |
---|
| | direction.x = 1; |
---|
| | } else if (key == KeyEvent.VK_S) { |
---|
| | direction.x = -1; |
---|
| | } else if (key == KeyEvent.VK_A) { |
---|
| | direction.y = -1; |
---|
| | } else if (key == KeyEvent.VK_D) { |
---|
| | direction.y = 1; |
---|
| | } else if (key == KeyEvent.VK_SPACE) { |
---|
| | direction.z = 1; |
---|
| | } else if (key == KeyEvent.VK_SHIFT) { |
---|
| | direction.z = -1; |
---|
| | } |
---|
| | } |
---|
| | public void keyReleased(int key){ |
---|
| | |
---|
| | if (key == KeyEvent.VK_W) { |
---|
| | direction.x = 0; |
---|
| | } else if (key == KeyEvent.VK_S) { |
---|
| | direction.x = 0; |
---|
| | } else if (key == KeyEvent.VK_A) { |
---|
| | direction.y = 0; |
---|
| | } else if (key == KeyEvent.VK_D) { |
---|
| | direction.y = 0; |
---|
| | } else if (key == KeyEvent.VK_SPACE) { |
---|
| | direction.z = 0; |
---|
| | } else if (key == KeyEvent.VK_SHIFT) { |
---|
| | direction.z = 0; |
---|
| | } |
---|
| | } |
---|
| | public void tick(Point2D mouseRel) { |
---|
| | // System.out.println(mouseRel.x + " " + mouseRel.y); |
---|
| | // gets called once every tick, before the repainting process happens. |
---|
| | // change rotation depending on mouse movement |
---|
| | rotation.z += mouseRel.x * mouseSensitivity; |
---|
| | rotation.y += mouseRel.y * mouseSensitivity; |
---|
| | // calculate direction vector to translate position by |
---|
| | Matrix zMat = new Matrix(3, 3); |
---|
| | zMat.setItems(new double[][]{ |
---|
| | {Math.cos(rotation.z), Math.sin(rotation.z), 0}, |
---|
| | {-Math.sin(rotation.z), Math.cos(rotation.z), 0}, |
---|
| | {0, 0, 1}} |
---|
| | ); |
---|
| | Matrix yMat = new Matrix(3, 3); |
---|
| | yMat.setItems(new double[][]{ |
---|
| | {Math.cos(rotation.y), 0, -Math.sin(rotation.y)}, |
---|
| | {0, 1, 0 }, |
---|
| | {Math.sin(rotation.y), 0, Math.cos(rotation.y) }} |
---|
| | ); |
---|
| | Matrix xMat = new Matrix(3, 3); |
---|
| | xMat.setItems(new double[][]{ |
---|
| | {1, 0, 0 }, |
---|
| | {0, Math.cos(rotation.x), Math.sin(rotation.x)}, |
---|
| | {0, -Math.sin(rotation.x), Math.cos(rotation.x)}} |
---|
| | ); |
---|
| | Matrix izMat = new Matrix(3, 3); |
---|
| | izMat.setItems(new double[][]{ |
---|
| | {Math.cos(-rotation.z), Math.sin(-rotation.z), 0}, |
---|
| | {-Math.sin(-rotation.z), Math.cos(-rotation.z), 0}, |
---|
| | {0, 0, 1}} |
---|
| | ); |
---|
| | // apply zmat to direction vector |
---|
| | try { |
---|
| | Point3D dirvec = izMat.multiplyPoint3(direction); |
---|
| | position.translate(dirvec); |
---|
| | } catch (Exception e) { |
---|
| | throw new RuntimeException(e); |
---|
| | } |
---|
| | Matrix traMat = new Matrix(4, 3); |
---|
| | traMat.setItems(new double[][] |
---|
| | {{1, 0, 0, -position.x}, |
---|
| | {0, 1, 0, -position.y}, |
---|
| | {0, 0, 1, -position.z}} |
---|
| | ); |
---|
| | // multiply out camera matrix |
---|
| | try { |
---|
| | //camMatrix = ((zMat.multiply(yMat)).multiply(xMat)).multiply(traMat); |
---|
| | // camMatrix = zMat.multiply(yMat); |
---|
| | // camMatrix = camMatrix.multiply(xMat); |
---|
| | // camMatrix = camMatrix.multiply(traMat); |
---|
| | camMatrix = xMat.multiply(yMat); |
---|
| | camMatrix = camMatrix.multiply(zMat); |
---|
| | camMatrix = camMatrix.multiply(traMat); |
---|
| | } catch (Exception e) { |
---|
| | throw new RuntimeException(e); |
---|
| | } |
---|
| | } |
---|
| | |
---|
| | // returnValue Functions |
---|
| | public Point3D getPos() { |
---|
| |
---|
| | |