Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 为什么我的碰撞预测没有检测到任何碰撞?_Objective C_Collision Detection_Game Physics - Fatal编程技术网

Objective c 为什么我的碰撞预测没有检测到任何碰撞?

Objective c 为什么我的碰撞预测没有检测到任何碰撞?,objective-c,collision-detection,game-physics,Objective C,Collision Detection,Game Physics,创建项目符号对象时,使用.l表示位置,使用.vel表示速度。我试着用一个30的大半径,但它们从未碰撞过 - (void)bulletsFired:(NSArray *)bullets li:(int)li { [self playBulletSound:li]; for (Bullet *b in bullets) { [self addChild:b]; b.tag = -1; b.shotNumber = shotsFired

创建项目符号对象时,使用.l表示位置,使用.vel表示速度。我试着用一个30的大半径,但它们从未碰撞过

- (void)bulletsFired:(NSArray *)bullets li:(int)li {
    [self playBulletSound:li];

    for (Bullet *b in bullets) {
        [self addChild:b];
        b.tag = -1;
        b.shotNumber = shotsFired;
    }

    for (Bullet *b in bullets) {
        for (Bullet *bb in self.bullets) {
            float timeOfApproach = TimeOfClosestApproach(b.l, b.vel, bb.l, bb.vel, 30, 30);
            if (timeOfApproach > 0) {
                NSLog(@"time of : %f", timeOfApproach);
                NSString *keyName = [self.collisions objectForKey:[self keyNameForTime:(int)timeOfApproach]];
                NSMutableArray *timedCollisions = [self.collisions objectForKey:keyName];
                if (timedCollisions == nil) {
                    NSMutableArray *newCollisions = [NSMutableArray array];
                    [self.collisions setObject:newCollisions forKey:keyName];
                }

                NSDictionary *collision = @{@"b1" : [NSString stringWithFormat:@"%d", bb.shotNumber], @"b2" : [NSString stringWithFormat:@"%d", b.shotNumber]};

                [timedCollisions addObject:collision];
            }
        }
    }


    [self.bullets addObjectsFromArray:bullets];

    [self.scoreCycler score:1];
}
我使用此功能检查关闭时间方法:

float TimeOfClosestApproach(CGPoint Pa, CGPoint Pb, CGPoint Va, CGPoint Vb, float Ra, float Rb) {
    CGPoint Pab = ccpSub(Pa, Pb);
    CGPoint Vab = ccpSub(Va, Vb);
    float a = ccpDot(Vab, Vab);
    float b = 2 * ccpDot(Pab, Vab);
    float c = ccpDot(Pab, Pab) - (Ra + Rb) * (Ra + Rb);

    // The quadratic discriminant.
    float discriminant = b * b - 4 * a * c;

    // Case 1:
    // If the discriminant is negative, then there are no real roots, so there is no collision.  The time of
    // closest approach is then given by the average of the imaginary roots, which is:  t = -b / 2a
    float t;
    if (discriminant < 0)    {
        t = -b / (2 * a);
        return -1;
    } else {
        // Case 2 and 3:
        // If the discriminant is zero, then there is exactly one real root, meaning that the circles just grazed each other.  If the
        // discriminant is positive, then there are two real roots, meaning that the circles penetrate each other.  In that case, the
        // smallest of the two roots is the initial time of impact.  We handle these two cases identically.
        float t0 = (-b + (float)sqrt(discriminant)) / (2 * a);
        float t1 = (-b - (float)sqrt(discriminant)) / (2 * a);
        t = min(t0, t1);

        // We also have to check if the time to impact is negative.  If it is negative, then that means that the collision
        // occured in the past.  Since we're only concerned about future events, we say that no collision occurs if t < 0.
        if (t < 0) {
            return -1;
        } else {

        }
    }

    // Finally, if the time is negative, then set it to zero, because, again, we want this function to respond only to future events.
    if (t < 0) {
        t = 0;
    }

    return t;
}
我不断得到-1的回复,子弹永远不会相撞,即使根据我的视力,它们也不会相撞

    if (t < 0) {
        return -1;
    } else {

    }
不断被触发


我的timeOfClosestApproach函数有什么问题?

那么,在调试这段代码时,您观察到了什么但不明白的地方?例如,您在两个位置返回-1,可能t也会变为-1,这是哪一个?更新时会记录失败的位置。a、b和判别式的值是多少。。。如果你有这些并运行计算,一定要明白为什么它会导致t