Math XNA二维点对象随时间移动到光标

Math XNA二维点对象随时间移动到光标,math,xna,rotation,mouse,Math,Xna,Rotation,Mouse,此时,我在屏幕上正确地找到了对象和光标的距离和旋转 Vector2 direction = podType[podIndex]._Position - MouseCursorInWorld; mousePoint = (float)(Math.Atan2(-direction.X, direction.Y)); 这很好,下一步我想知道如何慢慢地将当前方向旋转到鼠标位置是使用百分比,这很好,但有一个主要问题,这就是我试图解决的问题 percent = mousePoint /

此时,我在屏幕上正确地找到了对象和光标的距离和旋转

Vector2 direction = podType[podIndex]._Position - MouseCursorInWorld; 
mousePoint = (float)(Math.Atan2(-direction.X, direction.Y));
这很好,下一步我想知道如何慢慢地将当前方向旋转到鼠标位置是使用百分比,这很好,但有一个主要问题,这就是我试图解决的问题

        percent = mousePoint / mousePoint * increment;
        if(percent < mousePoint)increment += 0.01f;
        if (percent > mousePoint) increment -= 0.01f;
percent=mousePoint/mousePoint*增量;
如果(百分比<鼠标点)增量+=0.01f;
如果(百分比>鼠标点)增量-=0.01f;
正如您在这里看到的,百分比是指向光标的总旋转的百分比,如果旋转小于或大于该百分比,则它将移动到该增量,直到达到100%,这意味着它正确面对光标

问题是因为左边是负数,右边是正数,我的完全旋转达到3.1和-3.1,所以当我将光标移动到底部某个位置,从最右边移动到最左边,而不是继续向左移动光标,它向右旋转,因为当前鼠标点值为负2.2,而当前为正1.5


有没有什么方法可以让旋转不带正负角?或者有没有比我现在使用的更好的技术?谢谢您的时间:)

我不太清楚您是否了解整个百分比,但据我所知,您希望对象旋转逐渐转向鼠标

尝试将目标旋转量包装在Pi和-Pi之间,您可以执行以下操作

           Vector2 dist = podType[podIndex]._Position - MouseCursorInWorld; 
           float angleTo = (float)Math.Atan2(dist.Y, dist.X); //angle you want to get to

           rotation = MathHelper.WrapAngle(rotation); // keeps angle between pi and -pi

            if (angleTo > rotation)
                while (angleTo - rotation > MathHelper.Pi)
                    angleTo -= MathHelper.TwoPi;
            else
                while (angleTo - rotation < -MathHelper.Pi)
                    angleTo += MathHelper.TwoPi;


            if (rotation < angleTo) rotation += 0.01f;
            if (rotation > angleTo) rotation -= 0.01f;
Vector2 dist=podType[podIndex]。\u位置-MouseCursorInWorld;
浮动角度TO=(浮动)数学值Atan2(距离Y,距离X)//你想要到达的角度
旋转=MathHelper.WrapAngle(旋转);//保持pi和-pi之间的角度
如果(角度>旋转)
while(angleTo-rotation>MathHelper.Pi)
angleTo-=MathHelper.TwoPi;
其他的
while(angleTo-rotation<-MathHelper.Pi)
angleTo+=MathHelper.TwoPi;
如果(旋转<角度)旋转+=0.01f;
如果(旋转>角度)旋转-=0.01f;
轮换
将是您当前的轮换。此外,如果您希望以两个数字之间的百分比获得值,我将研究
MathHelper.Lerp
(线性插值)

最后,您可以使用以下内容,而不是+或-0.01f

rotation=MathHelper.Lerp(旋转,角度为0.01f)


这将使你的旋转值向目标角度每帧增加1%。

正是我想要的。谢谢本杰明:D如果你对我为什么要这样做感兴趣,那是为了一个宇宙飞船游戏,我希望一些宇宙飞船旋转速度比其他宇宙飞船慢一点,我不希望他们立即这样做。再次感谢您的帮助:)