Go to the documentation of this file.00001 #region Using Statement
00002 using System;
00003 using System.Collections.Generic;
00004 using System.Linq;
00005 using System.Text;
00006 using Microsoft.Xna.Framework;
00007 using Microsoft.Xna.Framework.Input;
00008 using Microsoft.Xna.Framework.Graphics;
00009 #endregion
00010
00011 namespace visLU
00012 {
00016 class InteractiveCamera : Camera
00017 {
00018
00019 #region Variables
00020
00021 Vector3 headingVector;
00022 Vector3 strafeVector;
00023
00024
00025 float maxRotation;
00026 float maxZoomOut;
00027 float velocityFactor;
00028
00029
00030
00031 Vector3 cameraRotation;
00032
00033
00034 Vector3 cameraRotationVelocity;
00035
00036 float zoomTranslation;
00037 MouseState currentMouseState;
00038 MouseState lastMouseState;
00039
00040 #endregion
00041
00042 #region Properties
00043 public float MaxRotation
00044 {
00045 set { maxRotation = value; }
00046 }
00047 public Vector3 CameraRotation
00048 {
00049 get { return cameraRotation; }
00050 set { cameraRotation = value; }
00051
00052 }
00053 public Vector3 CameraRotationVelocity
00054 {
00055 get { return cameraRotationVelocity; }
00056 set { cameraRotationVelocity = value; }
00057 }
00058 #endregion
00059
00060 #region Constructor
00061
00062
00063
00064
00065 public InteractiveCamera(Game _game)
00066 : base(_game)
00067 {
00068
00069
00070
00071
00072
00073
00074 upVector = Vector3.Up;
00075 headingVector = new Vector3(0, 0, -1);
00076 strafeVector = new Vector3(1, 0, 0);
00077
00078
00079 maxRotation = 360.0f;
00080 maxZoomOut = cameraPosition.Z;
00081 velocityFactor = 20.0f;
00082 cameraRotation = Vector3.Zero;
00083 cameraRotationVelocity = Vector3.Zero;
00084 zoomTranslation = 0.0f;
00085
00086 lastMouseState = Mouse.GetState();
00087
00088 }
00089 #endregion
00090
00091 #region Update
00092
00093
00094
00095
00096 public override void Update(GameTime gameTime)
00097 {
00098 #region move camera
00099 currentMouseState = Mouse.GetState();
00100 KeyboardState keyBoardState = Keyboard.GetState();
00101 if ((currentMouseState.LeftButton == ButtonState.Pressed)&&(keyBoardState.IsKeyDown(Keys.LeftAlt)))
00102 {
00103 float fX, fY;
00104 fX = MathHelper.ToRadians(currentMouseState.X - lastMouseState.X);
00105 fY = MathHelper.ToRadians(currentMouseState.Y - lastMouseState.Y);
00106
00107 Matrix transform = Matrix.CreateRotationX(fX) * Matrix.CreateRotationY(-fY);
00108 cameraPosition = Vector3.Transform(cameraPosition, transform);
00109 upVector = Vector3.Transform(upVector, transform);
00110 viewMatrix = Matrix.CreateLookAt(cameraPosition, cameraTarget, upVector);
00111 needUpdateViewProjection = true;
00112 }
00113 #endregion
00114 lastMouseState = currentMouseState;
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142 #region debug
00143
00144
00145
00146
00147 #endregion
00148
00149 base.Update(gameTime);
00150 }
00151
00152 protected override void updateView()
00153 {
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166 Matrix upRotation = Matrix.CreateFromAxisAngle(upVector, MathHelper.ToRadians(cameraRotation.Y));
00167 Matrix strafeRotation = Matrix.CreateFromAxisAngle(strafeVector, MathHelper.ToRadians(cameraRotation.X));
00168
00169 Vector3 newCameraPosition = cameraPosition - cameraTarget;
00170 newCameraPosition = Vector3.Transform(newCameraPosition, upRotation*strafeRotation);
00171
00172 float dir = -1;
00173 if (newCameraPosition.Z < 0) dir = 1;
00174 newCameraPosition = Vector3.Transform(newCameraPosition, Matrix.CreateTranslation(0.0f, 0.0f, dir * zoomTranslation));
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185 viewMatrix = Matrix.CreateLookAt(newCameraPosition + cameraTarget, cameraTarget, upVector);
00186
00187
00188 needUpdateView = false;
00189 needUpdateViewProjection = true;
00190 }
00191
00192 #endregion
00193
00194 #region additional methods
00195
00196 private void setOrientationVectors(Vector3 _cameraPos, Vector3 _cameraTarget, Vector3 _cameraUp)
00197 {
00198 headingVector = _cameraTarget - _cameraPos;
00199 headingVector.Normalize();
00200 upVector = _cameraUp;
00201 strafeVector = Vector3.Cross(headingVector, upVector);
00202 upVector = Vector3.Cross(strafeVector, headingVector);
00203 }
00204 private float clamp(float value, float minValue, float maxValue)
00205 {
00206 if (value > maxValue) value -= maxValue;
00207 if (value < minValue) value += maxValue;
00208
00209 return value;
00210 }
00211
00212 #endregion
00213
00214 }
00215 }