Matrix xna相机围绕自己的轴旋转,并查看模型组

Matrix xna相机围绕自己的轴旋转,并查看模型组,matrix,xna,camera,Matrix,Xna,Camera,我制作了一个3D场景,其中有三组模型。我有一个摄像头,它正看着其中一组人。 这些组中的模型围绕组中心(上轴)旋转,模型也旋转其自身的局部中心(上轴) 这类似于XNA赛车游戏的汽车选择屏幕。 唯一不同的是,我想能够旋转我的相机,看看另一组。 当旋转相机观看下一组时,我想将其旋转120度(我有3个模型组360/3=120) 注: -摄像机正从略高于组平面的位置观察组 对于摄像机: viewMatrix = Matrix.CreateLookAt(cameraPosition, cameraTarge

我制作了一个3D场景,其中有三组模型。我有一个摄像头,它正看着其中一组人。 这些组中的模型围绕组中心(上轴)旋转,模型也旋转其自身的局部中心(上轴)

这类似于XNA赛车游戏的汽车选择屏幕。 唯一不同的是,我想能够旋转我的相机,看看另一组。 当旋转相机观看下一组时,我想将其旋转120度(我有3个模型组360/3=120)

注: -摄像机正从略高于组平面的位置观察组

对于摄像机:

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

  • 我可以围绕它自己的轴旋转模型
  • 我可以围绕组中心点左右旋转模型组 (在游戏屏幕中,离屏幕最近的型号是当前选定的型号)
不正常:

  • 我找不到正确的方法来绕着它自己的上轴旋转相机
以下几张图片说明了这种情况:


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

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

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);
float经过=(float)gameTime.ElapsedGameTime.TotalSeconds;
浮动转速=0.01f//弧度每秒。品尝
Vector3摄像机镜头;//您希望相机固定在的组的中心点。随着输入的变化。
Vector3 currentLookingDirection=摄像机目标-摄像机位置//
Vector3 desiredLookingDirection=摄像机镜头-摄像机位置;
浮点角度分离因子=矢量3.Dot(currentLookingDirection,desiredLookingDirection);
if(角度分离系数<0.98f)//品尝
{
float directionToRotate=数学符号(Vector3.Cross(currentLookingDirection,desiredLookingDirection).Y);
cameraTarget=Vector3.Transform(cameraTarget-cameraPosition,Matrix.CreateRotationY(旋转速率*经过时间*方向旋转))+cameraPosition;
}
其他的
{
cameraTarget=cameraGoal;
}
视图=矩阵.createLookat(摄影机位置、摄影机目标、向量3.Up);