بســم الله الـرحمــن الرحيــم الدرس الخامس أهلا بكم في الدرس الخامس من سلسلة دروس تعلم ال3D Xna السلسلة الأولى، في هذا الدرس سوف نتحدث عن التدوير و النقل. سوف نقوم بجعل مثلثنا يدور ويتحرك. بما أننا نستخدم فضاء إحداثيات العالم، من السهل عمل ذلك. دعنا أولا نضيف متغير بإسم ‘angle’ إلى الصنف الخاص بنا من أجل تخزين زاوية التدوير الحالية. فقط قم بإضافة السطر التالي إلى المتغيرات.
private float angle = 0f;
angle += 0.005f;
Matrix worldMatrix = Matrix.CreateRotationY(3 * angle); effect.Parameters["xWorld"].SetValue(worldMatrix);
Matrix worldMatrix = Matrix.CreateTranslation(-20.0f/3.0f, -10.0f / 3.0f, 0) * Matrix.CreateRotationZ(angle);
Vector3 rotAxis = new Vector3(3*angle, angle, 2*angle); rotAxis.Normalize(); Matrix worldMatrix = Matrix.CreateTranslation(-20.0f/3.0f, -10.0f / 3.0f, 0) * Matrix.CreateFromAxisAngle(rotAxis, angle);
using System; using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.GamerServices; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Net; using Microsoft.Xna.Framework.Storage; namespace XNAtutorial { public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; GraphicsDevice device; Effect effect; VertexPositionColor vertices; VertexDeclaration myVertexDeclaration; Matrix viewMatrix; Matrix projectionMatrix; private float angle = 0f; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } protected override void Initialize() { graphics.PreferredBackBufferWidth = 500; graphics.PreferredBackBufferHeight = 500; graphics.IsFullScreen = false; graphics.ApplyChanges(); Window.Title = "Riemer's XNA Tutorials -- Series 1"; base.Initialize(); } protected override void LoadContent() { device = graphics.GraphicsDevice; spriteBatch = new SpriteBatch(GraphicsDevice); effect = Content.Load ("effects"); SetUpVertices(); SetUpCamera(); } private void SetUpVertices() { vertices = new VertexPositionColor[3]; vertices[0].Position = new Vector3(0f, 0f, 0f); vertices[0].Color = Color.Red; vertices[1].Position = new Vector3(10f, 10f, 0f); vertices[1].Color = Color.Yellow; vertices[2].Position = new Vector3(10f, 0f, -5f); vertices[2].Color = Color.Green; myVertexDeclaration = new VertexDeclaration(device, VertexPositionColor.VertexElements); } private void SetUpCamera() { viewMatrix = Matrix.CreateLookAt(new Vector3(0, 0, 50), new Vector3(0, 0, 0), new Vector3(0, 1, 0)); projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, device.Viewport.AspectRatio, 1.0f, 300.0f); } protected override void UnloadContent() { } protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); angle += 0.005f; base.Update(gameTime); } protected override void Draw(GameTime gameTime) { device.Clear(Color.DarkSlateBlue); device.RenderState.CullMode = CullMode.None; Vector3 rotAxis = new Vector3(3 * angle, angle, 2 * angle); rotAxis.Normalize(); Matrix worldMatrix = Matrix.CreateTranslation(-20.0f/3.0f, -10.0f / 3.0f, 0) * Matrix.CreateFromAxisAngle(rotAxis, angle); effect.CurrentTechnique = effect.Techniques["Colored"]; effect.Parameters["xView"].SetValue(viewMatrix); effect.Parameters["xProjection"].SetValue(projectionMatrix); effect.Parameters["xWorld"].SetValue(worldMatrix); effect.Begin(); foreach (EffectPass pass in effect.CurrentTechnique.Passes) { pass.Begin(); device.VertexDeclaration = myVertexDeclaration; device.DrawUserPrimitives(PrimitiveType.TriangleList, vertices, 0, 1); pass.End(); } effect.End(); base.Draw(gameTime); } } }