ดูการแปลงพิกัด
สรุป
คุณกําลังจัดการพารามิเตอร์ของการแปลงพิกัดมุมมอง
สภาพแวดล้อมในการทํางาน
ข้อกําหนดเบื้องต้น
รุ่น XNA ที่รองรับ |
|
แพลตฟอร์มที่รองรับ |
|
Windows ต้องใช้เวอร์ชัน Vertex Shader | 2.0 |
เวอร์ชัน Pixel Shader ที่จําเป็นของ Windows | 2.0 |
สภาพแวดล้อมในการทํางาน
แท่น |
|
วิธีการทํางานกับตัวอย่าง
เมาส์แป้นพิมพ์ใช้งานได้คอนโทรลเลอร์ Xbox | 360 | สัมผัส | ||
---|---|---|---|---|
การย้ายตําแหน่งของกล้อง | ↑↓←→ | แท่งซ้าย | ปุ่มซ้าย (ย้ายอัตโนมัติ, สลับการเคลื่อนไหว) | - |
การย้ายจุดสนใจของกล้อง | ดับเบิลยู, ซ, เอ, เอส | แท่งขวา | ปุ่มซ้าย (ย้ายอัตโนมัติ, สลับการเคลื่อนไหว) | - |
หมุนกล้องขึ้น | เอ็กซ์,ซี | ไกปืน | ปุ่มซ้าย (หมุนอัตโนมัติ, สลับการเคลื่อนไหว) | - |
สาร
เกี่ยวกับการแปลงมุมมอง
โปรดดูหน้าต่อไปนี้สําหรับภาพรวมของการแปลงพิกัดมุมมอง
สนาม
ในกรณีนี้เราจะแทนที่พารามิเตอร์ที่ใช้สําหรับการแปลงพิกัดมุมมองด้วยกล้องและใช้งานพารามิเตอร์สามตัวของ "ตําแหน่งกล้อง" "จุดจ้องมองกล้อง" และ "ทิศทางกล้องขึ้น"
<summary>
カメラの位置
</summary>
private Vector3 cameraPosition = new Vector3(2.0f, 4.0f, 20.0f);
<summary>
カメラの注視点
</summary>
private Vector3 cameraTarget = new Vector3(0.0f, 0.0f, 0.0f);
<summary>
カメラの上方方向回転角度(radian)
</summary>
private float cameraUpVectorRotate = 0.0f;
การทํางานกับกล้อง
ในเมธอด Game.Update เราใช้แป้นพิมพ์และคอนโทรลเลอร์เพื่อจัดการพารามิเตอร์แต่ละตัวของกล้อง สําหรับรายละเอียดของการดําเนินการแต่ละครั้ง โปรดดู "วิธีการ" ที่ด้านบนของหน้านี้ (รหัสด้านล่างคัดลอกมาจากส่วนการทํางานด้วยแป้นเกมเท่านั้น)
GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);
float speed = 0.25f;
// カメラの位置を移動させる
this.cameraPosition.X += gamePadState.ThumbSticks.Left.X * speed;
this.cameraPosition.Y += gamePadState.ThumbSticks.Left.Y * speed;
// カメラの注視点を移動させる
this.cameraTarget.X += gamePadState.ThumbSticks.Right.X * speed;
this.cameraTarget.Y += gamePadState.ThumbSticks.Right.Y * speed;
// カメラの上方方向を回転させる
this.cameraUpVectorRotate -= gamePadState.Triggers.Left * speed * 0.2f;
this.cameraUpVectorRotate += gamePadState.Triggers.Right * speed * 0.2f;
การสร้างเมทริกซ์มุมมอง
เมธอด "Matrix.CreateLookAt" ถูกตั้งค่าด้วยพารามิเตอร์แต่ละตัวเพื่อสร้างเมทริกซ์มุมมอง
อาร์กิวเมนต์แรกระบุตําแหน่งของกล้องอาร์กิวเมนต์ที่สองระบุจุดสนใจของกล้องและอาร์กิวเมนต์ที่สามระบุทิศทางขึ้นของกล้อง
เกี่ยวกับอาร์กิวเมนต์ที่สาม "ทิศทางขึ้นของกล้อง" เวกเตอร์จะถูกคํานวณโดยการหมุนรอบแกน Z ตามทิศทางแกน Y (0,1,0)
// ビューマトリックス作成
Matrix view = Matrix.CreateLookAt(
this.cameraPosition,
this.cameraTarget,
Vector3.Transform(Vector3.Up,
Matrix.CreateRotationZ(this.cameraUpVectorRotate))
);
Matrix.CreateLookAt
วิธี
สร้างเมทริกซ์มุมมองสําหรับระบบพิกัดที่ถนัดขวา
ตําแหน่งกล้อง | เวกเตอร์ 3 | ตําแหน่งกล้อง |
กล้องเป้าหมาย | เวกเตอร์ 3 | จุดสนใจของกล้อง |
กล้องอัพเวกเตอร์ | เวกเตอร์ 3 | ทิศทางขึ้นของกล้อง |
ส่งคืนค่า | เมตริกซ์ | รับเมทริกซ์มุมมอง |
Vector3.Transform
วิธี
แปลงเวกเตอร์เป็นการแปลงพิกัด
ฐานะ | เวกเตอร์ 3 | เวกเตอร์ต้นทาง |
เมตริกซ์ | เมตริกซ์ | การแปลงพิกัด |
ส่งคืนค่า | เวกเตอร์ 3 | เวกเตอร์ที่มีการแปลงพิกัด |
Matrix.CreateRotationZ
วิธี
สร้างเมทริกซ์การหมุนรอบแกน Z
เรเดียน | ลอย | มุม การหมุน (เรเดียน) |
ส่งคืนค่า | เมตริกซ์ | เมทริกซ์การหมุนเกี่ยวกับแกน Z |
ชุดของเมทริกซ์มุมมอง
กล่องถูกวางไว้ที่จุดสนใจเพื่อให้เข้าใจตําแหน่งของจุดสนใจของกล้องได้ง่าย นอกจากนี้ยังมีการตั้งค่าเมทริกซ์มุมมองสําหรับแต่ละเอฟเฟกต์
foreach (ModelMesh mesh in this.box.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
// ライトの設定
effect.EnableDefaultLighting();
// ボックスの位置をカメラの注視点に合わせる
effect.World = Matrix.CreateTranslation(this.cameraTarget);
// ビューマトリックスを設定
effect.View = view;
// プロジェクションマトリックス設定
effect.Projection = projection;
}
mesh.Draw();
}
รหัสทั้งหมด
using System;
using System.Collections.Generic;
using System.Linq;
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.Media;
#if WINDOWS_PHONE
using Microsoft.Xna.Framework.Input.Touch;
#endif
namespace ViewMatrix
{
<summary>
ゲームメインクラス
</summary>
public class GameMain : Microsoft.Xna.Framework.Game
{
<summary>
グラフィックデバイス管理クラス
</summary>
private GraphicsDeviceManager graphics = null;
<summary>
スプライトのバッチ化クラス
</summary>
private SpriteBatch spriteBatch = null;
<summary>
スプライトでテキストを描画するためのフォント
</summary>
private SpriteFont font = null;
<summary>
ボックスモデル
</summary>
private Model box = null;
<summary>
XYZモデル
</summary>
private Model xyz = null;
<summary>
カメラの位置
</summary>
private Vector3 cameraPosition = new Vector3(2.0f, 4.0f, 20.0f);
<summary>
カメラの注視点
</summary>
private Vector3 cameraTarget = new Vector3(0.0f, 0.0f, 0.0f);
<summary>
カメラの上方方向回転角度(radian)
</summary>
private float cameraUpVectorRotate = 0.0f;
<summary>
自動動作フラグ
</summary>
private int autoMode = 0;
<summary>
マウスボタン押下フラグ
</summary>
private bool isMousePressed = false;
<summary>
GameMain コンストラクタ
</summary>
public GameMain()
{
// グラフィックデバイス管理クラスの作成
this.graphics = new GraphicsDeviceManager(this);
// ゲームコンテンツのルートディレクトリを設定
this.Content.RootDirectory = "Content";
#if WINDOWS_PHONE
// Windows Phone のデフォルトのフレームレートは 30 FPS
this.TargetElapsedTime = TimeSpan.FromTicks(333333);
// バックバッファサイズの設定
this.graphics.PreferredBackBufferWidth = 480;
this.graphics.PreferredBackBufferHeight = 800;
// フルスクリーン表示
this.graphics.IsFullScreen = true;
#endif
}
<summary>
ゲームが始まる前の初期化処理を行うメソッド
グラフィック以外のデータの読み込み、コンポーネントの初期化を行う
</summary>
protected override void Initialize()
{
// TODO: ここに初期化ロジックを書いてください
// コンポーネントの初期化などを行います
base.Initialize();
}
<summary>
ゲームが始まるときに一回だけ呼ばれ
すべてのゲームコンテンツを読み込みます
</summary>
protected override void LoadContent()
{
// テクスチャーを描画するためのスプライトバッチクラスを作成します
this.spriteBatch = new SpriteBatch(this.GraphicsDevice);
// フォントをコンテンツパイプラインから読み込む
this.font = this.Content.Load<SpriteFont>("Font");
// ボックスモデルを読み込み
this.box = this.Content.Load<Model>("Box");
// XYZ軸モデルを読み込み
this.xyz = this.Content.Load<Model>("XYZ");
}
<summary>
ゲームが終了するときに一回だけ呼ばれ
すべてのゲームコンテンツをアンロードします
</summary>
protected override void UnloadContent()
{
// TODO: ContentManager で管理されていないコンテンツを
// ここでアンロードしてください
}
<summary>
描画以外のデータ更新等の処理を行うメソッド
主に入力処理、衝突判定などの物理計算、オーディオの再生など
</summary>
<param name="gameTime">このメソッドが呼ばれたときのゲーム時間</param>
protected override void Update(GameTime gameTime)
{
KeyboardState keyState = Keyboard.GetState();
MouseState mouseState = Mouse.GetState();
GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);
// Xbox 360 コントローラ、Windows Phone の BACK ボタンを押したときに
// ゲームを終了させます
if (gamePadState.Buttons.Back == ButtonState.Pressed)
{
this.Exit();
}
// マウスによる自動動作切り替え
if (this.isMousePressed == false &&
mouseState.LeftButton == ButtonState.Pressed)
{
this.isMousePressed = true;
this.autoMode = (this.autoMode + 1) % 6;
}
this.isMousePressed = mouseState.LeftButton == ButtonState.Pressed;
float speed = 0.25f;
// カメラの位置を移動させる
this.cameraPosition.X += gamePadState.ThumbSticks.Left.X * speed;
this.cameraPosition.Y += gamePadState.ThumbSticks.Left.Y * speed;
if (keyState.IsKeyDown(Keys.Left))
{
this.cameraPosition.X -= speed;
}
if (keyState.IsKeyDown(Keys.Right))
{
this.cameraPosition.X += speed;
}
if (keyState.IsKeyDown(Keys.Down))
{
this.cameraPosition.Y -= speed;
}
if (keyState.IsKeyDown(Keys.Up))
{
this.cameraPosition.Y += speed;
}
if (this.autoMode == 1)
{
this.cameraPosition.X = (float)Math.Sin(gameTime.TotalGameTime.TotalSeconds) * 10.0f;
}
if (this.autoMode == 2)
{
this.cameraPosition.Y = (float)Math.Sin(gameTime.TotalGameTime.TotalSeconds) * 10.0f;
}
// カメラの注視点を移動させる
this.cameraTarget.X += gamePadState.ThumbSticks.Right.X * speed;
this.cameraTarget.Y += gamePadState.ThumbSticks.Right.Y * speed;
if (keyState.IsKeyDown(Keys.A))
{
this.cameraTarget.X -= speed;
}
if (keyState.IsKeyDown(Keys.S))
{
this.cameraTarget.X += speed;
}
if (keyState.IsKeyDown(Keys.Z))
{
this.cameraTarget.Y -= speed;
}
if (keyState.IsKeyDown(Keys.W))
{
this.cameraTarget.Y += speed;
}
if (this.autoMode == 3)
{
this.cameraTarget.X = (float)Math.Sin(gameTime.TotalGameTime.TotalSeconds) * 10.0f;
}
if (this.autoMode == 4)
{
this.cameraTarget.Y = (float)Math.Sin(gameTime.TotalGameTime.TotalSeconds) * 10.0f;
}
// カメラの上方方向を回転させる
this.cameraUpVectorRotate -= gamePadState.Triggers.Left * speed * 0.2f;
this.cameraUpVectorRotate += gamePadState.Triggers.Right * speed * 0.2f;
if (keyState.IsKeyDown(Keys.X))
{
this.cameraUpVectorRotate -= speed * 0.2f;
}
if (keyState.IsKeyDown(Keys.C))
{
this.cameraUpVectorRotate += speed * 0.2f;
}
if (this.autoMode == 5)
{
this.cameraUpVectorRotate = (float)gameTime.TotalGameTime.TotalSeconds;
}
// 登録された GameComponent を更新する
base.Update(gameTime);
}
<summary>
描画処理を行うメソッド
</summary>
<param name="gameTime">このメソッドが呼ばれたときのゲーム時間</param>
protected override void Draw(GameTime gameTime)
{
// 画面を指定した色でクリアします
this.GraphicsDevice.Clear(Color.CornflowerBlue);
// 深度バッファを有効にする
this.GraphicsDevice.DepthStencilState = DepthStencilState.Default;
// ビューマトリックス作成
Matrix view = Matrix.CreateLookAt(
this.cameraPosition,
this.cameraTarget,
Vector3.Transform(Vector3.Up,
Matrix.CreateRotationZ(this.cameraUpVectorRotate))
);
// プロジェクションマトリックス作成
Matrix projection = Matrix.CreatePerspectiveFieldOfView(
MathHelper.PiOver4,
(float)this.GraphicsDevice.Viewport.Width /
(float)this.GraphicsDevice.Viewport.Height,
1.0f,
100.0f
);
// ボックスモデル /////
foreach (ModelMesh mesh in this.box.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
// ライトの設定
effect.EnableDefaultLighting();
// ボックスの位置をカメラの注視点に合わせる
effect.World = Matrix.CreateTranslation(this.cameraTarget);
// ビューマトリックスを設定
effect.View = view;
// プロジェクションマトリックス設定
effect.Projection = projection;
}
mesh.Draw();
}
// XYZ軸モデル /////
foreach (ModelMesh mesh in this.xyz.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
// ライトの設定
effect.EnableDefaultLighting();
// 原点に配置
effect.World = Matrix.Identity;
// ビューマトリックスを設定
effect.View = view;
// プロジェクションマトリックス設定
effect.Projection = projection;
}
mesh.Draw();
}
// スプライトの描画準備
this.spriteBatch.Begin();
// カメラの情報を表示
this.spriteBatch.DrawString(this.font,
"CameraPosition : {" +
this.cameraPosition.X.ToString("f2") + ", " +
this.cameraPosition.Y.ToString("f2") + "}" + Environment.NewLine +
"CameraTarget : {" +
this.cameraTarget.X.ToString("f2") + ", " +
this.cameraTarget.Y.ToString("f2") + "}" + Environment.NewLine +
"CameraUpVectorRotate : " + this.cameraUpVectorRotate.ToString("f2") + Environment.NewLine +
"MousePressAutoMode : " + this.autoMode,
new Vector2(20, 20), Color.White);
// スプライトの一括描画
this.spriteBatch.End();
// 登録された DrawableGameComponent を描画する
base.Draw(gameTime);
}
}
}