KEMBAR78
XNA L02–Basic Matrices and Transformations | PDF
Mohammad Shaker 
mohammadshaker.com 
@ZGTRShaker 
2011, 2012, 2013, 2014 
XNA Game Development 
L02 –Transformations and Basic Matrices
3D World
3D World
3D World
XNA Matrices
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.
Basic Matrices View, Projection and World Matrices
Basic Matrices 
•View, Projection and World Matrices
RULE
RULE 
TO SEE A 3D SCENE YOU SHOULD SET UP:
RULE 
TO SEE A 3D SCENE YOU SHOULD SET UP: 
CAMERA
RULE 
TO SEE A 3D SCENE YOU SHOULD SET UP: 
CAMERA 
PROJECTION
RULE 
TO SEE A 3D SCENE YOU SHOULD SET UP: 
CAMERA 
PROJECTION 
WORLD MATRIX
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)
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)
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)
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)
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)
Camera (View) Matrix
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.
View Matrices 
Matrix.CreateLookAt(Vector3 cameraPosition, Vector3 cameraTarget, Vector3 cameraUpVector);
View Matrices 
Matrix.CreateLookAt(Vector3 cameraPosition, Vector3 cameraTarget, Vector3 cameraUpVector);
View Matrices 
Matrix.CreateLookAt(Vector3 cameraPosition, Vector3 cameraTarget, Vector3 cameraUpVector);
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)
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)
Projection Matrix
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.
Projection Matrices
Projection Matrices 
The Difference?!
Projection Matrices 
The Difference?! 
perspective
Projection Matrices 
The Difference?! 
perspective 
orthographic
Projection Matrices
Projection Matrices
Projection Matrices
Projection Matrices 
Realistic? 
perspective 
orthographic
Projection Matrices 
•Projection Matries 
Realistic? 
perspective 
orthographic
Projection Matrices 
Architecture? 
perspective 
orthographic
Projection Matrices 
Architecture? 
perspective 
orthographic
Projection Matrices 
Usability? 
perspective 
orthographic
Projection Matrices 
Usability? 
perspective 
orthographic
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.
Orthographic Projections 
•An orthographic projection can be created with the following code: 
Matrix.CreateOrthographic(float width, float height, float zNearPlane, float zFarPlane);
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);
Orthographic Projections 
•Ummm …. 
Matrix.CreateOrthographicOffCenter(-1, 1, -1, 1, 0.1f, 100f); Matrix.CreateOrthographic(2, 2, 0.1f, 100f);
Orthographic Projections 
•Ummm …. 
Matrix.CreateOrthographicOffCenter(-1, 1, -1, 1, 0.1f, 100f); Matrix.CreateOrthographic(2, 2, 0.1f, 100f);
Perspective Projections 
•PerspectiveFieldOfView 
Matrix.CreatePerspectiveFieldOfView(float fieldOfView, 
float aspectRatio, 
float nearPlaneDistance, 
float farPlaneDistance);
Perspective Projections 
Matrix.CreatePerspectiveFieldOfView(float fieldOfView, 
float aspectRatio, 
float nearPlaneDistance, 
float farPlaneDistance);
Perspective Projections 
Matrix.CreatePerspectiveFieldOfView(float fieldOfView, 
float aspectRatio, 
float nearPlaneDistance, 
float farPlaneDistance);
Perspective Projections 
Matrix.CreatePerspectiveFieldOfView(float fieldOfView, 
float aspectRatio, 
float nearPlaneDistance, 
float farPlaneDistance);
Perspective Projections 
•Perspective projections 
Matrix.CreatePerspectiveFieldOfView(float fieldOfView, 
float aspectRatio, 
float nearPlaneDistance, 
float farPlaneDistance);
Perspective Projections 
Matrix.CreatePerspectiveFieldOfView(float fieldOfView, 
float aspectRatio, 
float nearPlaneDistance, 
float farPlaneDistance);
Perspective Projections 
Matrix.CreatePerspectiveFieldOfView(float fieldOfView, 
float aspectRatio, 
float nearPlaneDistance, 
float farPlaneDistance);
Perspective Projections 
Matrix.CreatePerspective( 
float width, 
float height, 
float nearPlaneDistance, float farPlaneDistance);
Perspective Projections 
Matrix.CreatePerspective( 
float width, 
float height, 
float nearPlaneDistance, float farPlaneDistance);
Perspective Projections 
Matrix.CreatePerspective( 
float width, 
float height, 
float nearPlaneDistance, float farPlaneDistance);
Perspective Projections 
Matrix.CreatePerspectiveOffCenter( 
float left, 
float right, 
float bottom, 
float top, 
float nearPlaneDistance, float farPlaneDistance);
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)
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)
World MatrixEach Object in 3D Has Its own World Matrix
World Matrix
World Matrix 
•Example 
•Let’s assume that the coordinates of the triangle vertices are as follows:
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:
World Matrix
World Matrix
Identity 
Scale 
Rotate 
Orbit 
Translate
Transformations 
•Load Identity Matrix 
Matrix.Identity(); 
Identity 
Scale 
Rotate 
Orbit 
Translate
Transformations 
•Create scale matrix 
Matrix.CreateScale(float scaleAmount); 
Identity 
Scale 
Rotate 
Orbit 
Translate
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
Transformations 
•Create a matrix that rotates points around an arbitrary axis: 
Matrix.CreateFromAxisAngle(Vector3 axis, float angleInRadians); 
Identity 
Scale 
Rotate 
Orbit 
Translate
Transformations 
•Create an Orbit matrix?! 
Identity 
Scale 
Rotate 
Orbit 
Translate
Transformations 
•Create an Orbit matrix?! 
It’s just 
“Translate then Rotate” 
Identity 
Scale 
Rotate 
Orbit 
Translate
Transformations 
•Create a translation matrix: 
Matrix.CreateTranslation(Vector3 position); 
Identity 
Scale 
Rotate 
Orbit 
Translate
Transformations 
•Multiple World Transformations
Transformations 
•Multiple World Transformations
Transformations 
•Multiple World Transformations 
Matrixresult = 
Matrix.CreateRotationX(MathHelper.ToRadians(45)) * 
Matrix.CreateTranslation(newVector3(10, 0, 0)); 
Identity 
Scale 
Rotate 
Orbit 
Translate
Transformations 
•Multiple World Transformations 
Matrixresult = 
Matrix.CreateRotationX(MathHelper.ToRadians(45)) * 
Matrix.CreateTranslation(newVector3(10, 0, 0)); 
Identity 
Scale 
Rotate 
Orbit 
Translate
Transformations 
•Multiple World Transformations 
Matrixresult = 
Matrix.CreateRotationX(MathHelper.ToRadians(45)) * 
Matrix.CreateTranslation(newVector3(10, 0, 0)); 
Identity 
Scale 
Rotate 
Orbit 
Translate
Transformations 
•Multiple World Transformations 
Matrixresult = 
Matrix.CreateRotationX(MathHelper.ToRadians(45)) * 
Matrix.CreateTranslation(newVector3(10, 0, 0)); 
Identity 
Scale 
Rotate 
Orbit 
Translate
Transformations 
•Multiple World Transformations 
Matrixresult = 
Matrix.CreateRotationX(MathHelper.ToRadians(45)) * 
Matrix.CreateTranslation(newVector3(10, 0, 0)); 
Matrixresult = 
Matrix.CreateTranslation(newVector3(10, 0, 0)) * 
Matrix.CreateRotationX(MathHelper.ToRadians(45)); 
Identity 
Scale 
Rotate 
Orbit 
Translate
Transformations 
•Multiple World Transformations 
Matrixresult = 
Matrix.CreateRotationX(MathHelper.ToRadians(45)) * 
Matrix.CreateTranslation(newVector3(10, 0, 0)); 
Matrixresult = 
Matrix.CreateTranslation(newVector3(10, 0, 0)) * 
Matrix.CreateRotationX(MathHelper.ToRadians(45)); 
Identity 
Scale 
Rotate 
Orbit 
Translate
Transformations 
•Multiple World Transformations 
Matrixresult = 
Matrix.CreateRotationX(MathHelper.ToRadians(45)) * 
Matrix.CreateTranslation(newVector3(10, 0, 0)); 
Matrixresult = 
Matrix.CreateTranslation(newVector3(10, 0, 0)) * 
Matrix.CreateRotationX(MathHelper.ToRadians(45)); 
Not the same! 
Identity 
Scale 
Rotate 
Orbit 
Translate
Transformations 
•Multiple World Transformations 
Matrixresult = 
Matrix.CreateRotationX(MathHelper.ToRadians(45)) * 
Matrix.CreateTranslation(newVector3(10, 0, 0)); 
Matrixresult = 
Matrix.CreateTranslation(newVector3(10, 0, 0)) * 
Matrix.CreateRotationX(MathHelper.ToRadians(45)); 
Not the same! 
Identity 
Scale 
Rotate 
Orbit 
Translate
Transformations 
•Multiple World Transformations 
Matrixresult = 
Matrix.CreateRotationX(MathHelper.ToRadians(45)) * 
Matrix.CreateTranslation(newVector3(10, 0, 0)); 
Matrixresult = 
Matrix.CreateTranslation(newVector3(10, 0, 0)) * 
Matrix.CreateRotationX(MathHelper.ToRadians(45)); 
Not the same! 
Identity 
Scale 
Rotate 
Orbit 
Translate
Transformations 
•Multiple World Transformations 
Matrixresult = 
Matrix.CreateTranslation(newVector3(10, 0, 0)) * 
Matrix.CreateRotationX(MathHelper.ToRadians(45)); 
Identity 
Scale 
Rotate 
Orbit 
Translate
Transformations 
•Multiple World Transformations 
Matrixresult = 
Matrix.CreateTranslation(newVector3(10, 0, 0)) * 
Matrix.CreateRotationX(MathHelper.ToRadians(45)); 
Identity 
Scale 
Rotate 
Orbit 
Translate
Test’emlive! 
Souvenir, 9:03 AM, Tuesday, Aug.16th-2011 still awake :S
Custom Matrices
Custom Matrices 
Matrix customMatrix= new Matrix( 
floatm11, floatm12, floatm13, floatm14, floatm21, floatm22, floatm23, floatm24, floatm31, floatm32, floatm33, floatm34, floatm41, floatm42, floatm43, floatm44);
Custom Matrices 
•Custom Matrices 
Matrix customMatrix= new Matrix( 
floatm11, floatm12, floatm13, floatm14, floatm21, floatm22, floatm23, floatm24, floatm31, floatm32, floatm33, floatm34, floatm41, floatm42, floatm43, floatm44); 
customMatrix.M31 = 4;
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);
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);
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);
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);
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);
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);

