XNa development-mobile object

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.