  MULTIMEDIA

# DirectX 10 Game Programming : 3D Primer - Matrix Rotation, Multiply Matrices

1/12/2013 3:28:41 AM
##### ### 1. Matrix Rotation The final effect I’m going to describe is rotation. Rotation is the act of turning an object around a certain axis. This allows objects to change their orientation within space. For instance, if you wanted to rotate a planet, you would create a rotation matrix and apply it to the planet. Rotation matrices take a little more explanation because the matrix needs to change based on the axis you’re trying to rotate around. #### Rotate around the X Axis When rotating around any axis, it will require you to convert the angle of rotation into both a sine and cosine value. These values are then plugged into the matrix to cause the proper amount of rotation. When rotating around the X axis, the sine and cosine values are placed into the matrix in the following manner. ```float RotateXMatrix  = { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, cosAngle, sinAngle, 0.0f, 0.0f, -sinAngle, cosAngle, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f };``` The Direct3D function D3DXMatrixRotationX will create a rotation matrix along the X axis. ```D3DXMATRIX * D3DXMatrixRotationX( D3DXMATRIX * pOut, FLOAT Angle );``` #### Rotate around the Y Axis Rotating around the Y axis (see Figure 1) requires only that the sine and cosine values be moved to different positions within the matrix. In this instance, the values are moved to affect the X and Z axes. Figure 1. A cube rotated 45 degrees around the Y axis. ```float RotateYMatrix  = { cosAngle, 0.0f, -sinAngle, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, sinAngle, 0.0f, cosAngle, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f };``` The D3DXMatrixRotationY function will generate a rotation matrix around the Y axis. ```D3DXMATRIX * D3DXMatrixRotationY( D3DXMATRIX * pOut, FLOAT Angle );``` #### Rotate around the Z Axis The final rotation matrix allows for rotation around the Z axis. In this instance, the sin and cosine values affect the X and Y axes. ```float RotateZMatrix  = { cosAngle, sinAngle, 0.0f, 0.0f, -sinAngle, cosAngle, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f };``` The D3DXMatrixRotationZ function will generate a rotation matrix around the Z axis. ```D3DXMATRIX * D3DXMatrixRotationZ( D3DXMATRIX * pOut, FLOAT Angle );```### 2. Multiply Matrices The last thing you need to learn about matrices is how to combine them. Most of the time, you won’t need to only translate an object, you’ll need to scale it or maybe rotate it all in one go. Multiple matrices can be multiplied, or concatenated, together to create a single matrix that is capable of containing the previously single calculations. This means that instead of needing a matrix for rotation as well as one for translation, these two can be combined into one. Because the D3DXMatrix type overrides the multiplication operator, the act of multiplying two D3DXMatrix objects together is easy. The following single line of code demonstrates this. `D3DXMatrix finalMatrix = rotationMatrix * translationMatrix;` A new matrix called finalMatrix is created, which now contains both the rotation and translation. This new matrix can then be applied to any object going through the pipeline. One thing to mention though is to watch out for the order in which you multiply matrices. Using the previous line of code, objects will be rotated first and then translated away from that position. For example, rotating the object 90 degrees around the Y axis and then translating it four units along the X axis would cause the object to be rotated in place at the origin and then moved four units to the right. If the translation came first, the object would be translated four units to the right of the origin first and then rotated 90 degrees. Think what would happen if you extended your hand to your right and then turned your body 90 degrees—where would your hand end up? Not exactly in the same place as before; you need to make sure that the order in which you multiply the matrices is the order you really want them applied. If you don’t like using the multiplication operator to combine matrices, you can use the Direct3D function D3DXMatrixMultiply instead. ```D3DXMATRIX * D3DXMatrixMultiply( D3DXMATRIX * pOut, CONST D3DXMATRIX *pM1, CONST D3DXMATRIX *pM2 );``` The D3DXMatrixMultiply function takes three parameters. The first parameter is a pointer to the output matrix to be created. The second and third parameters are the matrices you want to multiply. Using this function may make your code slightly easier to follow. Note Matrix multiplication is not cumulative. Multiplying Matrix A by Matrix B does not result in the same output matrix as multiplying Matrix B by Matrix A. The order in which matrices are multiplied is important.
 Other Most View -  Visual Studio 2010 IDE : Managing User Settings
 -  Asus P8Z77-V LK Mainboard - One Of The Simplest Designs From Asus (Part 2)
 -  Fujifilm X-E1 - A Retro Camera That Inspires (Part 9)
 -  How To Create A Lively Video Clip
 -  Secret Tips For Google Maps (Part 1)
 -  Thin. Thinner. Thinnest. Skinny One-Box PCS (Part 3)
 -  Xigamtek Midgard II - An Affordable Chassis
 -  Windows Server 2008 and Windows Vista : Item-Level Targeting (part 2) - Date Match, Dial-Up Connection,Disk Space, Domain
 -  Windows Phone 8 : Microsoft Office Mobile - Accessing Microsoft Office (part 3) - Adding a SharePoint Folder , Searching for a Document  Top 10 -  Sharepoint 2013 : Building a BCS-enabled Business Solution : Building an Integrated BCS Solution with an App for SharePoint Containing an App for Office
 -  Business Connectivity Services in Apps for SharePoint 2013 : Building an App-level BCS Solution for Office 365 SharePoint Online
 -  Business Connectivity Services in SharePoint 2013 : Adding a Business Data Connectivity Model to Office 365 SharePoint Online
 -  Remote Event Receivers in Sharepoint 2013 : Introducing Remote Event Receivers
 -  Windows Server 2008 and Windows Vista : Common GPO Troubleshooting Tools (part 3) - GPResult, GPOTool
 -  Windows Server 2008 and Windows Vista : Common GPO Troubleshooting Tools (part 2) - GPMC
 -  Windows Server 2008 and Windows Vista : Common GPO Troubleshooting Tools (part 1) - GPLogView
 -  Windows Server 2008 and Windows Vista : Using Event Logging for Troubleshooting (part 4) - Summary of Group Policy Event IDs
 -  Windows Server 2008 and Windows Vista : Using Event Logging for Troubleshooting (part 3) - Divide the Custom View of the Log into Three Phases
 -  Windows Server 2008 and Windows Vista : Using Event Logging for Troubleshooting (part 2)   