【问题标题】:xna camera rotates around own axis and looks at model groupxna 相机围绕自己的轴旋转并查看模型组
【发布时间】:2013-03-23 23:20:51
【问题描述】:

我制作了一个 3D 场景,其中包含三组模型。我有一台相机正在查看其中一个组。 这些组中的模型围绕组中心(上轴)旋转,模型也旋转自己的局部中心(上轴)。

这类似于 XNA Racing Game 的汽车选择屏幕。 唯一的区别是我希望能够旋转我的相机来查看另一组。 旋转相机以查看下一组时,我想将其旋转 120 度(我有 3 个模型组 360/3=120)

注意: - 摄像头正在从小组平面上方略高于小组的位置观察小组。

相机:

viewMatrix = Matrix.CreateLookAt(cameraPosition, cameraTarget, Vector3.Up);    
projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, aspectRatio, 1f, 1000f);

好的:

  • 我可以围绕自己的轴旋转模型。
  • 我可以围绕组中心点向左或向右旋转模型组 (在游戏画面中,离画面最近的模型是当前选中的模型)。

不行:

  • 我找不到正确的方法来围绕它自己的上轴旋转相机。

几张图片来说明这种情况:

【问题讨论】:

    标签: matrix xna camera


    【解决方案1】:

    在我看来,您希望所有的旋转都围绕世界上轴而不是相机的上轴。

    这是一种将视图从一个组更改为另一组的可能方法。这将以恒定的速度旋转。如果您希望它在到达所需组时稍微减慢旋转速度,则需要添加代码。这假设您知道每个组的中心位置。

    float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
    float rotationRate = 0.01f; //radians per second. set to taste
    Vector3 cameraGoal;// the center point of the group that you want the camera to settle on. changes with input.
    
    Vector3 currentLookingDirection = cameraTarget - cameraPosition;//
    Vector3 desiredLookingDirection = cameraGoal - cameraPosition;
    float angularSeparationFactor = Vector3.Dot(currentLookingDirection, desiredLookingDirection);
    if(angularSeparationFactor < 0.98f);//set to taste
    {
      float directionToRotate = Math.Sign(Vector3.Cross(currentLookingDirection , desiredLookingDirection ).Y);
       cameraTarget = Vector3.Transform(cameraTarget - cameraPosition, Matrix.CreateRotationY(rotationRate * elapsed * directionToRotate)) + cameraPosition;
    }
    else
    {
       cameraTarget = cameraGoal;
    }
    
    view = Matrix.CreatLookAt(cameraPosition, cameraTarget, Vector3.Up);
    

    【讨论】:

      猜你喜欢
      • 2013-03-19
      • 2012-09-29
      • 1970-01-01
      • 1970-01-01
      • 2016-09-22
      • 2018-08-03
      • 2017-10-13
      • 2023-03-07
      相关资源
      最近更新 更多