【发布时间】:2014-07-12 02:23:03
【问题描述】:
我已经阅读了各种确定两个向量之间角度的方法,我真的很困惑,所以我需要一些帮助来了解我需要做什么。
下面是我的第一人称相机代码
public class FPSCamera : Engine3DObject
{
public Matrix View { get; private set; }
public Matrix Projeciton { get; private set; }
private Quaternion rotation;
private float yaw;
private float pitch;
private bool isViewDirty;
private bool isRotationDirty;
public BoundingFrustum Frustum { get; private set; }
public bool Changed { get; private set; }
public Vector3 ForwardVector
{
get
{
return this.View.Forward;
}
}
public FPSCamera()
: base()
{
this.Position = Vector3.Zero;
this.rotation = Quaternion.Identity;
this.yaw = this.pitch = 0;
this.isViewDirty = true;
this.isRotationDirty = false;
}
public void SetPosition(Vector3 position)
{
this.Position = position;
this.isViewDirty = true;
this.Update(null);
}
public void Initialize(float aspectRatio)
{
this.Projeciton = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, aspectRatio, 1f, 1000f);
}
public void Update(GameTime gameTime)
{
this.Changed = false;
if (isRotationDirty)
{
if (yaw > MathHelper.TwoPi)
{
yaw = yaw - MathHelper.TwoPi;
}
if (yaw < -MathHelper.TwoPi)
{
yaw = yaw + MathHelper.TwoPi;
}
if (pitch > MathHelper.TwoPi)
{
pitch = pitch - MathHelper.TwoPi;
}
if (pitch < -MathHelper.TwoPi)
{
pitch = pitch + MathHelper.TwoPi;
}
this.rotation = Quaternion.CreateFromYawPitchRoll(yaw, pitch, 0);
this.isRotationDirty = false;
this.isViewDirty = true;
this.Changed = true;
}
if (isViewDirty)
{
Vector3 up = Vector3.Transform(Vector3.Up, rotation);
Vector3 target = Vector3.Transform(Vector3.Forward, rotation) + Position;
this.View = Matrix.CreateLookAt(this.Position, target, up);
this.isViewDirty = false;
if (this.Frustum == null)
{
this.Frustum = new BoundingFrustum(this.View * this.Projeciton);
}
else
{
this.Frustum.Matrix = (this.View * this.Projeciton);
}
this.Changed = true;
}
}
public void Move(Vector3 distance)
{
this.Position += Vector3.Transform(distance, rotation);
this.isViewDirty = true;
}
public void Rotate(float yaw, float pitch)
{
this.yaw += yaw;
this.pitch += pitch;
this.isRotationDirty = true;
}
public void LookAt(Vector3 lookAt)
{
}
}
“lookat”方法是空白的,因为我正试图弄清楚如何去做。我移动相机,使其位置为 (500,500,500) 并需要它查看 (0,0,0) 但我不知道如何获得它们之间的偏航和俯仰,因此我可以正确设置旋转。我已经阅读了有关规范化向量和使用交叉点积的信息,但是您无法规范化 (0,0,0),因为它没有方向,所以我不知道该怎么做。任何帮助将不胜感激。
【问题讨论】: