嘿!要到禮拜五,時間過得真快。好吧,切入正題,繼續說個人loon開發心得!事先說明,說的很差,說錯的,可別噴人! 框架
我還沒接觸3d的時候,一直想着3d這視圖是怎麼定位的,怎麼轉起來,用到了什麼技術。我一直帶着這個疑問開始了本身的3d之路。3d是難,因此我選擇了難中最簡單的3d框架(xna)來學習。學的過程第一個接觸到的就是3d世界裏的攝像機。對,攝像機在3d裏面是不可缺的組件!來講說像魔獸世界那樣的相機是怎麼實現的! 學習
一個攝像機由兩個矩陣組成,1:投影矩陣,視圖矩陣。在xna裏面的矩陣是4*4的。在xna下,建立這兩個矩陣是很輕鬆的事情,不要任何基本技術。建立投影矩陣Matrix.CreatePerspectiveFieldOfView 用這個就很容易的建立出來, 只須要四個參數 ,視角弧度,攝像機長寬比,攝像機多近時沒法看清物體,攝像機多遠時沒法看清物體。怎麼樣,很簡單吧。通常投影矩陣建立了以後,就不會再改變它了,除非你屏幕拉大變小要從新建立投影矩陣。建立視圖矩陣也簡單,不過這個視圖矩陣參數要了解清楚,由於在遊戲過程當中,咱們要實時的更新這個視圖矩陣。用Matrix.CreateLookAt 能夠建立一個視圖矩陣。來講下這個視圖矩陣的參數,1攝像機的位置,這位在比較好理解。2:攝像機的方向,這個方向就重要了,這個方向要加上相機的位置才能獲得攝像機最終的方向。比如你在馬路上行走,你頭一直向左看着,你人向前移動,你眼睛看得東西是否是在日後移動。由於你向前移動了,方向也跟着向前移動,因此,視圖矩陣的第二參數(方向)必定要加上相機的位置才行!相機的方向不少地方會用到。好比:控制role向前移動的方向3:相機的朝向,相機究竟是倒立着,仍是正的,就靠這第三參數了。有了上面兩個矩陣就能夠組成一個相機了,無論複雜的相機都是如此!接下來,我也不廢話了,看下魔獸的相機是怎麼實現的,相機怎麼圍繞的role旋轉,怎麼用鼠標控制相機左右上下旋轉。 this
說下基本知識: spa
向量 Vector3 就x,y,z。 3d
矩陣 Matrix 矩陣旋轉:Matrix.CreateRotationX 圍繞x轉旋轉,還有y,z同理。 orm
Vector3.Transform 這個根據一個矩陣獲取一個向量。 遊戲
xna裏面的座標系是右手座標系(笛卡爾座標系)。 ci
好晚了,不寫了,今天就到這把!我先把代碼貼出來,下個微博再爲大家解釋,抱歉! 開發
這是相機類,跟wow同樣的相機,不會難。 支持者,請頂一下,讓更多的人看到。 get
//鼠標控制相機旋轉
int dx = this.engine.mouse.X - this.engine.oldMouse.X;
int dy = this.engine.mouse.Y - this.engine.oldMouse.Y;
this.engine.oldMouse = this.engine.mouse;
if (this.engine.mouse.RightButton == ButtonState.Pressed)
{
CameraOrbitAngle += dx * MathHelper.Pi / 300.0f;
CameraPitchAngle -= dy * MathHelper.Pi / 300.0f;
}
this.engine.camera.SetThirdPersonView(CameraOrbitAngle, CameraPitchAngle);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Loon.SceneNodes;
namespace Loon.Cameras
{
public class Camera
{
private Vector3 fang = Vector3.Zero;
private Matrix projectionMatrix;
private Matrix viewMatrix;
public Matrix cameraFacingMatrix = Matrix.Identity;
public Vector3 CameraPositionOffset = new Vector3(0, 0, 100);
public Vector3 CameraTargetOffset = new Vector3(0, 0, 0);
public Vector3 cameraPosition = Vector3.Zero;
public Vector3 cameraTarget = Vector3.Zero;
private float orbitRadians = 0.0f;
private float pitchRadians = 0.0f;
public GraphicsDevice graphicsDevice;
RoleModelSceneNode role;
public Camera(GraphicsDevice graphicsDevice)
{
this.graphicsDevice = graphicsDevice;
projectionMatrix = Matrix.CreatePerspectiveFieldOfView(
MathHelper.ToRadians(45.0f), graphicsDevice.Viewport.AspectRatio, 1f, 1000);
}
public void Update() {
cameraFacingMatrix = Matrix.CreateRotationX(-pitchRadians) * Matrix.CreateRotationY(-orbitRadians);
Vector3 positionOffset = Vector3.Transform(CameraPositionOffset,
cameraFacingMatrix);
Vector3 targetOffset = Vector3.Transform(CameraTargetOffset,
cameraFacingMatrix);
cameraPosition = Utility.JVectorToVector(role.Body.Position) + positionOffset;
cameraTarget = Utility.JVectorToVector(role.Body.Position) + targetOffset;
fang = cameraTarget - cameraPosition;
fang.Y = 0.0000001f;
fang.Normalize();
viewMatrix = Matrix.CreateLookAt(cameraPosition,
cameraTarget,
Vector3.Up);
}
public Vector3 GetCameraDirection() {
Vector3 dir = Vector3.Zero;
dir = cameraTarget - cameraPosition;
dir.Normalize();
return dir;
}
public void SetThirdPersonView(float orbitRadians, float pitchRadians)
{
this.orbitRadians = orbitRadians;
this.pitchRadians = pitchRadians;
}
public Vector3 Fang
{
get {
fang.Y = 0;
fang.Normalize();
return fang;
}
set { fang = value; }
}
public Matrix ViewMatrix
{
get { return viewMatrix; }
set { viewMatrix = value; }
}
public Matrix ProjectionMatrix
{
get { return projectionMatrix; }
set { projectionMatrix = value; }
}
public RoleModelSceneNode Role { get { return role; } set { role = value; } } } }