为什么我的碰撞预测未检测到任何碰撞?

时间:2014-12-25 04:38:23

标签: objective-c collision-detection game-physics

使用.l创建Bullet对象以获取位置,使用.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];
}

我使用此函数检查timeOfClosestApproach:

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函数出了什么问题?

0 个答案:

没有答案