摄像机旋转

三维场景中的旋转,是摄像机本身在世界坐标系中绕Y轴进行旋转,从而改变位置,而其他的姿态不变,也就是摄像机的Position向量绕着世界坐标系的Y轴进行旋转。

三种方式:

1,采用角度(不通用),此方法适合目标点是世界坐标系原点。

private float angleFirst = 0;
private void RotateYFirst(float paAngle)
{
   //通过角度
   angleFirst += paAngle;
   if (angle > 6.28f)
   {
       angle -= 6.28f;
   }
   camPosition = new Vector3(4 * (float)Math.Cos(angleFirst), 0, 4 * (float)Math.Sin(angleFirst));
   Matrix view = Matrix.LookAtLH(camPosition, camTarget, camUp);
   device.SetTransform(TransformType.View, view);
}

2,采用Matrix.RotationY方法对Position向量进行旋转(较为通用)

//通过世界矩阵的Transform
private void RotateYSecond(float paAngle)
{
   //摄像机绕世界坐标系的Y轴进行旋转,摄像机本身的姿态不进行任何改变
   Vector4 tempRotate = Vector3.Transform(
       camPosition, Matrix.RotationY(paAngle));
   camPosition = new Vector3(tempRotate.X, tempRotate.Y, tempRotate.Z);
 
   Matrix view = Matrix.LookAtLH(
       camPosition,
       camTarget,
       camUp);
   device.SetTransform(TransformType.View, view);
}

3,原理和2中一样,不过在旋转计算时,采用四元素数进行处理

//通过四元素数进行旋转变换(同样绕着世界坐标系的Y轴)
private void RotateYThree(float paAngle)
{
   Vector4 tempRotate = Vector3.Transform(
       camPosition,
       Matrix.RotationQuaternion(Quaternion.RotationAxis(new Vector3(0, 1, 0), paAngle)));
   camPosition = new Vector3(tempRotate.X, tempRotate.Y, tempRotate.Z);
 
   Matrix view = Matrix.LookAtLH(camPosition, camTarget, camUp);
   device.SetTransform(TransformType.View, view);
}
原文地址:https://www.cnblogs.com/sharpfeng/p/1999593.html