• <bdo id='mS0MF'></bdo><ul id='mS0MF'></ul>
  • <legend id='mS0MF'><style id='mS0MF'><dir id='mS0MF'><q id='mS0MF'></q></dir></style></legend>

    1. <small id='mS0MF'></small><noframes id='mS0MF'>

      <i id='mS0MF'><tr id='mS0MF'><dt id='mS0MF'><q id='mS0MF'><span id='mS0MF'><b id='mS0MF'><form id='mS0MF'><ins id='mS0MF'></ins><ul id='mS0MF'></ul><sub id='mS0MF'></sub></form><legend id='mS0MF'></legend><bdo id='mS0MF'><pre id='mS0MF'><center id='mS0MF'></center></pre></bdo></b><th id='mS0MF'></th></span></q></dt></tr></i><div id='mS0MF'><tfoot id='mS0MF'></tfoot><dl id='mS0MF'><fieldset id='mS0MF'></fieldset></dl></div>
        <tfoot id='mS0MF'></tfoot>
      1. Unity - 任意角度之间的夹子旋转

        时间:2023-10-06
        • <bdo id='GhKhI'></bdo><ul id='GhKhI'></ul>
          <i id='GhKhI'><tr id='GhKhI'><dt id='GhKhI'><q id='GhKhI'><span id='GhKhI'><b id='GhKhI'><form id='GhKhI'><ins id='GhKhI'></ins><ul id='GhKhI'></ul><sub id='GhKhI'></sub></form><legend id='GhKhI'></legend><bdo id='GhKhI'><pre id='GhKhI'><center id='GhKhI'></center></pre></bdo></b><th id='GhKhI'></th></span></q></dt></tr></i><div id='GhKhI'><tfoot id='GhKhI'></tfoot><dl id='GhKhI'><fieldset id='GhKhI'></fieldset></dl></div>

            1. <small id='GhKhI'></small><noframes id='GhKhI'>

              <legend id='GhKhI'><style id='GhKhI'><dir id='GhKhI'><q id='GhKhI'></q></dir></style></legend>
                <tbody id='GhKhI'></tbody>
            2. <tfoot id='GhKhI'></tfoot>
                1. 本文介绍了Unity - 任意角度之间的夹子旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个任务,我必须在其他两个角度之间夹一个角度.

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

                  问题是限制可以是 >360 或 <0

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

                  (例如 [-45,45] 或 [275,45]).

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

                  考虑到所有特殊情况,有没有一种干净的方法来做到这一点?案例?

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

                  (例如,范围 [-45,45] 和 225 的输入角度应为 -45).

                  (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.

                  当前代码:

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

                  夹具旋转2:

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

                  推荐答案

                  好的,所以我找到了在所有情况下都能正确夹角的解决方案,

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

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

                  这篇关于Unity - 任意角度之间的夹子旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何逐渐旋转一个物体以面对另一个转动最短距离 下一篇:如何在 C#/Windows 窗体中翻转/旋转标签?

                  相关文章

                  <small id='iCEQr'></small><noframes id='iCEQr'>

                  • <bdo id='iCEQr'></bdo><ul id='iCEQr'></ul>

                    <tfoot id='iCEQr'></tfoot>

                  1. <legend id='iCEQr'><style id='iCEQr'><dir id='iCEQr'><q id='iCEQr'></q></dir></style></legend>

                    1. <i id='iCEQr'><tr id='iCEQr'><dt id='iCEQr'><q id='iCEQr'><span id='iCEQr'><b id='iCEQr'><form id='iCEQr'><ins id='iCEQr'></ins><ul id='iCEQr'></ul><sub id='iCEQr'></sub></form><legend id='iCEQr'></legend><bdo id='iCEQr'><pre id='iCEQr'><center id='iCEQr'></center></pre></bdo></b><th id='iCEQr'></th></span></q></dt></tr></i><div id='iCEQr'><tfoot id='iCEQr'></tfoot><dl id='iCEQr'><fieldset id='iCEQr'></fieldset></dl></div>