5-shift scaling for 3D development in Windows Phone

Source: Internet
Author: User

In the previous section, we have the first 3D object. In this section, we first let it have the most basic movement of translation and scaling.

 

Looking back at our basiceffect, there is a world attribute used to determine the object coordinate system. If we apply some operations on this attribute, we can make the object move. Generally, the value of world is a matrix of units, that is, the object is located at the origin. To move an object, you only need to multiply it by a matrix on the corresponding coordinate axis. The matrix construction method is:

Matrix createtranslation (float xposition, float yposition, float zposition );

The three parameters represent the offset on the X, Y, and Z axes respectively.

 

Similarly, scaling changes multiply a scaling matrix by the matrix where the object is located. The method for constructing a scaling matrix is as follows:

Matrix createscale (float XScale, float yscale, float zscale );

Similarly, the three parameters represent the scaling ratio on the X, Y, and Z axes respectively. If you want to perform proportional Scaling with the same proportion on the three axes, you can use:

Matrix createscale (float scale );

 

Follow the xNa project we created in the previous lesson and open this project in vs2010. Open the game1.cs file and modify the game1 class. Add two member variables to it, representing the translation matrix and the scaling matrix respectively:

Matrix translatematrix = matrix. identity;

Matrix scalematrix = matrix. identity;

Then, modify the world attribute of the basiceffect object in the draw () method:

Basiceffect. World = scalematrix * translatematrix;

 

To increase the effect, we may wish to add a little interaction, that is, each click on the screen will make the object a little movement, so as to better observe the shape of the object motion. This interaction must be included in the update () method. You must adjust the value of the translation matrix and zoom matrix each time you click the screen (this section does not describe the gesture recognition method ).

 

Finally, we can try to modify the values of the translation matrix and the zoom matrix and multiply them to observe the final motion effect.

 

Complete source code for the game1 class is provided in this section:



public class Game1: Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        Camera camera;
        Matrix world = Matrix.Identity;
        BasicEffect basicEffect;
        VertexPositionColor [] triangle;
        Matrix translateMatrix = Matrix.Identity;
        Matrix scaleMatrix = Matrix.Identity;

        public Game1 ()
        {
            graphics = new GraphicsDeviceManager (this);
            Content.RootDirectory = "Content";

            // Frame rate is 30 fps by default for Windows Phone.
            TargetElapsedTime = TimeSpan.FromTicks (333333);

            // Extend battery life under lock.
            InactiveSleepTime = TimeSpan.FromSeconds (1);
            graphics.IsFullScreen = true;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content. Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </ summary>
        protected override void Initialize ()
        {
            // TODO: Add your initialization logic here

            base.Initialize ();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </ summary>
        protected override void LoadContent ()
        {
            camera = new Camera (this, new Vector3 (0, 0, 5), Vector3.Zero, Vector3.Up, MathHelper.PiOver4, GraphicsDevice.Viewport.AspectRatio, 1.0f, 50.0f);
            Components.Add (camera);
            basicEffect = new BasicEffect (GraphicsDevice);
            triangle = new VertexPositionColor [] {
                new VertexPositionColor (new Vector3 (0, 1, 0), Color.Red),
                new VertexPositionColor (new Vector3 (1, -1, 0), Color.Green),
                new VertexPositionColor (new Vector3 (-1, -1, 0), Color.Blue)
            };
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </ summary>
        protected override void UnloadContent ()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </ summary>
        /// <param name = "gameTime"> Provides a snapshot of timing values. </ param>
        protected override void Update (GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState (PlayerIndex.One) .Buttons.Back == ButtonState.Pressed)
                this.Exit ();

            TouchPanel.EnabledGestures = GestureType.Tap;
            if (TouchPanel.IsGestureAvailable)
            {
                GestureSample gestureSample = TouchPanel.ReadGesture ();
                if (gestureSample.GestureType == GestureType.Tap)
                {
                    translateMatrix * = Matrix.CreateTranslation (0.3f, 0, 0);
                    scaleMatrix * = Matrix.CreateScale (0.9f);
                }
            }

            base.Update (gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </ summary>
        /// <param name = "gameTime"> Provides a snapshot of timing values. </ param>
        protected override void Draw (GameTime gameTime)
        {
            GraphicsDevice.Clear (Color.CornflowerBlue);

            basicEffect.World = scaleMatrix * translateMatrix;
            basicEffect.View = camera.view;
            basicEffect.Projection = camera.projection;

            foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
            {
                pass.Apply ();
                GraphicsDevice.DrawUserPrimitives <VertexPositionColor> (PrimitiveType.TriangleStrip, triangle, 0, 1);
            }

            base.Draw (gameTime);
        }
}



-- Welcome to reprint, please indicate the source of http://blog.csdn.net/caowenbin --


Related Article

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.