Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* KEY INPUT WORKS, BUT MOUSE DOESN'T*/
- public class FreeCamera
- {
- private Vector3 position_;
- private Vector3 rotation_;
- private Vector3 lookAt_;
- private Vector3 direction_;
- private float speed_ = 0.1f;
- private Vector3 mouseRotationBuffer_;
- private MouseState pastMouse_;
- public FreeCamera()
- {
- pastMouse_ = Mouse.GetState();
- resetCamera();
- }
- public Vector3 Position
- {
- get
- { return position_; }
- set
- {
- position_ = value;
- updateLookAt();
- }
- }
- public Vector3 Rotation
- {
- get { return rotation_; }
- set
- {
- rotation_ = value;
- updateLookAt();
- }
- }
- public Vector3 Direction { get { return direction_; } }
- public void resetCamera()
- {
- position_ = new Vector3(0, 0, 50);
- speed_ = 0.3f;
- rotation_ = Vector3.Zero;
- }
- private void updateLookAt()
- {
- Matrix rotationMatrix = Matrix.CreateRotationX(rotation_.X) *
- Matrix.CreateRotationY(rotation_.Y);
- direction_ = Vector3.Transform(Vector3.UnitZ, rotationMatrix);
- lookAt_ = position_ + direction_;
- }
- private void cameraInput(float panelWidth, float panelHeight, float deltaTime)
- {
- KeyboardState keyboardState = Keyboard.GetState();
- MouseState currentMouse = Mouse.GetState();
- Vector3 moveVector = Vector3.Zero;
- float deltaX, deltaY;
- if (currentMouse != pastMouse_)
- {
- deltaX = Mouse.GetState().X - (panelWidth / 2);
- deltaY = Mouse.GetState().Y - (panelHeight / 2);
- mouseRotationBuffer_.X -= 0.01f * deltaX * deltaTime;
- mouseRotationBuffer_.Y -= 0.01f * deltaY * deltaTime;
- if (mouseRotationBuffer_.Y < MathHelper.ToRadians(-75.0f))
- mouseRotationBuffer_.Y -= (mouseRotationBuffer_.Y - MathHelper.ToRadians(90.0f));
- if (mouseRotationBuffer_.Y < MathHelper.ToRadians(90.0f))
- mouseRotationBuffer_.Y -= (mouseRotationBuffer_.Y - MathHelper.ToRadians(90.0f));
- Rotation = new Vector3(-MathHelper.Clamp(mouseRotationBuffer_.Y, MathHelper.ToRadians(-75.0f),
- MathHelper.ToRadians(90.0f)), MathHelper.WrapAngle(mouseRotationBuffer_.X), 0);
- deltaX = 0;
- deltaY = 0;
- }
- if (keyboardState.IsKeyDown(Keys.W))
- moveVector.Z += 1;
- if (keyboardState.IsKeyDown(Keys.S))
- moveVector.Z -= 1;
- if (keyboardState.IsKeyDown(Keys.A))
- moveVector.X += 1;
- if (keyboardState.IsKeyDown(Keys.D))
- moveVector.X -= 1;
- if (keyboardState.IsKeyDown(Keys.R))
- moveVector.Y += 1;
- if (keyboardState.IsKeyDown(Keys.F))
- moveVector.Y -= 1;
- if (moveVector != Vector3.Zero)
- {
- moveVector.Normalize();
- moveVector *= speed_;
- Move(moveVector);
- pastMouse_ = currentMouse;
- }
- }
- public void update(float panelWidth, float panelHeight, float timer)
- {
- cameraInput(panelWidth, panelHeight, timer);
- }
- public void MoveTo(Vector3 position, Vector3 rotation)
- {
- Position = position;
- Rotation = rotation;
- }
- public Vector3 PreviewMove(Vector3 amount)
- {
- Matrix rotate = Matrix.CreateRotationY(Rotation.Y);
- Vector3 movement = new Vector3(amount.X, amount.Y, amount.Z);
- movement = Vector3.Transform(movement, rotate);
- return Position + movement;
- }
- public void Move(Vector3 scale)
- {
- MoveTo(PreviewMove(scale), Rotation);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement