Vector3 Structure
ใน xna จะมีการใช้กฎมือขวานะค่ะ (หรือเปล่า)
โดยจะให้ แกน X คือเลื่อนไปทางขวาและซ้าย (- ซ้าย ขวา +)
แกน Y เลื่อนไปบนและล่าง (- ล่าง บน +)
ส่วนแกน Z จะเลื่อนเข้าเลื่อนออก ถ้านึกภาพไม่ออกแกน Z จะเลื่อนเข้ามากระแทกหน้าเราอะค่ะ
(- เลื่อนไปหมายถึงเราจะเห็นตัวมันเล็กลงเนื่องจากมันเลื่อนไปข้างหน้า[ไกลกล้อง] + เลื่อนเข้าจะเห็นใหญ่ขึ้น)
ภาพ right-handed ในหนังสือเล่มส้ม
ประมาณนี้มั่ง
แต่เนื่องจากไม่ค่อยจะเห็นภาพนะค่ะ
เลยลองมั่วถั่วดูดังนี้
คือไปโหลด 3d model หรืออะไรก้อได้ของมาสักหน่อยแล้วลองเลื่อนแกน ปรับๆ ดู
(ปรับค่า x y z)
ใน ส่วนนี้
WorldTransformMatrix = Matrix.CreateTranslation(x, y, z);
โดยแสดงโค้ดด้านล่างเด้อ
..................
#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion
namespace WindowsGame1{
///
/// This is the main type for your game
///
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;ContentManager content;Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(70.0f), (640.0f / 480.0f), 1.0f, 10000000f);
//Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, aspectRatio,0.0001f, 1000.0f, out projection);
Matrix view = Matrix.CreateLookAt(new Vector3(20.0f, 0.0f,20.0f), new Vector3(0f, 0f,0f), Vector3.Up);public float x = 0.0f;
public float y = 0.0f;public float z = 0.0f;
float count = 0;public Matrix WorldTransformMatrix = Matrix.CreateTranslation(0.0f,0.0f,0.0f);
public Matrix[] BoneTransforms;Model box;
//Vector3 boxposition;
Texture2D pointimg;SpriteBatch point;
public Game1(){
graphics = new GraphicsDeviceManager(this);content = new ContentManager(Services);}
///
/// 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.
///
protected override void Initialize(){
// TODO: Add your initialization logic here
box = content.Load<Model>("box");
BoneTransforms = new Matrix[box.Bones.Count];
box.CopyAbsoluteBoneTransformsTo(BoneTransforms);
foreach (ModelMesh mesh in box.Meshes){
foreach (BasicEffect effect in mesh.Effects){
effect.EnableDefaultLighting();
}
}
pointimg = content.Load<Texture2D>("point");point = new SpriteBatch(graphics.GraphicsDevice);
base.Initialize();}
///
/// Load your graphics content. If loadAllContent is true, you should
/// load content from both ResourceManagementMode pools. Otherwise, just
/// load ResourceManagementMode.Manual content.
///
/// Which type of content to load.
protected override void LoadGraphicsContent(bool loadAllContent){
if (loadAllContent){
// TODO: Load any ResourceManagementMode.Automatic content
}
// TODO: Load any ResourceManagementMode.Manual content
}
///
/// Unload your graphics content. If unloadAllContent is true, you should
/// unload content from both ResourceManagementMode pools. Otherwise, just
/// unload ResourceManagementMode.Manual content. Manual content will get
/// Disposed by the GraphicsDevice during a Reset.
///
/// Which type of content to unload.
protected override void UnloadGraphicsContent(bool unloadAllContent){
if (unloadAllContent){
// TODO: Unload any ResourceManagementMode.Automatic content
content.Unload();
}
// TODO: Unload any ResourceManagementMode.Manual content
}
///
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input and playing audio.
///
/// Provides a snapshot of timing values.
protected override void Update(GameTime gameTime){
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit();// TODO: Add your update logic here
count += (float)gameTime.ElapsedGameTime.Milliseconds;KeyboardState k = Keyboard.GetState();
if (count > 0.2f){
if (k.IsKeyDown(Keys.X) & k.IsKeyDown(Keys.Up)){
x++;
}
if (k.IsKeyDown(Keys.X) & k.IsKeyDown(Keys.Down)){
x--;
}
if (k.IsKeyDown(Keys.C) & k.IsKeyDown(Keys.Up)){
y++;
}
if (k.IsKeyDown(Keys.C) & k.IsKeyDown(Keys.Down)){
y--;
}
if (k.IsKeyDown(Keys.Z) & k.IsKeyDown(Keys.Up)){
z++;
}
if (k.IsKeyDown(Keys.Z) & k.IsKeyDown(Keys.Down)){
z--;
}
if (k.IsKeyDown(Keys.Space) ){
z = 0; x = 0; y = 0;
}
count = 0;
}
WorldTransformMatrix = Matrix.CreateTranslation(x, y, z);base.Update(gameTime);}
///
/// This is called when the game should draw itself.
///
/// Provides a snapshot of timing values.
protected override void Draw(GameTime gameTime){
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);// TODO: Add your drawing code here
foreach (ModelMesh mesh in box.Meshes){
foreach (BasicEffect effect in mesh.Effects){
effect.View = view;
effect.Projection = projection;
effect.World = BoneTransforms[mesh.ParentBone.Index] * WorldTransformMatrix;
}
mesh.Draw();
}
point.Begin();
point.Draw(pointimg,new Vector2(100,0),Color.White);point.End();
base.Draw(gameTime);}
}
}
จบแระค่ะ
บายค่ะ








