c# - 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 到反转反射并重新定位为零。
推荐阅读
- javascript - Javascript逻辑错误 - 未显示正确答案与猜测数
- mongodb - 如何计算和更新mongodb中的平均值?
- perl - 在 Perl 中从 PATH 中删除 x 天的旧报告文件
- vue.js - Vue watch 不触发数组中的元素
- google-chrome - IIS 项目 ASP.NET Core 上本地网络中的自证书 - 不安全(net::ERR_CERT_COMMON_NAME_INVALID)
- sql-server - 无法从 Visual Studio 中的 SSMS 正确打开基于 SSIS 包的作业
- javascript - 如何仅处理弹出窗口
使用柏树的按钮? - swashbuckle - OSS 规范中的服务器元素
- javascript - 如何定期检查状态并与请求同步?
- ssl - 使用 HAProxy 入口控制器时,TLS 握手间歇性失败