上節內容中,我們已經有了第一個三維物體,本節中我們就先讓它擁有最基本的平移和縮放的運動。
回顧我們的BasicEffect,它有一個World屬性是用於決定物體座標系的,如果我們對這個屬性施加一些運算,就可以讓物體動起來。通常,World的取值是單位矩陣,即物體位於原點處。要讓物體發生移動,只需要在對應的座標軸上與一個矩陣相乘。這個矩陣的構造方法是:
Matrix CreateTranslation(float xPosition, float yPosition, float zPosition);
三個參數分別代表在x、y、z三個座標軸上的位移量。
同理,縮放變化也是將一個縮放矩陣與物體所在位置的矩陣相乘,構造縮放矩陣的方法是:
Matrix CreateScale(float xScale, float yScale, float zScale);
同樣的,三個參數分別代表在x、y、z三個座標軸上的縮放的比例。如果想在三個軸上做相同比例的等比縮放,可以使用:
Matrix CreateScale(float scale);
沿用上節課我們建好的XNA項目,在VS2010中開啟該項目。開啟Game1.cs檔案,我們來修改Game1類。為其添加兩個成員變數,分別代表平移矩陣和縮放矩陣:
Matrix translateMatrix=Matrix.Identity;
Matrix scaleMatrix=Matrix.Identity;
然後在Draw()方法中修改basicEffect對象的World屬性:
basicEffect.World= scaleMatrix * translateMatrix;
為了增加效果,我們不妨增加一點互動,即每點擊一次螢幕,就讓物體發生一點運動,從而可以更好的觀察物體運動的形態。這樣的互動肯定是要放在Update()方法當中的,要做的就是每次點擊螢幕時調整平移矩陣和縮放矩陣的值(手勢識別的方法在此不做介紹)。
最後,我們還可以將嘗試修改平移矩陣和縮放矩陣的值以及二者相乘的順序,觀察最終的運動效果。
附本節Game1類的完整源碼:
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); } }
——歡迎轉載,請註明出處 http://blog.csdn.net/caowenbin ——