문제

-(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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top