XNA L02–Basic Matrices and Transformations

  • 1.
    Mohammad Shaker mohammadshaker.com @ZGTRShaker 2011, 2012, 2013, 2014 XNA Game Development L02 –Transformations and Basic Matrices
  • 2.
  • 3.
  • 4.
  • 5.
  • 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.
  • 7.
    Basic Matrices View,Projection and World Matrices
  • 8.
    Basic Matrices •View,Projection and World Matrices
  • 9.
  • 10.
    RULE TO SEEA 3D SCENE YOU SHOULD SET UP:
  • 11.
    RULE TO SEEA 3D SCENE YOU SHOULD SET UP: CAMERA
  • 12.
    RULE TO SEEA 3D SCENE YOU SHOULD SET UP: CAMERA PROJECTION
  • 13.
    RULE TO SEEA 3D SCENE YOU SHOULD SET UP: CAMERA PROJECTION WORLD MATRIX
  • 14.
    RULE TO SEEA 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 SEEA 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 SEEA 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 SEEA 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 SEEA 3D SCENE YOU SHOULD SET UP: CAMERA (Singleton, for all objects) PROJECTION (Singleton, for all objects) WORLD MATRIX (For each object separately)
  • 19.
  • 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.
  • 21.
    View Matrices Matrix.CreateLookAt(Vector3cameraPosition, Vector3 cameraTarget, Vector3 cameraUpVector);
  • 22.
    View Matrices Matrix.CreateLookAt(Vector3cameraPosition, Vector3 cameraTarget, Vector3 cameraUpVector);
  • 23.
    View Matrices Matrix.CreateLookAt(Vector3cameraPosition, Vector3 cameraTarget, Vector3 cameraUpVector);
  • 24.
    RULE TO SEEA 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 SEEA 3D SCENE YOU SHOULD SET UP: CAMERA (Singleton, for all objects) PROJECTION (Singleton, for all objects) WORLD MATRIX (For each object separately)
  • 26.
  • 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.
  • 28.
  • 29.
  • 30.
    Projection Matrices TheDifference?! perspective
  • 31.
    Projection Matrices TheDifference?! perspective orthographic
  • 32.
  • 33.
  • 34.
  • 35.
    Projection Matrices Realistic? perspective orthographic
  • 36.
    Projection Matrices •ProjectionMatries Realistic? perspective orthographic
  • 37.
    Projection Matrices Architecture? perspective orthographic
  • 38.
    Projection Matrices Architecture? perspective orthographic
  • 39.
    Projection Matrices Usability? perspective orthographic
  • 40.
    Projection Matrices Usability? perspective orthographic
  • 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 •Anorthographic projection can be created with the following code: Matrix.CreateOrthographic(float width, float height, float zNearPlane, float zFarPlane);
  • 43.
    Orthographic Projections •Anorthographic 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);
  • 44.
    Orthographic Projections •Ummm…. Matrix.CreateOrthographicOffCenter(-1, 1, -1, 1, 0.1f, 100f); Matrix.CreateOrthographic(2, 2, 0.1f, 100f);
  • 45.
    Orthographic Projections •Ummm…. Matrix.CreateOrthographicOffCenter(-1, 1, -1, 1, 0.1f, 100f); Matrix.CreateOrthographic(2, 2, 0.1f, 100f);
  • 46.
    Perspective Projections •PerspectiveFieldOfView Matrix.CreatePerspectiveFieldOfView(float fieldOfView, float aspectRatio, float nearPlaneDistance, float farPlaneDistance);
  • 47.
    Perspective Projections Matrix.CreatePerspectiveFieldOfView(floatfieldOfView, float aspectRatio, float nearPlaneDistance, float farPlaneDistance);
  • 48.
    Perspective Projections Matrix.CreatePerspectiveFieldOfView(floatfieldOfView, float aspectRatio, float nearPlaneDistance, float farPlaneDistance);
  • 49.
    Perspective Projections Matrix.CreatePerspectiveFieldOfView(floatfieldOfView, float aspectRatio, float nearPlaneDistance, float farPlaneDistance);
  • 50.
    Perspective Projections •Perspectiveprojections Matrix.CreatePerspectiveFieldOfView(float fieldOfView, float aspectRatio, float nearPlaneDistance, float farPlaneDistance);
  • 51.
    Perspective Projections Matrix.CreatePerspectiveFieldOfView(floatfieldOfView, float aspectRatio, float nearPlaneDistance, float farPlaneDistance);
  • 52.
    Perspective Projections Matrix.CreatePerspectiveFieldOfView(floatfieldOfView, float aspectRatio, float nearPlaneDistance, float farPlaneDistance);
  • 53.
    Perspective Projections Matrix.CreatePerspective( float width, float height, float nearPlaneDistance, float farPlaneDistance);
  • 54.
    Perspective Projections Matrix.CreatePerspective( float width, float height, float nearPlaneDistance, float farPlaneDistance);
  • 55.
    Perspective Projections Matrix.CreatePerspective( float width, float height, float nearPlaneDistance, float farPlaneDistance);
  • 56.
    Perspective Projections Matrix.CreatePerspectiveOffCenter( float left, float right, float bottom, float top, float nearPlaneDistance, float farPlaneDistance);
  • 57.
    RULE TO SEEA 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 SEEA 3D SCENE YOU SHOULD SET UP: CAMERA (Singleton, for all objects) PROJECTION (Singleton, for all objects) WORLD MATRIX (For each object separately)
  • 59.
    World MatrixEach Objectin 3D Has Its own World Matrix
  • 60.
  • 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:
  • 63.
  • 64.
  • 65.
    Identity Scale Rotate Orbit Translate
  • 66.
    Transformations •Load IdentityMatrix Matrix.Identity(); Identity Scale Rotate Orbit Translate
  • 67.
    Transformations •Create scalematrix Matrix.CreateScale(float scaleAmount); Identity Scale Rotate Orbit Translate
  • 68.
    Transformations •Create amatrix 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 amatrix that rotates points around an arbitrary axis: Matrix.CreateFromAxisAngle(Vector3 axis, float angleInRadians); Identity Scale Rotate Orbit Translate
  • 70.
    Transformations •Create anOrbit matrix?! Identity Scale Rotate Orbit Translate
  • 71.
    Transformations •Create anOrbit matrix?! It’s just “Translate then Rotate” Identity Scale Rotate Orbit Translate
  • 72.
    Transformations •Create atranslation matrix: Matrix.CreateTranslation(Vector3 position); Identity Scale Rotate Orbit Translate
  • 73.
  • 74.
  • 75.
    Transformations •Multiple WorldTransformations Matrixresult = Matrix.CreateRotationX(MathHelper.ToRadians(45)) * Matrix.CreateTranslation(newVector3(10, 0, 0)); Identity Scale Rotate Orbit Translate
  • 76.
    Transformations •Multiple WorldTransformations Matrixresult = Matrix.CreateRotationX(MathHelper.ToRadians(45)) * Matrix.CreateTranslation(newVector3(10, 0, 0)); Identity Scale Rotate Orbit Translate
  • 77.
    Transformations •Multiple WorldTransformations Matrixresult = Matrix.CreateRotationX(MathHelper.ToRadians(45)) * Matrix.CreateTranslation(newVector3(10, 0, 0)); Identity Scale Rotate Orbit Translate
  • 78.
    Transformations •Multiple WorldTransformations Matrixresult = Matrix.CreateRotationX(MathHelper.ToRadians(45)) * Matrix.CreateTranslation(newVector3(10, 0, 0)); Identity Scale Rotate Orbit Translate
  • 79.
    Transformations •Multiple WorldTransformations Matrixresult = Matrix.CreateRotationX(MathHelper.ToRadians(45)) * Matrix.CreateTranslation(newVector3(10, 0, 0)); Matrixresult = Matrix.CreateTranslation(newVector3(10, 0, 0)) * Matrix.CreateRotationX(MathHelper.ToRadians(45)); Identity Scale Rotate Orbit Translate
  • 80.
    Transformations •Multiple WorldTransformations Matrixresult = Matrix.CreateRotationX(MathHelper.ToRadians(45)) * Matrix.CreateTranslation(newVector3(10, 0, 0)); Matrixresult = Matrix.CreateTranslation(newVector3(10, 0, 0)) * Matrix.CreateRotationX(MathHelper.ToRadians(45)); Identity Scale Rotate Orbit Translate
  • 81.
    Transformations •Multiple WorldTransformations Matrixresult = Matrix.CreateRotationX(MathHelper.ToRadians(45)) * Matrix.CreateTranslation(newVector3(10, 0, 0)); Matrixresult = Matrix.CreateTranslation(newVector3(10, 0, 0)) * Matrix.CreateRotationX(MathHelper.ToRadians(45)); Not the same! Identity Scale Rotate Orbit Translate
  • 82.
    Transformations •Multiple WorldTransformations Matrixresult = Matrix.CreateRotationX(MathHelper.ToRadians(45)) * Matrix.CreateTranslation(newVector3(10, 0, 0)); Matrixresult = Matrix.CreateTranslation(newVector3(10, 0, 0)) * Matrix.CreateRotationX(MathHelper.ToRadians(45)); Not the same! Identity Scale Rotate Orbit Translate
  • 83.
    Transformations •Multiple WorldTransformations Matrixresult = Matrix.CreateRotationX(MathHelper.ToRadians(45)) * Matrix.CreateTranslation(newVector3(10, 0, 0)); Matrixresult = Matrix.CreateTranslation(newVector3(10, 0, 0)) * Matrix.CreateRotationX(MathHelper.ToRadians(45)); Not the same! Identity Scale Rotate Orbit Translate
  • 84.
    Transformations •Multiple WorldTransformations Matrixresult = Matrix.CreateTranslation(newVector3(10, 0, 0)) * Matrix.CreateRotationX(MathHelper.ToRadians(45)); Identity Scale Rotate Orbit Translate
  • 85.
    Transformations •Multiple WorldTransformations Matrixresult = Matrix.CreateTranslation(newVector3(10, 0, 0)) * Matrix.CreateRotationX(MathHelper.ToRadians(45)); Identity Scale Rotate Orbit Translate
  • 86.
    Test’emlive! Souvenir, 9:03AM, Tuesday, Aug.16th-2011 still awake :S
  • 87.
  • 88.
    Custom Matrices MatrixcustomMatrix= new Matrix( floatm11, floatm12, floatm13, floatm14, floatm21, floatm22, floatm23, floatm24, floatm31, floatm32, floatm33, floatm34, floatm41, floatm42, floatm43, floatm44);
  • 89.
    Custom Matrices •CustomMatrices Matrix customMatrix= new Matrix( floatm11, floatm12, floatm13, floatm14, floatm21, floatm22, floatm23, floatm24, floatm31, floatm32, floatm33, floatm34, floatm41, floatm42, floatm43, floatm44); customMatrix.M31 = 4;
  • 90.
    Basic Matrices -AFinal 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 -AFinal 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 -AFinal 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 -AFinal 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 -AFinal 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 -AFinal 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);