2012-10-11 49 views
0

如何在局部空间中放置摄像机?在世界空间而不是本地空间中的摄像机间距

我有一个模型可以在其轴上旋转,而不管它在世界空间中的哪个位置。我遇到的问题是不管模型的其他旋转(偏航)如何让相机俯仰。目前,如果模型在世界空间中朝北或朝南,摄像机会相应调整。但是,当模型面对任何其他的基本方向时,在试图俯仰相机之后,相机在模型后面以圆周运动上下移动(因为它仅识别世界空间而不识别模型面向哪个方向)。

如何获得与模型一起旋转的音高,以便无论模型面向哪个方向?当我尝试调整相机将移动模型?

// 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); 
    } 

回答

1
> 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; 
    } 
相关问题