XNA中摄像机是通过View和Projection来体现的,其中View矩阵用于实现世界坐标到摄像机坐标的转换。Projection是投影矩阵,用于完成摄像机坐标系到屏幕坐标系的转换。根据面向对象的思想及程序重用性和可维护性的需要可以将有关摄像机的操作封装到一个摄像机类中,同时提供第一人称、第三人称摄像机。
1、 View的创建
public static Matrix CreateLookAt (
Vector3 cameraPosition, /* The position of the camera */
Vector3 cameraTarget, /* The direction that the camera is pointing */
Vector3 cameraUpVector
)
Vector3 cameraUpVector: The direction that is "up" from the camera's point of view
view = Matrix.CreateLookAt(cameraPosition, cameraLookat, cameraLookUp);
2、 Projection的创建
public static Matrix CreatePerspectiveFieldOfView (
float fieldOfView, /*Field of view in radians*/
float aspectRatio, /*宽高比*/
float nearPlaneDistance, /*Distance to the near view plane*/
float farPlaneDistance /*Distance to the far view plane*/
)
float aspectRatio:Aspect ratio, defined as view space width divided by height
proj = Matrix.CreatePerspectiveFieldOfView(viewAngle, aspectRatio, nearClip, farClip);
在编程过程中我们需要改变摄像机的位置和摄像机所观察的指向来跟踪物体或者完成第一人称摄像机与第三人称摄像机的切换。