The document discusses 3D matrix transformations in XNA game development. It explains that to render a 3D scene, a camera matrix, projection matrix, and separate world matrix for each object must be set up. It provides details on creating view matrices with CreateLookAt, projection matrices with CreatePerspectiveFieldOfView or CreateOrthographic, and transforming objects using world matrices created from scaling, rotation, translation and other transformations. Multiple transformations can be combined by multiplying the matrices together in order.
6. XNA Matrices
• Matrix.CreateRotationX, Matrix.CreateRotationY, and Matrix.CreateRotationZ: Each of these creates a rotation matrix for each of the axes.
• Matrix.Translation: Creates a translation matrix (one or more axes).
• Matrix.Scale: Creates a scale matrix (one or more axes).
• Matrix.CreateLookAt: Creates a view matrix used to position the camera, by setting the 3D position of the camera, the 3D position it is facing, and which direction is “up” for the camera.
• Matrix.CreatePerspectiveFieldOfView: Creates a projection matrix that uses a perspective view.
11. RULE
TO SEE A 3D SCENE YOU SHOULD SET UP:
CAMERA
12. RULE
TO SEE A 3D SCENE YOU SHOULD SET UP:
CAMERA
PROJECTION
13. RULE
TO SEE A 3D SCENE YOU SHOULD SET UP:
CAMERA
PROJECTION
WORLD MATRIX
14. RULE
TO SEE A 3D SCENE YOU SHOULD SET UP:
CAMERA (Singleton, for all objects)
PROJECTION (Singleton, for all objects)
WORLD MATRIX (For each object separately)
15. RULE
TO SEE A 3D SCENE YOU SHOULD SET UP:
CAMERA (Singleton, for all objects)
PROJECTION (Singleton, for all objects)
WORLD MATRIX (For each object separately)
16. RULE
TO SEE A 3D SCENE YOU SHOULD SET UP:
CAMERA (Singleton, for all objects)
PROJECTION (Singleton, for all objects)
WORLD MATRIX (For each object separately)
17. RULE
TO SEE A 3D SCENE YOU SHOULD SET UP:
CAMERA (Singleton, for all objects)
PROJECTION (Singleton, for all objects)
WORLD MATRIX (For each object separately)
18. RULE
TO SEE A 3D SCENE YOU SHOULD SET UP:
CAMERA (Singleton, for all objects)
PROJECTION (Singleton, for all objects)
WORLD MATRIX (For each object separately)
20. XNA Matrices
• Matrix.CreateRotationX, Matrix.CreateRotationY, and Matrix.CreateRotationZ: Each of these creates a rotation matrix for each of the axes.
• Matrix.Translation: Creates a translation matrix (one or more axes).
• Matrix.Scale: Creates a scale matrix (one or more axes).
• Matrix.CreateLookAt: Creates a view matrix used to position the camera, by setting the 3D position of the camera, the 3D position it is facing, and which direction is “up” for the camera.
• Matrix.CreatePerspectiveFieldOfView: Creates a projection matrix that uses a perspective view.
24. RULE
TO SEE A 3D SCENE YOU SHOULD SET UP:
CAMERA (Singleton, for all objects)
PROJECTION (Singleton, for all objects)
WORLD MATRIX (For each object separately)
25. RULE
TO SEE A 3D SCENE YOU SHOULD SET UP:
CAMERA (Singleton, for all objects)
PROJECTION (Singleton, for all objects)
WORLD MATRIX (For each object separately)
27. XNA Matrices
• Matrix.CreateRotationX, Matrix.CreateRotationY, and Matrix.CreateRotationZ: Each of these creates a rotation matrix for each of the axes.
• Matrix.Translation: Creates a translation matrix (one or more axes).
• Matrix.Scale: Creates a scale matrix (one or more axes).
• Matrix.CreateLookAt: Creates a view matrix used to position the camera, by setting the 3D position of the camera, the 3D position it is facing, and which direction is “up” for the camera.
• Matrix.CreatePerspectiveFieldOfView: Creates a projection matrix that uses a perspective view.
41. XNA Matrices
• Matrix.CreateRotationX, Matrix.CreateRotationY, and Matrix.CreateRotationZ: Each of these creates a rotation matrix for each of the axes.
• Matrix.Translation: Creates a translation matrix (one or more axes).
• Matrix.Scale: Creates a scale matrix (one or more axes).
• Matrix.CreateLookAt: Creates a view matrix used to position the camera, by setting the 3D position of the camera, the 3D position it is facing, and which direction is “up” for the camera.
• Matrix.CreatePerspectiveFieldOfView: Creates a projection matrix that uses a perspective view.
42. Orthographic Projections
•An orthographic projection can be created with the following code:
Matrix.CreateOrthographic(float width, float height, float zNearPlane, float zFarPlane);
43. Orthographic Projections
•An orthographic projection can be created with the following code:
•Off-center orthogonal projection:
Matrix.CreateOrthographic(float width, float height, float zNearPlane, float zFarPlane);
Matrix.CreateOrthographicOffCenter(float left,
float right,
float bottom,
float top,
float zNearPlane, float zFarPlane);
57. RULE
TO SEE A 3D SCENE YOU SHOULD SET UP:
CAMERA (Singleton, for all objects)
PROJECTION (Singleton, for all objects)
WORLD MATRIX (For each object separately)
58. RULE
TO SEE A 3D SCENE YOU SHOULD SET UP:
CAMERA (Singleton, for all objects)
PROJECTION (Singleton, for all objects)
WORLD MATRIX (For each object separately)
61. World Matrix
•Example
•Let’s assume that the coordinates of the triangle vertices are as follows:
62. World Matrix
•Example
•To translate 40 units over the y axis’s positive direction,allyou need to do is to add 40 toeachy position, and you have the new coordinates for the vertices:
68. Transformations
•Create a matrix that rotates around the x-axis:
Matrix.CreateRotateX(float angleInRadians);
•Create a matrix that rotatesaround the y-axis:
Matrix.CreateRotateY(float angleInRadians);
•Create a matrix that rotatesaround the z-axis:
Matrix.CreateRotateZ(float angleInRadians);
Identity
Scale
Rotate
Orbit
Translate
69. Transformations
•Create a matrix that rotates points around an arbitrary axis:
Matrix.CreateFromAxisAngle(Vector3 axis, float angleInRadians);
Identity
Scale
Rotate
Orbit
Translate
90. Basic Matrices -A Final Example
Vector3cameraPosition= newVector3(30.0f, 30.0f, 30.0f);
Vector3cameraTarget= newVector3(0.0f, 0.0f, 0.0f); // Look back at the origin
floatfovAngle= MathHelper.ToRadians(45); // convert 45 degrees to radians
floataspectRatio= graphics.PreferredBackBufferWidth/ graphics.PreferredBackBufferHeight;
floatnear = 0.01f; // the near clipping plane distance
floatfar = 100f; // the far clipping plane distance
Matrixworld = Matrix.CreateTranslation(10.0f, 0.0f, 10.0f);
Matrixview = Matrix.CreateLookAt(cameraPosition, cameraTarget, Vector3.Up);
Matrixprojection = Matrix.CreatePerspectiveFieldOfView(fovAngle, aspectRatio, near, far);
91. Basic Matrices -A Final Example
Vector3cameraPosition= newVector3(30.0f, 30.0f, 30.0f);
Vector3cameraTarget= newVector3(0.0f, 0.0f, 0.0f); // Look back at the origin
float fovAngle= MathHelper.ToRadians(45); // convert 45 degrees to radians
float aspectRatio= graphics.PreferredBackBufferWidth/ graphics.PreferredBackBufferHeight;
float near = 0.01f; // the near clipping plane distance
float far = 100f; // the far clipping plane distance
Matrix world = Matrix.CreateTranslation(10.0f, 0.0f, 10.0f);
Matrixview = Matrix.CreateLookAt(cameraPosition, cameraTarget, Vector3.Up);
Matrix projection = Matrix.CreatePerspectiveFieldOfView(fovAngle, aspectRatio, near, far);
92. Basic Matrices -A Final Example
Vector3 cameraPosition= new Vector3(30.0f, 30.0f, 30.0f);
Vector3 cameraTarget= new Vector3(0.0f, 0.0f, 0.0f); // Look back at the origin
floatfovAngle= MathHelper.ToRadians(45); // convert 45 degrees to radians
floataspectRatio= graphics.PreferredBackBufferWidth/ graphics.PreferredBackBufferHeight;
floatnear = 0.01f; // the near clipping plane distance
floatfar = 100f; // the far clipping plane distance
Matrix world = Matrix.CreateTranslation(10.0f, 0.0f, 10.0f);
Matrix view = Matrix.CreateLookAt(cameraPosition, cameraTarget, Vector3.Up);
Matrixprojection = Matrix.CreatePerspectiveFieldOfView(fovAngle, aspectRatio, near, far);
93. Basic Matrices -A Final Example
Vector3 cameraPosition= new Vector3(30.0f, 30.0f, 30.0f);
Vector3 cameraTarget= new Vector3(0.0f, 0.0f, 0.0f); // Look back at the origin
float fovAngle= MathHelper.ToRadians(45); // convert 45 degrees to radians
float aspectRatio= graphics.PreferredBackBufferWidth/ graphics.PreferredBackBufferHeight;
float near = 0.01f; // the near clipping plane distance
float far = 100f; // the far clipping plane distance
Matrix world = Matrix.CreateTranslation(10.0f, 0.0f, 10.0f);
Matrix view = Matrix.CreateLookAt(cameraPosition, cameraTarget, Vector3.Up);
Matrix projection = Matrix.CreatePerspectiveFieldOfView(fovAngle, aspectRatio, near, far);
94. Basic Matrices -A Final Example
Vector3 cameraPosition= new Vector3(30.0f, 30.0f, 30.0f);
Vector3 cameraTarget= new Vector3(0.0f, 0.0f, 0.0f); // Look back at the origin
float fovAngle= MathHelper.ToRadians(45); // convert 45 degrees to radians
float aspectRatio= graphics.PreferredBackBufferWidth/ graphics.PreferredBackBufferHeight;
float near = 0.01f; // the near clipping plane distance
float far = 100f; // the far clipping plane distance
Matrix world = Matrix.CreateTranslation(10.0f, 0.0f, 10.0f);
Matrix view = Matrix.CreateLookAt(cameraPosition, cameraTarget, Vector3.Up);
Matrix projection = Matrix.CreatePerspectiveFieldOfView(fovAngle, aspectRatio, near, far);
95. Basic Matrices -A Final Example
Vector3 cameraPosition= new Vector3(30.0f, 30.0f, 30.0f);
Vector3 cameraTarget= new Vector3(0.0f, 0.0f, 0.0f); // Look back at the origin
float fovAngle= MathHelper.ToRadians(45); // convert 45 degrees to radians
float aspectRatio= graphics.PreferredBackBufferWidth/ graphics.PreferredBackBufferHeight;
float near = 0.01f; // the near clipping plane distance
float far = 100f; // the far clipping plane distance
Matrix world = Matrix.CreateTranslation(10.0f, 0.0f, 10.0f);
Matrix view = Matrix.CreateLookAt(cameraPosition, cameraTarget, Vector3.Up);
Matrix projection = Matrix.CreatePerspectiveFieldOfView(fovAngle, aspectRatio, near, far);