iosobjective-csprite-kitboids

Flocking separation rule


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

My magnitude and subtract methods both work fine:

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

For some reason this is my output from that method:

Error Log

I've been looking at this for too long to see what's wrong. Any ideas?


Solution

  • It looks like your condition is incorrect. You're taking the difference of the magnitudes, but you want the magnitude of the difference. I think you want this:

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

    etc.