Ten components for 3D development in Windows Phone

Source: Internet
Author: User

In the previous article, we have implemented loading complex 3D models and controlling them in space. Usually in a game program, there are many such 3D models, if every one is drawn in the scene, the draw () method will be very complicated, and it is not conducive to code reuse. A better way is to implement the ship as gamecomponent, And it is drawablegamecomponent. Next we will construct the spacecraft components.


First, add a new element to the project. Use gamecomponent in xNa and make the following changes in the generated code:

Public class ship: Microsoft. xNa. Framework. drawablegamecomponent

Change gamecomponent to drawablegamecomponent, and then add the draw () method.

Public override void draw (gametimegametime)

Model model; and matrix [] modeltransforms are also used to save model and skeleton data. Complete the loading in the initialize () method. For the code, see:


    public override void Initialize()
        {
            model = Game.Content.Load<Model>(@"Models/wedge_player1");
            modelTransforms = new Matrix[model.Bones.Count];
            base.Initialize();
        }

However, after such encapsulation, the user cannot control the coordinate transformation. Therefore, three matrices for coordinate transformation are also required. Add member variables:


  public Matrix worldMatrix {set;get;}
        public Matrix viewMatrix { set; get; }
        public Matrix projectionMatrix { set; get; }

Finally, complete the draw () method. The Code is as follows:


  public override void Draw(GameTime gameTime)
        {
            //GraphicsDevice.Clear(Color.CornflowerBlue);

            model.CopyAbsoluteBoneTransformsTo(modelTransforms);
            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.World = modelTransforms[mesh.ParentBone.Index] * worldMatrix;
                    effect.View = viewMatrix;
                    effect.Projection = projectionMatrix;
                }
                mesh.Draw();
            }
            base.Draw(gameTime);
        }

In this way, this ship class is encapsulated. When used in the scenario, you need to create a ship object:

Ship = new ship (this );

Then add it to components:

Components. Add (SHIP );

In the loadcontent () method, specify the transformation parameter for it:


  ship.projectionMatrix = camera.projection;
            ship.viewMatrix = camera.view * Matrix.CreateTranslation(new Vector3(4, 0, 10));
            ship.worldMatrix = Matrix.CreateScale(-0.02f) * Matrix.CreateRotationZ(MathHelper.ToRadians(180))*Matrix.CreateTranslation(new Vector3(20,0,0));


Of course, you can also update ship. worldmatrix in the update () method to rotate, translate, and scale it. If you need to create multiple ship formations, you only need to create several ship objects and set them to different coordinates. In this way, the game components can be encapsulated by inheriting from gamecompenent, and the benefits can be achieved in code reusability.


Well, we already have more objects. Try to generate more ships to see if the formation is spectacular.


Source code of the ship class:


   public class Ship : Microsoft.Xna.Framework.DrawableGameComponent
    {
        public Matrix worldMatrix {set;get;}
        public Matrix viewMatrix { set; get; }
        public Matrix projectionMatrix { set; get; }
        Model model;
        Matrix[] modelTransforms;

        public Ship(Game game)
            : base(game)
        {
            worldMatrix = Matrix.Identity;
            viewMatrix = Matrix.Identity;
            projectionMatrix = Matrix.Identity;
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            model = Game.Content.Load<Model>(@"Models/wedge_player1");
            modelTransforms = new Matrix[model.Bones.Count];
            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            //GraphicsDevice.Clear(Color.CornflowerBlue);

            model.CopyAbsoluteBoneTransformsTo(modelTransforms);
            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.World = modelTransforms[mesh.ParentBone.Index] * worldMatrix;
                    effect.View = viewMatrix;
                    effect.Projection = projectionMatrix;
                }
                mesh.Draw();
            }
            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.