我有一个任务,我必须夹住另外两个角度之间的角度。
问题在于限制可以是> 360或<0
(例如[-45,45]或[275,45])。
考虑到所有特殊情况,是否有一种干净的方法可以做到这一点 例
(例如范围[-45,45]和输入角度225应为-45)。
提前致谢! 附:我使用的是统一,所以我手边有所有默认的Quaternion方法。
当前代码:
Quaternion inputAngle = Quaternion.identity;
if (Character.IsFacingRight)
inputAngle = Quaternion.FromToRotation(forwardVector, playerInput);
else
inputAngle = Quaternion.FromToRotation(playerInput, forwardVector);
Quaternion minAngle = Quaternion.Euler(0F, 0F, addedForce.force.angle);
Quaternion angleRange = Quaternion.Euler(0F, 0F, addedForce.force.angleRange);
Quaternion maxAngle = angleRange * minAngle;
// var yaw = Math.Atan2(2.0 * (inputAngle.y * inputAngle.z + inputAngle.w * inputAngle.x), inputAngle.w * inputAngle.w - inputAngle.x * inputAngle.x - inputAngle.y * inputAngle.y + inputAngle.z * inputAngle.z);
// var pitch = Math.Asin(-2.0 * (inputAngle.x * inputAngle.z - inputAngle.w * inputAngle.y));
float roll = (float)Math.Atan2(2.0 * (inputAngle.x * inputAngle.y + inputAngle.w * inputAngle.z), inputAngle.w * inputAngle.w + inputAngle.x * inputAngle.x - inputAngle.y * inputAngle.y - inputAngle.z * inputAngle.z);
Quaternion correctedRotation = Quaternion.Euler(0F, 0F, roll / Mathf.PI * 180F);
float endAngleZ = ClampRotation2(correctedRotation.eulerAngles.z, minAngle.eulerAngles.z, maxAngle.eulerAngles.z);
Quaternion endAngle = Quaternion.Euler(0F, 0F, endAngleZ);
ClampRotation2:
public static float ClampRotation2(float angle, float min, float max)
{
if (angle < 0) angle += 360;
if (max < 0) max += 360;
if (min < 0) min += 360;
if (min > max) min -= 360;
return Mathf.Clamp(angle, min, max);
}
答案 0 :(得分:2)
好的,所以我找到了在所有情况下正确夹住角度的解决方案,
Quaternion.Angle()
我带来的是
Quaternion inputAngle = Quaternion.identity;
if (Character.IsFacingRight)
inputAngle = Quaternion.FromToRotation(forwardVector, playerInput);
else
inputAngle = Quaternion.FromToRotation(playerInput, forwardVector);
Quaternion minAngle = Quaternion.Euler(0F, 0F, addedForce.force.angle);
Quaternion angleRange = Quaternion.Euler(0F, 0F, addedForce.force.angleRange);
Quaternion maxAngle = angleRange * minAngle;
float roll = (float)Math.Atan2(2.0 * (inputAngle.x * inputAngle.y + inputAngle.w * inputAngle.z), inputAngle.w * inputAngle.w + inputAngle.x * inputAngle.x - inputAngle.y * inputAngle.y - inputAngle.z * inputAngle.z);
float correctedRotation = roll / Mathf.PI * 180F;
float minAngleF = minAngle.eulerAngles.z;
float maxAngleF = maxAngle.eulerAngles.z;
if (correctedRotation < 0) correctedRotation += 360;
if (maxAngleF < 0) maxAngleF += 360;
if (minAngleF < 0) minAngleF += 360;
if (minAngleF > maxAngleF) minAngleF -= 360;
if (correctedRotation < minAngleF || correctedRotation > maxAngleF)
{
float rotationToMax = Quaternion.Angle(Quaternion.Euler(0F, 0F, correctedRotation), Quaternion.Euler(0F, 0F, maxAngleF));
float rotationToMin = Quaternion.Angle(Quaternion.Euler(0F, 0F, correctedRotation), Quaternion.Euler(0F, 0F, minAngleF));
if (Mathf.Abs(rotationToMax) < Mathf.Abs(rotationToMin))
correctedRotation = maxAngleF;
else
correctedRotation = minAngleF;
}
Quaternion endAngle = Quaternion.Euler(0F, 0F, correctedRotation);