SHOW:
|
|
- or go back to the newest paste.
| 1 | - | /* KEY INPUT WORKS, BUT MOUSE DOESN'T |
| 1 | + | /* KEY INPUT WORKS, BUT MOUSE DOESN'T*/ |
| 2 | public class FreeCamera | |
| 3 | {
| |
| 4 | private Vector3 position_; | |
| 5 | private Vector3 rotation_; | |
| 6 | private Vector3 lookAt_; | |
| 7 | private Vector3 direction_; | |
| 8 | private float speed_ = 0.1f; | |
| 9 | private Vector3 mouseRotationBuffer_; | |
| 10 | private MouseState pastMouse_; | |
| 11 | ||
| 12 | ||
| 13 | public FreeCamera() | |
| 14 | {
| |
| 15 | pastMouse_ = Mouse.GetState(); | |
| 16 | resetCamera(); | |
| 17 | } | |
| 18 | ||
| 19 | public Vector3 Position | |
| 20 | {
| |
| 21 | get | |
| 22 | { return position_; }
| |
| 23 | ||
| 24 | set | |
| 25 | {
| |
| 26 | position_ = value; | |
| 27 | updateLookAt(); | |
| 28 | } | |
| 29 | } | |
| 30 | ||
| 31 | public Vector3 Rotation | |
| 32 | {
| |
| 33 | get { return rotation_; }
| |
| 34 | set | |
| 35 | {
| |
| 36 | rotation_ = value; | |
| 37 | updateLookAt(); | |
| 38 | } | |
| 39 | } | |
| 40 | ||
| 41 | public Vector3 Direction { get { return direction_; } }
| |
| 42 | ||
| 43 | ||
| 44 | public void resetCamera() | |
| 45 | {
| |
| 46 | position_ = new Vector3(0, 0, 50); | |
| 47 | speed_ = 0.3f; | |
| 48 | rotation_ = Vector3.Zero; | |
| 49 | ||
| 50 | } | |
| 51 | ||
| 52 | private void updateLookAt() | |
| 53 | {
| |
| 54 | Matrix rotationMatrix = Matrix.CreateRotationX(rotation_.X) * | |
| 55 | Matrix.CreateRotationY(rotation_.Y); | |
| 56 | ||
| 57 | direction_ = Vector3.Transform(Vector3.UnitZ, rotationMatrix); | |
| 58 | lookAt_ = position_ + direction_; | |
| 59 | } | |
| 60 | ||
| 61 | private void cameraInput(float panelWidth, float panelHeight, float deltaTime) | |
| 62 | {
| |
| 63 | ||
| 64 | KeyboardState keyboardState = Keyboard.GetState(); | |
| 65 | MouseState currentMouse = Mouse.GetState(); | |
| 66 | ||
| 67 | Vector3 moveVector = Vector3.Zero; | |
| 68 | float deltaX, deltaY; | |
| 69 | ||
| 70 | if (currentMouse != pastMouse_) | |
| 71 | {
| |
| 72 | deltaX = Mouse.GetState().X - (panelWidth / 2); | |
| 73 | deltaY = Mouse.GetState().Y - (panelHeight / 2); | |
| 74 | ||
| 75 | mouseRotationBuffer_.X -= 0.01f * deltaX * deltaTime; | |
| 76 | mouseRotationBuffer_.Y -= 0.01f * deltaY * deltaTime; | |
| 77 | ||
| 78 | if (mouseRotationBuffer_.Y < MathHelper.ToRadians(-75.0f)) | |
| 79 | mouseRotationBuffer_.Y -= (mouseRotationBuffer_.Y - MathHelper.ToRadians(90.0f)); | |
| 80 | if (mouseRotationBuffer_.Y < MathHelper.ToRadians(90.0f)) | |
| 81 | mouseRotationBuffer_.Y -= (mouseRotationBuffer_.Y - MathHelper.ToRadians(90.0f)); | |
| 82 | ||
| 83 | Rotation = new Vector3(-MathHelper.Clamp(mouseRotationBuffer_.Y, MathHelper.ToRadians(-75.0f), | |
| 84 | MathHelper.ToRadians(90.0f)), MathHelper.WrapAngle(mouseRotationBuffer_.X), 0); | |
| 85 | ||
| 86 | deltaX = 0; | |
| 87 | deltaY = 0; | |
| 88 | } | |
| 89 | ||
| 90 | if (keyboardState.IsKeyDown(Keys.W)) | |
| 91 | moveVector.Z += 1; | |
| 92 | if (keyboardState.IsKeyDown(Keys.S)) | |
| 93 | moveVector.Z -= 1; | |
| 94 | if (keyboardState.IsKeyDown(Keys.A)) | |
| 95 | moveVector.X += 1; | |
| 96 | if (keyboardState.IsKeyDown(Keys.D)) | |
| 97 | moveVector.X -= 1; | |
| 98 | if (keyboardState.IsKeyDown(Keys.R)) | |
| 99 | moveVector.Y += 1; | |
| 100 | if (keyboardState.IsKeyDown(Keys.F)) | |
| 101 | moveVector.Y -= 1; | |
| 102 | ||
| 103 | if (moveVector != Vector3.Zero) | |
| 104 | {
| |
| 105 | moveVector.Normalize(); | |
| 106 | moveVector *= speed_; | |
| 107 | ||
| 108 | Move(moveVector); | |
| 109 | ||
| 110 | pastMouse_ = currentMouse; | |
| 111 | } | |
| 112 | } | |
| 113 | ||
| 114 | public void update(float panelWidth, float panelHeight, float timer) | |
| 115 | {
| |
| 116 | cameraInput(panelWidth, panelHeight, timer); | |
| 117 | } | |
| 118 | ||
| 119 | ||
| 120 | public void MoveTo(Vector3 position, Vector3 rotation) | |
| 121 | {
| |
| 122 | Position = position; | |
| 123 | Rotation = rotation; | |
| 124 | } | |
| 125 | ||
| 126 | ||
| 127 | public Vector3 PreviewMove(Vector3 amount) | |
| 128 | {
| |
| 129 | Matrix rotate = Matrix.CreateRotationY(Rotation.Y); | |
| 130 | Vector3 movement = new Vector3(amount.X, amount.Y, amount.Z); | |
| 131 | movement = Vector3.Transform(movement, rotate); | |
| 132 | return Position + movement; | |
| 133 | } | |
| 134 | ||
| 135 | public void Move(Vector3 scale) | |
| 136 | {
| |
| 137 | MoveTo(PreviewMove(scale), Rotation); | |
| 138 | } | |
| 139 | } |