如何在本地空间中投放相机?
我有一个模型,无论它在世界空间中的哪个位置,它都会在其轴上旋转。我遇到的问题是让相机俯仰相同,而不管模型的其他旋转(偏航)。目前,如果模型在世界空间中面向北方或南方,则相机会相应地倾斜。但是当模型面对任何其他基本方向时,在尝试俯仰相机后,相机在模型后面以圆周运动上下移动(因为它只识别世界空间而不是模型面对的方向)。
如何让俯仰与模型一起旋转,以便无论模型面向哪个方向?当我尝试俯仰时,相机会在模型上方移动吗?
// Rotates model and pitches camera on its own axis
public void modelRotMovement(GamePadState pController)
{
/* For rotating the model left or right.
* Camera maintains distance from model
* throughout rotation and if model moves
* to a new position.
*/
Yaw = pController.ThumbSticks.Right.X * MathHelper.ToRadians(speedAngleMAX);
AddRotation = Quaternion.CreateFromYawPitchRoll(Yaw, 0, 0);
ModelLoad.MRotation *= AddRotation;
MOrientation = Matrix.CreateFromQuaternion(ModelLoad.MRotation);
/* Camera pitches vertically around the
* model. Problem is that the pitch is
* in worldspace and doesn't take into
* account if the model is rotated.
*/
Pitch = pController.ThumbSticks.Right.Y * MathHelper.ToRadians(speedAngleMAX);
}
// Orbit (yaw) Camera around model
public void cameraYaw(Vector3 axisYaw, float yaw)
{
ModelLoad.CameraPos = Vector3.Transform(ModelLoad.CameraPos - ModelLoad.camTarget,
Matrix.CreateFromAxisAngle(axisYaw, yaw)) + ModelLoad.camTarget;
}
// Pitch Camera around model
public void cameraPitch(Vector3 axisPitch, float pitch)
{
ModelLoad.CameraPos = Vector3.Transform(ModelLoad.CameraPos - ModelLoad.camTarget,
Matrix.CreateFromAxisAngle(axisPitch, pitch)) + ModelLoad.camTarget;
}
public void updateCamera()
{
cameraPitch(Vector3.Right, Pitch);
cameraYaw(Vector3.Up, Yaw);
}