c# monogame如何使相机复位位置缓慢落后的球员



你好,我正在尝试创造一种MMO摄像机风格,玩家可以在其中拖动角色环顾四周,当他向前移动时,摄像机会慢慢旋转回玩家身后,当摄像机复位时,它会选择向左或向右移动玩家,这是最短的路径。目前,当你需要旋转360度到0度时,下面的代码不起作用。

[example of working][1]
[example of working][2]
[example of not working][3]
[1]: https://i.stack.imgur.com/LblS0.png
[2]: https://i.stack.imgur.com/ujiSs.png
[3]: https://i.stack.imgur.com/zpHGE.png
float yMaxRotation; //is our target rotation (our Green Pointer)
float yRotation; //is our camera rotation (our Grey Pointer)
yMaxRotation = target.rotation.Z - (MathHelper.PiOver2);
yMaxRotation = yMaxRotation % MathHelper.ToRadians(360);
if (yMaxRotation < 0) yMaxRotation += MathHelper.ToRadians(360);
float newRotation = yMaxRotation;
if (yMaxRotation <= MathHelper.ToRadians(90) && yRotation >= MathHelper.ToRadians(270))
{
newRotation = yMaxRotation - MathHelper.ToRadians(360);
}
if (yMaxRotation >= MathHelper.ToRadians(270) && yRotation <= MathHelper.ToRadians(90))
{
newRotation = yMaxRotation + MathHelper.ToRadians(360);
}
if (yRotation <= newRotation)
{
yRotation += (newRotation - yRotation) / 15;
}
if (yRotation > newRotation)
{
yRotation -= Math.Abs(newRotation - yRotation) / 15;
}

对于处理距离和方向的摄像机它在每次更新时调用这段代码

Position //is our cameras position
newPos //is our players character position - the offset (push camera back and up)
Vector3 newPos = ((target.position - target.positionOffset) + new Vector3(0, 0, 18));
SetLookAt(Position, newPos, Vector3.Backward);

在比较角度时,数学运算并不等于1和359之间的距离等于2。

float yMaxRotation; //is our target rotation (our Green Pointer)
float yRotation; //is our camera rotation (our Grey Pointer)
yMaxRotation = target.rotation.Z - MathHelper.PiOver2; // Copied from source.

float newRotationDelta = (yMaxRotation - yRotation - MathHelper.Pi - 
MathHelper.TwoPi) % MathHelper.TwoPi + MathHelper.Pi; // scale -179 to 180

yRotation += newRotationDelta / 15;

yMaxRotation - yRotation得到角。减去540(180 + 360或360的任何倍数,因为mod删除了它,以确保值是负的)以创建一个负的x轴反射角建模到范围(-359到0),然后加上180来反转反射并重新集中在零上。

最新更新