Objective c 如何知道何时增加或减少角度以获得特定角度?

Objective c 如何知道何时增加或减少角度以获得特定角度?,objective-c,math,geometry,Objective C,Math,Geometry,我正在编写一个游戏,我遇到了一个非常困难的地方。基本上我有一个圆,这个圆上有两个角。角度1(A)是我希望角度2(B)指向的点。在我的游戏中,每一帧我都需要检查是否增加或减少一定的角度值(速度),以最终达到第一个角度。我的问题是我如何做到这一点 我试过这么做,但似乎做得不对 bool increase = false; float B = [self radiansToDegrees:tankAngle]; float A = [self radiansToDegrees:tankDestina

我正在编写一个游戏,我遇到了一个非常困难的地方。基本上我有一个圆,这个圆上有两个角。角度1(A)是我希望角度2(B)指向的点。在我的游戏中,每一帧我都需要检查是否增加或减少一定的角度值(速度),以最终达到第一个角度。我的问题是我如何做到这一点

我试过这么做,但似乎做得不对

bool increase = false;

float B = [self radiansToDegrees:tankAngle];
float A = [self radiansToDegrees:tankDestinationAngle];
float newAngle = B;

if(B < A) {

    float C = B - (360 - A);
    float D = A - B;

    if(C < D) increase = false;
    else increase = true;

} else if(B > A) {

    float C = B - A;
    float D = A - (360 - B);

    if(C < D) increase = false;
    else increase = true;

}

if(increase) {
    newAngle += 1.0;
} else {
    newAngle -= 1.0;
}

if(newAngle > 360.0) {
    newAngle = 0 + (newAngle - 360.0);
} else if(newAngle < 0.0) {
    newAngle = 360 + newAngle;
}

if(newAngle == 0) newAngle = 360;

newAngle = [self degreesToRadians:newAngle];

[self setTanksProperPositionRotation:newAngle];
bool-increase=false;
浮动B=[自辐射度:桶角];
浮点数A=[自辐射度:tankDestinationAngle];
浮动角度=B;
if(BA){
浮点数C=B-A;
浮动D=A-(360-B);
如果(C360.0){
新角度=0+(新角度-360.0);
}否则如果(新角度<0.0){
新角度=360+新角度;
}
如果(newAngle==0)newAngle=360;
newAngle=[自度数弧度:newAngle];
[自设置油箱正确位置旋转:新角度];

我试图达到的基本效果是当用户创建一个新点时,即角度1,角度2将朝角度1移动,选择最快的方向。我想我已经花了大约4个小时试图弄明白这一点。

基本上,你要检查哪个角度给你的长度最小(L)


标准化0到360度之间的角度,取较小者:

float normalize(float angle)
{
    while(angle < 0)
        angle += 360;
    return angle % 360;
}

//To use...
float angle1 = A - B;
float angle2 = B - A;
if(normalize(angle1) < normalize(angle2))
    //Use angle1
else
    //Use angle2
浮动规格化(浮动角度)
{
而(角度<0)
角度+=360;
返回角%360;
}
//使用。。。
浮动角度1=A-B;
浮动角度2=B-A;
if(标准化(角度1)<标准化(角度2))
//使用角度1
其他的
//使用角度2

假设当前和期望值为正且小于360:

float inc; // abs. distance from current to desired if incrementing
float dec; // abs. distance from current to desired if decrementing

if (current > desired)
{
    inc = current + 360.0f - desired; // incrementing would wrap over
    dec = current - desired;
}
else
{
    inc = desired - current;
    dec = current + 360.0f - desired; // decrementing would wrap over
}

// the expressions above are arranged so inc and dec are both +ve
// so just compare them
if (inc < dec)
    newAngle = current + step;
else 
    newAngle = current - step;
float公司;//防抱死制动系统。从当前到所需的距离(如果递增)
浮动十二月;//防抱死制动系统。从电流到所需的距离(如果递减)
如果(当前>所需)
{
inc=当前+360.0f-所需;//递增将结束
dec=当前-期望值;
}
其他的
{
inc=所需电流;
dec=当前+360.0f-所需;//递减将结束
}
//以上表达式的排列方式是inc和dec都是+ve
//那就比较一下吧
如果(inc
太棒了,谢谢。我刚弄明白等式中的第一个符号是什么意思。@Thomas:我很高兴能帮上忙。你是说
a
?它是以度为单位测量的角度。如果角度以弧度表示,则类型更简单:
L=theta*r
这个答案没有用。因为r、pi和180都是常数,所以你基本上是说“使用较小的角度”——这就是他遇到的问题!