XNA for C#
DirectX 9 for C#
DirectX 9 for C++
DirectX 9 for VB
Forum
   
My XNA Book
      
       Go to section on this site

Additional info


Latest Forum posts

 XNA 4.0 conversion
  Posted by: Anonymous
  When: 04/06/2013 at 09:50:05

 XNA Augmented Reality App
  Posted by: Anonymous
  When: 29/05/2013 at 21:20:58

 integer to float conversion
  Posted by: derensendil
  When: 27/05/2013 at 14:56:24

 Tutorial 3 for Windows Phone 7
  Posted by: Anonymous
  When: 20/05/2013 at 02:30:13

 No download link for 2d series: shooter
  Posted by: zaboleq
  When: 07/05/2013 at 15:46:28

 Collision Class?
  Posted by: Anonymous
  When: 05/05/2013 at 19:03:59

 stack overflow
  Posted by: cityguy
  When: 07/04/2013 at 01:58:38

 Meshes looks strange.
  Posted by: ab_saratov
  When: 01/04/2013 at 04:31:08

 Lamppost Not loaded
  Posted by: Anonymous
  When: 22/03/2013 at 06:43:52

 Collision Class?
  Posted by: Da_Boom
  When: 21/03/2013 at 01:23:09


Ads

The maths behind Matrices

The fact you’re reading this page means you already have a basic idea what matrices are used for by DirectX, but you want to go a little further. So let’s start by showing what a matrix actually looks like.

A matrix is nothing more than a table of numbers. Much like your basic Excel sheet; or a 2D array of floats, if you’re more into that. To process 3D coordinates, matrices of 3 columns and 3 rows are used, much like you can see here:



If you want, you can represent the 3 coordinates of a 3D points as a matrix of 1 column and 3 rows. Then multiplication between such a point and a matrix is defined like this:



Now you know everything that’s going on there! And as you can see, nothing difficult. Maybe it’s time for a small example, so you can see this in action. Imagine we have a point in 3D space with coordinates (6,18,9.5). Let’s start with a simple example: we want to enlarge our scene, so all objects will look twice as large. This means we’re going to scale by factor 2. In general, a scaling matrix simply looks like this:



Which gives



In the case of our small example, s = 2, thus the coordinates of our transformed point are (12, 36, 19).

Now we’ve seen the scaling matrix, let’s move on and have a look at the rotation matrices. These look a little more complex, but remember that all sin and cos are in fact a number between -1 and +1. These 3 matrices represent the rotation around the X,Y and Z axis respectively over an angle theta:



A small example would be in place I guess. Say we want to rotate the point (10,5,0) around the Z axis over 45 degrees. Our translation matrix would be:



Since pi=3.14 corresponds to 180 degrees, 45 degrees corresponds to pi/4.
This gives us:



So when you rotate the point (10,5,0) for 45 degrees, you get the point (10.6065, -3.5355, 0)! Luckily, DirectX takes care of all these maths for us, but now you have an idea of how this all works.

There is only one problem left. There is no such matrix you can multiply with a point to get the translated version of this point. To solve this, DirectX uses homogeneous matrices, which we’ll see in page 3.



Click here to go to the forum on this chapter!

Or click on one of the topics on this chapter to go there:
  • WPF to XNA matrix and rotation
          Hi there! Seems you are my last hope in this quest...
  • PCA and help
          First of all - Hello! Great idea with forum. Sorr...
  • PCA Example
          Maybe example of PCA (in code: VB or VB) could be ...
  • In the example rotation ...
          Hi Riemer, Great tutorials, but I have a questi...
  • Y rotation matrix?
          If I understand correctly, the signs are wrong for...
  • Theorie
          How you know which of the three matrices with cosi...





    Google
     
    Webwww.riemers.net


    If you appreciate the amount of time I spend creating and updating
    these pages, feel free to donate -- any amount is welcome !



    - Website design & XNA + DirectX code : Riemer Grootjans -
    ©2003 - 2011 Riemer Grootjans
  • Translations

    This site in English
    This site in Korean
    This site in Czech

    Microsoft MVP Award



    2007 - 2011 MVP Award
    DirectX - XNA

    Contents

    News
    Home
    Forum
    XNA 2.0 Recipes Book (8)
    XNA 3.0 Recipes Book (8)
    Downloads
    Extra Reading (3)
    Matrices: geometrical
    Matrix Mathematics
    Homogenous matrices
    Community Projects (1)
    Tutorials (160)
    -- Expand all --


    Thank you!

    Support this site --
    any amount is welcome !

    Stay up-to-date

    I don't have the time to keep a News section, so stay informed about the updates by clicking on this RSS file!