1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
namespace Jeu.Moteur_Graphique.Camera
{
/// <summary>
/// This is a game component that implements IUpdateable.
/// </summary>
public class Camera : Microsoft.Xna.Framework.GameComponent
{
public Vector3 CameraInitPosition;
public Vector3 CameraPosition;
public Vector3 CameraInitLookAt;
public Vector3 CameraLookAt;
public Matrix ViewMatrix;
public Matrix ProjectionMatrix;
Matrix CameraRotation;
public float OffSetZ;
public float OffSetY;
public float OffSetX;
private Vector3 CameraUpVector;
private float Near;
private int Far;
private int AspectRatio;
public Vector3 Dimension;
public Matrix World;
private int RotationX;
private int RotationY;
private int RotationZ;
public Camera(Game game)
: base(game)
{
CameraInitPosition = CameraPosition = new Vector3(0, 200, 0.0001f);
CameraInitLookAt = CameraLookAt = new Vector3(0, -10, 0);
ViewMatrix = Matrix.Identity;
ProjectionMatrix = Matrix.Identity;
CameraRotation = Matrix.Identity;
OffSetX = 0;
OffSetY = 0;
OffSetZ = 0;
RotationX = 0;
RotationY = 0;
RotationZ = 0;
CameraUpVector = Vector3.Up;
Near = 1.0F;
Far = 2000;
/*
* Dimension of the field view by the camera.
* The camera must not move outside the field
*/
Dimension = Vector3.Zero;
}
/// <summary>
/// Allows the game component to perform any initialization it needs to before starting
/// to run. This is where it can query for any required services and load content.
/// </summary>
public override void Initialize()
{
// TODO: Add your initialization code here
AspectRatio =
GLOBALS_GAME_RESSOURCES.VP_viewGame.Width /
GLOBALS_GAME_RESSOURCES.VP_viewGame.Height;
base.Initialize();
}
/// <summary>
/// Allows the game component to update itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
public override void Update(GameTime gameTime)
{
//Recuperation du clavier
KeyboardState kstate = Keyboard.GetState();
if (kstate.IsKeyDown(Keys.Left) && !kstate.IsKeyDown(Keys.LeftAlt))
{
OffSetX -= 10;
if (OffSetX <= 0)
OffSetX = 0;
}
if (kstate.IsKeyDown(Keys.Right) && !kstate.IsKeyDown(Keys.LeftAlt))
{
OffSetX += 10;
if (OffSetX >= Dimension.X)
OffSetX = Dimension.X;
}
if (kstate.IsKeyDown(Keys.Down) && !kstate.IsKeyDown(Keys.LeftControl))
{
OffSetZ -= 10;
if (OffSetZ <= 0)
OffSetZ = 0;
}
if (kstate.IsKeyDown(Keys.Up) && !kstate.IsKeyDown(Keys.LeftControl))
{
OffSetZ += 10;
if (OffSetZ >= Dimension.Y)
OffSetZ = Dimension.Y;
}
if (kstate.IsKeyDown(Keys.Up) && kstate.IsKeyDown(Keys.LeftControl))
{
OffSetY += 10;
if (OffSetY > 1800)
OffSetY = 1800;
}
if (kstate.IsKeyDown(Keys.Down) && kstate.IsKeyDown(Keys.LeftControl))
{
OffSetY -= 10;
if (OffSetY <= Dimension.Z)
OffSetY = Dimension.Z;
}
if (kstate.IsKeyDown(Keys.Down) && kstate.IsKeyDown(Keys.LeftAlt))
RotationX += 2;
if (kstate.IsKeyDown(Keys.Up) && kstate.IsKeyDown(Keys.LeftAlt))
RotationX -= 2;
///////////////////////////////////////////////////////////////////
//Calcule des matrices
CameraRotation = Matrix.CreateRotationX(MathHelper.ToRadians(RotationX));
CameraPosition = Vector3.Transform(CameraInitPosition, CameraRotation);
CameraPosition += new Vector3(OffSetX, OffSetY, OffSetZ);
CameraLookAt = Vector3.Transform(CameraInitLookAt, CameraRotation);
CameraLookAt += new Vector3(OffSetX, 0, OffSetZ);
CameraUpVector = Vector3.Transform(CameraUpVector, CameraRotation);
ViewMatrix = Matrix.CreateLookAt(CameraPosition, CameraLookAt, CameraUpVector);
ProjectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, AspectRatio, Near, Far);
base.Update(gameTime);
}
}
} |
Partager