Unity - Clamp Rotation between arbitrary angles

微笑、不失礼 提交于 2019-12-08 01:35:11

问题


I have a task where I have to clamp an angle between other two angle.

The catch is that the limits can be >360 or <0

(ex. [-45,45] or [275,45]).

Is there a clean way to do this taking into account all the special cases?

(ex. range [-45,45] and input angle of 225 should be -45).

Thanks in advance! P.S. I am using unity, so I have all the default Quaternion methods at hand.

Current Code:

  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);
}

回答1:


OK, so I found the solution which clamps the angle correctly in all cases,

Brought to you by Quaternion.Angle() I present :

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);


来源:https://stackoverflow.com/questions/43492004/unity-clamp-rotation-between-arbitrary-angles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!