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

Additional info

Latest Forum posts

 Account settings
  Posted by: Anonymous
  When: 07/05/2014 at 09:48:39

 forced subtitle
  Posted by: Applefly
  When: 07/05/2014 at 06:00:48

 convert DVD into PMS
  Posted by: Applefly
  When: 07/05/2014 at 05:55:25

 DVD to Digital Copy easily
  Posted by: VIKIVannessa
  When: 05/05/2014 at 06:52:29

 DVD on Xbox 360/Xbox One Console
  Posted by: VIKIVannessa
  When: 05/05/2014 at 06:51:47

 Extract .Srt Subtitles
  Posted by: Applefly
  When: 04/05/2014 at 03:54:38

 Encode Movie collection
  Posted by: Applefly
  When: 04/05/2014 at 03:52:41

 Convert DVD to WMV
  Posted by: Applefly
  When: 29/04/2014 at 05:53:50

 rip DVDs into digital files
  Posted by: Applefly
  When: 29/04/2014 at 05:51:20

 iTunes movies/music to Kindle Fire
  Posted by: ciciyu80
  When: 29/04/2014 at 05:10:20


Rotations and translations

This chapter will make your triangle spin around. Since we are using world space coordinates, this is very easy. Let's first add a variable 'angle' to our class to store the current rotation angle. Just add this one to your variables.

 private float angle = 0f;

Now, increase this variable with 0.05f every time OnPaint is called. Add this to the end of the OnPaint class:

 angle += 0.05f;

With our angle increasing automatically, all we have to do is to rotate the world coordinates. I hope you remember from your maths class this is done using transformation matrices ;) Luckily, all you have to do is specify the rotation axis and the rotation angle. All the rest is done by Direct3D!

You should add the following line right before you actually draw the triangle, this is before the call to device.DrawUserPrimitives :

 device.Transform.World = Matrix.RotationZ(angle);

When you run the application, you will see that your triangle is spinning around its (0,0,0) point as expected! But imagine we would like to spin it through the center. One possibility is to redefine the point so the (0,0,0) would be in the center of our triangle. The better solution would be to first move (=translate) the triangle a bit to the left and down, and then rotate it. To do this, simply first multiply your World matrix with a translation matrix :

 device.Transform.World = Matrix.Translation(-5,-10*1/3,0)*Matrix.RotationZ(angle);

This will move the triangle so the (0,0,0) point is positioned in the gravity point of the triangle. Then our triangle is rotated around this point, giving us the desired result. Please note the order of transformations. Go ahead and place the translation AFTER the rotation. You will see a triangle rotation around one point, moved to the left and below. You can easily change the code to make the triangle rotate around the Y or Z axis. Make sure to try one of them, to get the first feeling of 3D. A bit more complex is the Matrix.RotateAxis, where you first specify your own custom rotation axis :

 device.Transform.World = Matrix.Translation(-5,-10*1/3,0)*Matrix.RotationAxis(new Vector3(angle*4,angle*2,angle*3), angle);

This will make our triangle spin around an every changing axe. Before starting next chapter, let's clear things up a bit. Put the camera positioning in a new method CameraPositioning() and the vertex declaration in VertexDeclaration(), as you can find in the code at the bottom of the page. Since both of them need to be declared only once, we'll only call them from our Main method, so the OnPaint method will go faster. Don't forget to make vertices a variable in your class by adjusting its definition and placing this line in the top of your class :

 private CustomVertex.PositionColored[] vertices;

DirectX Tutorial 5 - Rotation - Translation

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

Click here to go to the forum on this chapter!

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home1/riemers/public_html/eng/Tutorials/DirectX/Csharp/Series1/tut5.php(9) : eval()'d code(35) : eval()'d code on line 303

Here's the code:

 using System;
 using System.Drawing;
 using System.Collections;
 using System.ComponentModel;
 using System.Windows.Forms;
 using System.Data;
 using Microsoft.DirectX;
 using Microsoft.DirectX.Direct3D;
 namespace DirectX_Tutorial
     public class WinForm : System.Windows.Forms.Form
         private Device device;
         private System.ComponentModel.Container components = null;
         private float angle = 0f;
         private CustomVertex.PositionColored[] vertices;
         public WinForm()
             this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
         public void InitializeDevice()
             PresentParameters presentParams = new PresentParameters();
             presentParams.Windowed = true;
             presentParams.SwapEffect = SwapEffect.Discard;
             device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
         private void CameraPositioning()
             device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI/4, this.Width/this.Height, 1f, 50f);
             device.Transform.View = Matrix.LookAtLH(new Vector3(0,0,-30), new Vector3(0,0,0), new Vector3(0,1,0));
             device.RenderState.Lighting = false;
             device.RenderState.CullMode = Cull.None;
         private void VertexDeclaration()
             vertices = new CustomVertex.PositionColored[3];
             vertices[0].Position = new Vector3(0f, 0f, 0f);
             vertices[0].Color = Color.Red.ToArgb();
             vertices[1].Position = new Vector3(10f, 0f, 0f);
             vertices[1].Color = Color.Green.ToArgb();
             vertices[2].Position = new Vector3(5f, 10f, 0f);
             vertices[2].Color = Color.Yellow.ToArgb();
         protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
             device.Clear(ClearFlags.Target, Color.DarkSlateBlue , 1.0f, 0);
             device.VertexFormat = CustomVertex.PositionColored.Format;
             device.Transform.World = Matrix.Translation(-5,-10*1/3,0)*Matrix.RotationAxis(new Vector3(angle*4,angle*2,angle*3), angle);
             device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, vertices);
             angle += 0.05f;
         protected override void Dispose (bool disposing)
             if (disposing)
                 if (components != null)
         private void InitializeComponent()
             this.components = new System.ComponentModel.Container();
             this.Size = new System.Drawing.Size(500,500);
             this.Text = "DirectX Tutorial";
         static void Main()
             using (WinForm our_directx_form = new WinForm())


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

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

Microsoft MVP Award

2007 - 2011 MVP Award
DirectX - XNA


XNA 2.0 Recipes Book (8)
XNA 3.0 Recipes Book (8)
Extra Reading (3)
Matrices: geometrical
Matrix Mathematics
Homogenous matrices
Community Projects (1)
Tutorials (160)
XNA 4.0 using C# (89)
DirectX using C# (54)
Series 1:Terrain (14)
Opening a window
Linking to the Device
Drawing a triangle
Rotation - Translation
Terrain creation
Terrain from file
Importing bmp files
Colored vertices
DirectX Light basics
Mesh creation
Mesh lighting
Series 2: Flightsim (19)
Series 3: HLSL (19)
Short Tuts (2)
DirectX using C++ (15)
DirectX using VB (2)
-- 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!