1
-(CGPoint)Rule2:(Boid*)b
    {
        CGPoint v = CGPointMake(0, 0);

        for (Boid *boid in ActiveBoids)
        {
            if (boid != b)
            {
                NSLog(@"%f", [Utilities Magnitude:boid.position] - [Utilities Magnitude:b.position]);
                if(([Utilities Magnitude:boid.position] - [Utilities Magnitude:b.position]) < 150)
                {
                    v = [Utilities MinusVector:v Vector2:CGPointMake(boid.position.x - b.position.x, boid.position.y - b.position.y)];
                    NSLog(@"%f", v.x);
                    NSLog(@"%f", v.y);
                }
            }
        }
        return v;
    }

我的幅度和减法都可以正常工作:

+ (CGPoint)MinusVector:(CGPoint)v1 Vector2:(CGPoint)v2
{
    return CGPointMake(v1.x - v2.x, v1.y - v2.y);
}

+ (float)Magnitude:(CGPoint)p1
{
    return hypot(p1.x, p1.y);
}

出于某种原因,这是我从该方法得到的输出:

错误日志

我已经看这个太久了,看不出有什么问题。有任何想法吗?

4

1 回答 1

0

看来您的情况不正确。您正在考虑幅度的差异,但您想要差异的幅度。我想你想要这个:

if ([Utilities Magnitude:CGPointMake (boid.position - b.position)] < 150)

等等

于 2014-03-07T04:12:39.770 回答