1 #region Using Statement
3 using System.Collections.Generic;
6 using Microsoft.Xna.Framework;
7 using Microsoft.Xna.Framework.Input;
8 using Microsoft.Xna.Framework.Graphics;
21 protected Quaternion cameraRotation =
new Quaternion(0, 0, 0, 1);
25 public Quaternion
CameraRotation {
get {
return cameraRotation; } set { cameraRotation = value; } }
39 cameraRotation =
new Quaternion(0, 0, 0, 1);
51 needUpdateView =
true;
52 needUpdateProjection =
true;
53 needUpdateViewProjection =
true;
60 public override void Update()
66 Vector3 mouseVector =
new Vector3(cameraInput.X, cameraInput.Y, 0.0f);
67 float mouseTranslate = cameraInput.Z;
68 if (mouseVector.X != 0.0f || mouseVector.Y != 0.0f)
73 Revolve(Vector3.UnitX, mouseVector.Y /
GameProperties.Instance.XNADrawSurfaceHeight * 3.2f);
78 RevolveGlobal(Vector3.UnitZ, -mouseVector.X /
GameProperties.Instance.XNADrawSurfaceWidth * 3.2f);
80 if (mouseTranslate != 0.0f)
82 Translate(
new Vector3(0, 0, mouseTranslate /
GameProperties.Instance.XNADrawSurfaceWidth * 3.2f));
93 worldMatrix = Matrix.Identity;
95 viewMatrix = Matrix.Invert(
96 Matrix.CreateFromQuaternion(cameraRotation) *
97 Matrix.CreateTranslation(cameraPosition)
100 needUpdateView =
false;
101 needUpdateViewProjection =
true;
106 #region additional methods
107 private void Rotate(Vector3 axis,
float angle)
109 axis = Vector3.Transform(axis, Matrix.CreateFromQuaternion(cameraRotation));
110 cameraRotation = Quaternion.Normalize(Quaternion.CreateFromAxisAngle(axis, angle) * cameraRotation);
111 needUpdateView =
true;
114 private void Translate(Vector3 distance)
116 cameraPosition += Vector3.Transform(distance, Matrix.CreateFromQuaternion(cameraRotation));
117 needUpdateView =
true;
120 private void Revolve(Vector3 axis,
float angle)
122 Vector3 revolveAxis = Vector3.Transform(axis, Matrix.CreateFromQuaternion(cameraRotation));
123 Quaternion rotate = Quaternion.CreateFromAxisAngle(revolveAxis, angle);
124 cameraPosition = Vector3.Transform(cameraPosition - cameraTarget, Matrix.CreateFromQuaternion(rotate)) + cameraTarget;
129 private void RotateGlobal(Vector3 axis,
float angle)
131 cameraRotation = Quaternion.Normalize(Quaternion.CreateFromAxisAngle(axis, angle) * cameraRotation);
132 needUpdateView =
true;
135 private void TranslateGlobal(Vector3 distance)
137 cameraPosition += distance;
138 needUpdateView =
true;
141 private void RevolveGlobal(Vector3 axis,
float angle)
143 Quaternion rotate = Quaternion.CreateFromAxisAngle(axis, angle);
144 cameraPosition = Vector3.Transform(cameraPosition - cameraTarget, Matrix.CreateFromQuaternion(rotate)) + cameraTarget;
146 RotateGlobal(axis, angle);