植绒分离规则

时间:2014-03-07 03:57:09

标签: ios objective-c sprite-kit boids

-(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);
}

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

Error Log

我一直在看这个问题太久没看错了。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

看起来你的情况不正确。你正在考虑大小的差异,但你想要差异的大小。我想你想要这个:

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