如何在局部空间中对相机进行倾斜?
我有一个模型,无论它在世界空间中的什么位置,它都可以在它的轴上旋转。我遇到的问题是,无论模型的其他旋转(偏航)如何,相机的倾斜都是相同的。目前,如果模型在世界空间中朝北或朝南,则摄影机会相应地倾斜。但是,当模型面向任何其他基本方向时,在尝试倾斜相机后,相机会在模型后面以圆周运动的方式上下移动(因为它只识别世界空间,而不是模型所面对的方向)。
我如何让俯仰与模型一起旋转,这样无论模型朝向哪个方向?当我尝试俯仰时,相机会在模型上移动吗?
// 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);
}
发布于 2012-10-11 22:13:05
> public void cameraPitch(Vector3 axisPitch, float pitch)
> {
> ModelLoad.CameraPos = Vector3.Transform(ModelLoad.CameraPos - ModelLoad.camTarget,
> Matrix.CreateFromAxisAngle(axisPitch, pitch)) + ModelLoad.camTarget;
> }
我应该在你的上一个问题中看到这一点。sry。
axisPitch确实是一个全局空间向量,它需要在局部空间中。这应该是可行的。
public void cameraPitch(float pitch)
{
Vector3 f = ModelLoad.camTarget - ModelLoad.CameraPos;
Vector3 axisPitch = Vector3.Cross(Vector3.Up, f);
axisPitch.Normalize();
ModelLoad.CameraPos = Vector3.Transform(ModelLoad.CameraPos - ModelLoad.camTarget,
Matrix.CreateFromAxisAngle(axisPitch, pitch)) + ModelLoad.camTarget;
}
https://stackoverflow.com/questions/12833530
复制相似问题