如何逐步旋转对象面对新的转向最短距离 [英] How to gradually rotate an object to face another turning the shortest distance

查看:136
本文介绍了如何逐步旋转对象面对新的转向最短距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前想这取决于有多少度(或拉德,我宁愿度)旋转精灵它不同于直接正对着一个目标,问题是当目标达到一定位置的精灵决定做旋转完整的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:

蓝方=目标

红场=对象

绿线=旋转就是了

黑线=当前旋转

布朗箭头=它的旋转方式实现这一

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.

检查所有的情况下分开。

Check all cases 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;
}






方法2



从看图片,你可能会意识到你可能只是检查是否在右边的对象,那么如果没有,假设它是在左边(因为只要目前的角度小于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;
}

这篇关于如何逐步旋转对象面对新的转向最短距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