To make the static square more interesting, let's take a quick look at how it rotates around the screen.
To achieve this, you must first track the rotation angle. Add a class-level float variable, name it as _ angle, and add 5 degrees to each update.
Protected override void Update (gametime)
{
// Allows the game to exit
If (gamepad. getstate (playerindex. One). Buttons. Back = buttonstate. Pressed)
This. Exit ();
_ Angle + = mathhelper. toradians (5 );
Base. Update (gametime );
}
We need to update the world matrix (all the details about the world matrix will be discussed in the "Understanding matrix transformation" Section) to apply this angle to the square. To rotate the square, provide a rotation matrix for it. The matrix class of xNa provides multiple methods for creating such a matrix. In this example, we select the createrotationzfunction function. This function accepts a single parameter (rotation angle) and returns a usable matrix.
The updated plot rotates the squareCode
Protected override void draw (gametime)
{
Graphicsdevice. Clear (color. cornflowerblue );
// Set the world matrix so that the square rotates
_ Effect. World = matrix. createrotationz (_ angle );
Foreach (effectpass pass in _ effect. currenttechnique. Passes)
{
// Apply the pass
Pass. Apply ();
// Draw the square
Graphicsdevice. drawuserprimitives (primitivetype. trianglestrip, _ vertices, 0, 2 );
}
Base. Draw (gametime );
}
Note that calling the drawuserprimitives function will not actually change the square at all; actually, the state of the object to be rotated is the effect rather than the Drawing Instruction. This is quite different from the sprite-based rendering method.