<bdo id='pBx8S'></bdo><ul id='pBx8S'></ul>
  • <tfoot id='pBx8S'></tfoot>

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

      2. <small id='pBx8S'></small><noframes id='pBx8S'>

        <legend id='pBx8S'><style id='pBx8S'><dir id='pBx8S'><q id='pBx8S'></q></dir></style></legend>

        如何逐渐旋转一个物体以面对另一个转动最短距离

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

                <tbody id='YMl0S'></tbody>

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

              2. <tfoot id='YMl0S'></tfoot>
                • 本文介绍了如何逐渐旋转一个物体以面对另一个转动最短距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我目前正在尝试根据与直接面向目标的度数(或弧度,我更喜欢度数)不同来旋转精灵,问题是当目标到达特定位置时精灵决定旋转一个完整的 360 到其他方式,而不是做额外的 10 个.这张图片可能更好地解释了这个问题:

                  I'm currently trying to rotate a sprite depending on how many degrees(or rads, I prefer degrees) it differs from facing straight towards a target, the problem is when the target reaches a certain position the sprites decides to do rotate a full 360 to other way instead of doing the 10 extra. This picture probably explains the problem better:

                  蓝色方块 = 目标

                  红色方块 = 物体

                  绿线 = 它想要的旋转

                  Green line = rotation it wants

                  黑线 = 当前旋转

                  棕色箭头 = 它如何旋转来实现这一点

                  Brown arrow = how it rotates to achieve this

                  红色箭头 = 我希望它如何旋转.

                  Red arrow = how I want it to rotate.

                  请注意,案例 1 始终有效,具体取决于它的旋转方式,但案例 2 始终执行该旋转,无论它是在当前旋转的右侧"还是左侧".

                  Note that Case 1 always work, depending what way it rotates, but Case 2 it always does that rotation, no matter if it is to the "right" or "left" of the current rotation.

                  这是我用来旋转对象的代码.

                  This is the code I'm using to rotate the object.

                      Vector2 distance = new Vector2(target.worldPos.X - this.worldPos.X, target.worldPos.Y - this.worldPos.Y);
                      float wantRot = (float)Math.Atan2(distance.Y, distance.X);
                      if (this.rotation < wantRot)
                          this.rotation += MathHelper.ToRadians(45) * Time.deltaTime;
                      if (this.rotation > wantRot)
                          this.rotation -= MathHelper.ToRadians(45) * Time.deltaTime;
                  

                  我想要实现的是让它根据红色箭头而不是棕色箭头旋转(在案例 2 中).

                  What i want to achieve is have it rotate (in Case 2) according to the red arrow instead of the brown one.

                  注意:我不是编程方面的专家,我只是在过去的一年中不时进行过编程(主要是简单的 2D 射击/射击类游戏),因此将不胜感激.我也是一名学习编程的学生.

                  NOTE: I'm not an expert at programming, I've only done it from time to time for the past year(Mainly simple 2D Shooter/shoot'em up kind of games), so in depth explanation would be appreciated. I'm also a student learning programming.

                  PS:标题的建议也将不胜感激,因为我完全不知道该放什么.

                  PS: Suggestions for Title would also be appreciated as I had absolutely no idea what to put there.

                  推荐答案

                  你的问题是,目标可能在 5 角,对象可能面向 355 度(例如).根据你的测试,5小于355,所以逆时针走.

                  Your problem is that the target could be at angle 5, and the object could be facing 355 degrees (for example). According to your test, 5 is less than 355, so go anticlockwise.

                  你应该做的是测试目标是在你左边180度以内,还是在你右边180度以内,然后相应地移动.

                  What you should do is test whether the target is within 180 degrees to your left, or within 180 degrees to your right, then move accordingly.

                  棘手的部分是让检查在 360 <-> 0 周围环绕".看起来你的情况还剩下 0 度,所以当wantRot 位于 0 度的一侧时,硬测试是在里面.

                  The tricky part is getting the check to 'wrap' around 360 <-> 0. It looks like 0 degrees is left in your case, so the hard test is for when the wantRot is on the side that has 0 degrees within it.

                  如下图所示画一个圆圈,然后将您的对象放在我们所面对的左侧.您会看到必须分别检查 2 个阴影区域.

                  To visualise draw a circle as below, then place your object on the left of where we're facing. You'll see that you have to check the 2 shaded areas separately.

                  分别检查所有案例.

                  注意:下面的代码在我的脑海中,未经测试.您需要将度数更改为弧度.

                  Note: Code below is in my head and untested. You'll need to change degrees to radians.

                  int MoveDir = 0;
                  var BehindMe = this.rotation - 180;
                  if (BehindMe < 0)
                      BehindMe += 360;
                  
                  if (wantRot != this.rotation)
                  {
                      if (wantRot == BehindMe)
                          MoveDir = 1; // or randomly choose
                      else if ((wantRot > BehindMe && wantRot < this.rotation) ||
                               (this.rotation < 180 && (wantRot > BehindMe ||
                                                        wantRot < this.rotation)))
                          MoveDir = -1;
                      else if ((wantRot < BehindMe && wantRot > this.rotation) ||
                               (this.rotation > 180 && (wantRot < BehindMe ||
                                                        wantRot > this.rotation))
                          MoveDir= 1;
                  
                      this.rotation += MoveDir * MathHelper.ToRadians(45) * Time.deltaTime;
                  }
                  

                  <小时>

                  方法二

                  看图片,你可能会发现你可以只检查物体是否在右边,如果不是,就假设它在左边(因为只要当前角度小于 180 度,检查它在对的很简单).如果当前角度超过 180 度,则颠倒这个概念 - 检查它是否在左侧,如果不是则假设在右侧.如下所示:


                  Method 2

                  From looking at the image, you may realise that you could just check whether the object on the right, then if not, assume it's on the left (since as long as the current angle is less than 180 degrees checking its on the right is easy). If the current angle is more than 180 degrees, then reverse the concept - check whether it's on the left and if not assume right. Something like below:

                  int MoveDir = 0;
                  var BehindMe = this.rotation - 180;
                  if (BehindMe < 0)
                      BehindMe += 360;
                  
                  if (wantRot != this.rotation)
                  {
                      if (this.rotation <= 180)
                      {
                          if (wantRot > this.rotation && wanrRot < BehindMe)
                              MoveDir = 1;
                          else
                              MoveDir = -1;
                      }
                      else
                      {
                          if (wantRot < this.rotation && wanrRot > BehindMe)
                              MoveDir = -1;
                          else
                              MoveDir = 1;
                      }
                  
                      this.rotation += MoveDir * MathHelper.ToRadians(45) * Time.deltaTime;
                  }
                  

                  这篇关于如何逐渐旋转一个物体以面对另一个转动最短距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:C#中的旋转文本对齐 下一篇:Unity - 任意角度之间的夹子旋转

                  相关文章

                • <legend id='8RP0W'><style id='8RP0W'><dir id='8RP0W'><q id='8RP0W'></q></dir></style></legend>
                • <i id='8RP0W'><tr id='8RP0W'><dt id='8RP0W'><q id='8RP0W'><span id='8RP0W'><b id='8RP0W'><form id='8RP0W'><ins id='8RP0W'></ins><ul id='8RP0W'></ul><sub id='8RP0W'></sub></form><legend id='8RP0W'></legend><bdo id='8RP0W'><pre id='8RP0W'><center id='8RP0W'></center></pre></bdo></b><th id='8RP0W'></th></span></q></dt></tr></i><div id='8RP0W'><tfoot id='8RP0W'></tfoot><dl id='8RP0W'><fieldset id='8RP0W'></fieldset></dl></div>

                    <tfoot id='8RP0W'></tfoot>

                      <bdo id='8RP0W'></bdo><ul id='8RP0W'></ul>

                    <small id='8RP0W'></small><noframes id='8RP0W'>