Sprite kit 在SpriteKit中检测碰撞,具有相同位掩码的对象?

Sprite kit 在SpriteKit中检测碰撞,具有相同位掩码的对象?,sprite-kit,Sprite Kit,我是SpriteKit新手,似乎不知道如何检测具有相同位掩码的两个或多个对象之间的碰撞。在我的游戏中,汽车从左/右和上/下进入场景。为了得分,我需要能够跟踪汽车碰撞的情况。现在,车辆交叉和碰撞(左/右车1从上/下)但不是同一车道上的车辆(左车1从左车2从左高速)。相反,它们最终只是相互穿行。代码如下: 首先,我在Car的开头设置位掩码。m: typedef enum : NSUInteger { vehicleCategory = (1 << 0),

我是SpriteKit新手,似乎不知道如何检测具有相同位掩码的两个或多个对象之间的碰撞。在我的游戏中,汽车从左/右和上/下进入场景。为了得分,我需要能够跟踪汽车碰撞的情况。现在,车辆交叉和碰撞(左/右车1从上/下)但不是同一车道上的车辆(左车1从左车2从左高速)。相反,它们最终只是相互穿行。代码如下:

首先,我在Car的开头设置位掩码。m:

typedef enum : NSUInteger {
    vehicleCategory         = (1 << 0),
    environmentCategory     = (1 << 1),
} NodeCategory;
当两辆车在同一车道上相撞时,有没有办法检测?现在我的didBeginContact是相当赤裸裸的;只是一些关于节点及其属性的NSLog语句。我从这里学到了很多,我很兴奋,我现在真的有一个问题要问。

根据for
SKPhysicsBody
path
参数对于
bodyWithPolygonFromPath:
方法被描述为:

“具有逆时针缠绕且无自相交的多边形路径。点是相对于所属节点的原点指定的。”

请确保您创建的路径符合上述描述


这里有一个关于凸多边形定义的例子。

谢谢大家,在阅读了如何正确设置后,这条路径有点可笑。清理之后,碰撞效果很好。
- (Car*)spawnCarWithColor:(NSUInteger)color andSpeed:(NSUInteger)speed screenEdge:(NSUInteger)screenEdge afterDelay:(CGFloat)delay andScale:(CGFloat)carScale {

    NSArray *carColorsAssets = [NSArray arrayWithObjects:@"blueCar", @"redCar", @"greenCar", nil];

    carColorsForDisplay = [NSArray arrayWithObjects:@"blue", @"red", @"green", nil];

    edgeOfScreenForDisplay = [NSArray arrayWithObjects:@"left", @"top", @"right", @"bottom", nil];

    Car *newCar = [Car spriteNodeWithImageNamed:[carColorsAssets objectAtIndex:color]];


//    set CGPath for phyics

    CGFloat offsetX = newCar.frame.size.width * newCar.anchorPoint.x;
    CGFloat offsetY = newCar.frame.size.height * newCar.anchorPoint.y;

    CGMutablePathRef path = CGPathCreateMutable();

    CGPathMoveToPoint(path, NULL, 0 - offsetX, 24 - offsetY);
    CGPathAddLineToPoint(path, NULL, 0 - offsetX, 21 - offsetY);
    CGPathAddLineToPoint(path, NULL, 0 - offsetX, 18 - offsetY);
    CGPathAddLineToPoint(path, NULL, 1 - offsetX, 16 - offsetY);
    CGPathAddLineToPoint(path, NULL, 1 - offsetX, 13 - offsetY);
    CGPathAddLineToPoint(path, NULL, 3 - offsetX, 11 - offsetY);
    CGPathAddLineToPoint(path, NULL, 5 - offsetX, 8 - offsetY);
    CGPathAddLineToPoint(path, NULL, 6 - offsetX, 6 - offsetY);
    CGPathAddLineToPoint(path, NULL, 8 - offsetX, 5 - offsetY);
    CGPathAddLineToPoint(path, NULL, 11 - offsetX, 3 - offsetY);
    CGPathAddLineToPoint(path, NULL, 14 - offsetX, 3 - offsetY);
    CGPathAddLineToPoint(path, NULL, 16 - offsetX, 3 - offsetY);
    CGPathAddLineToPoint(path, NULL, 18 - offsetX, 3 - offsetY);
    CGPathAddLineToPoint(path, NULL, 20 - offsetX, 3 - offsetY);
    CGPathAddLineToPoint(path, NULL, 23 - offsetX, 3 - offsetY);
    CGPathAddLineToPoint(path, NULL, 25 - offsetX, 3 - offsetY);
    CGPathAddLineToPoint(path, NULL, 28 - offsetX, 3 - offsetY);
    CGPathAddLineToPoint(path, NULL, 29 - offsetX, 3 - offsetY);
    CGPathAddLineToPoint(path, NULL, 34 - offsetX, 4 - offsetY);
    CGPathAddLineToPoint(path, NULL, 37 - offsetX, 4 - offsetY);
    CGPathAddLineToPoint(path, NULL, 40 - offsetX, 4 - offsetY);
    CGPathAddLineToPoint(path, NULL, 44 - offsetX, 4 - offsetY);
    CGPathAddLineToPoint(path, NULL, 48 - offsetX, 4 - offsetY);
    CGPathAddLineToPoint(path, NULL, 50 - offsetX, 4 - offsetY);
    CGPathAddLineToPoint(path, NULL, 54 - offsetX, 4 - offsetY);
    CGPathAddLineToPoint(path, NULL, 56 - offsetX, 4 - offsetY);
    CGPathAddLineToPoint(path, NULL, 60 - offsetX, 3 - offsetY);
    CGPathAddLineToPoint(path, NULL, 63 - offsetX, 3 - offsetY);
    CGPathAddLineToPoint(path, NULL, 66 - offsetX, 3 - offsetY);
    CGPathAddLineToPoint(path, NULL, 69 - offsetX, 3 - offsetY);
    CGPathAddLineToPoint(path, NULL, 72 - offsetX, 3 - offsetY);
    CGPathAddLineToPoint(path, NULL, 74 - offsetX, 3 - offsetY);
    CGPathAddLineToPoint(path, NULL, 77 - offsetX, 3 - offsetY);
    CGPathAddLineToPoint(path, NULL, 80 - offsetX, 3 - offsetY);
    CGPathAddLineToPoint(path, NULL, 83 - offsetX, 3 - offsetY);
    CGPathAddLineToPoint(path, NULL, 86 - offsetX, 3 - offsetY);
    CGPathAddLineToPoint(path, NULL, 88 - offsetX, 3 - offsetY);
    CGPathAddLineToPoint(path, NULL, 91 - offsetX, 4 - offsetY);
    CGPathAddLineToPoint(path, NULL, 93 - offsetX, 6 - offsetY);
    CGPathAddLineToPoint(path, NULL, 95 - offsetX, 7 - offsetY);
    CGPathAddLineToPoint(path, NULL, 96 - offsetX, 10 - offsetY);
    CGPathAddLineToPoint(path, NULL, 97 - offsetX, 12 - offsetY);
    CGPathAddLineToPoint(path, NULL, 98 - offsetX, 15 - offsetY);
    CGPathAddLineToPoint(path, NULL, 98 - offsetX, 18 - offsetY);
    CGPathAddLineToPoint(path, NULL, 98 - offsetX, 21 - offsetY);
    CGPathAddLineToPoint(path, NULL, 98 - offsetX, 24 - offsetY);
    CGPathAddLineToPoint(path, NULL, 98 - offsetX, 26 - offsetY);
    CGPathAddLineToPoint(path, NULL, 98 - offsetX, 29 - offsetY);
    CGPathAddLineToPoint(path, NULL, 98 - offsetX, 32 - offsetY);
    CGPathAddLineToPoint(path, NULL, 97 - offsetX, 35 - offsetY);
    CGPathAddLineToPoint(path, NULL, 96 - offsetX, 39 - offsetY);
    CGPathAddLineToPoint(path, NULL, 95 - offsetX, 41 - offsetY);
    CGPathAddLineToPoint(path, NULL, 93 - offsetX, 44 - offsetY);
    CGPathAddLineToPoint(path, NULL, 90 - offsetX, 45 - offsetY);
    CGPathAddLineToPoint(path, NULL, 85 - offsetX, 46 - offsetY);
    CGPathAddLineToPoint(path, NULL, 81 - offsetX, 46 - offsetY);
    CGPathAddLineToPoint(path, NULL, 76 - offsetX, 46 - offsetY);
    CGPathAddLineToPoint(path, NULL, 72 - offsetX, 46 - offsetY);
    CGPathAddLineToPoint(path, NULL, 67 - offsetX, 46 - offsetY);
    CGPathAddLineToPoint(path, NULL, 63 - offsetX, 46 - offsetY);
    CGPathAddLineToPoint(path, NULL, 58 - offsetX, 46 - offsetY);
    CGPathAddLineToPoint(path, NULL, 53 - offsetX, 46 - offsetY);
    CGPathAddLineToPoint(path, NULL, 47 - offsetX, 46 - offsetY);
    CGPathAddLineToPoint(path, NULL, 42 - offsetX, 46 - offsetY);
    CGPathAddLineToPoint(path, NULL, 39 - offsetX, 46 - offsetY);
    CGPathAddLineToPoint(path, NULL, 35 - offsetX, 46 - offsetY);
    CGPathAddLineToPoint(path, NULL, 31 - offsetX, 46 - offsetY);
    CGPathAddLineToPoint(path, NULL, 27 - offsetX, 46 - offsetY);
    CGPathAddLineToPoint(path, NULL, 23 - offsetX, 46 - offsetY);
    CGPathAddLineToPoint(path, NULL, 19 - offsetX, 46 - offsetY);
    CGPathAddLineToPoint(path, NULL, 16 - offsetX, 46 - offsetY);
    CGPathAddLineToPoint(path, NULL, 12 - offsetX, 46 - offsetY);
    CGPathAddLineToPoint(path, NULL, 8 - offsetX, 45 - offsetY);
    CGPathAddLineToPoint(path, NULL, 5 - offsetX, 43 - offsetY);
    CGPathAddLineToPoint(path, NULL, 3 - offsetX, 40 - offsetY);
    CGPathAddLineToPoint(path, NULL, 1 - offsetX, 35 - offsetY);

    CGPathCloseSubpath(path);

    newCar.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path];
    newCar.physicsBody.contactTestBitMask = vehicleCategory;
    newCar.physicsBody.collisionBitMask = vehicleCategory;
    newCar.physicsBody.categoryBitMask = vehicleCategory;
    newCar.physicsBody.restitution = 1.0;
    newCar.physicsBody.dynamic = YES;
    newCar.physicsBody.usesPreciseCollisionDetection = TRUE;
    newCar.scale = carScale;
    newCar.zPosition = 2;
    newCar.carColor = [carColorsForDisplay objectAtIndex:color];
    newCar.edgeOfScreen = [edgeOfScreenForDisplay objectAtIndex:screenEdge];
    newCar.vehicleType = @"car";

    switch (screenEdge) {
        case 0:
            newCar.zRotation = M_PI;
            newCar.position = CGPointMake(-100, 512);
            carVelocity = CGVectorMake(speed, 0);
            leftEdgeCarTotal++;
            carName = [NSString stringWithFormat:@"left%i", leftEdgeCarTotal];
            [leftEdgeCars addObject:carName];
            newCar.name = carName;
            newCar.edgeOfScreen = @"left";
            break;

        case 1:
            newCar.zRotation = M_PI/2;
            newCar.position = CGPointMake(352, 868);
            carVelocity = CGVectorMake(0, -speed);
            topEdgeCarTotal++;
            carName = [NSString stringWithFormat:@"top%i", topEdgeCarTotal];
            [topEdgeCars addObject:carName];
            newCar.name = carName;
            break;

        case 2:
            newCar.position = CGPointMake(1124, 545);
            carVelocity = CGVectorMake(-speed, 0);
            rightEdgeCarTotal++;
            carName = [NSString stringWithFormat:@"right%i", rightEdgeCarTotal];
            [rightEdgeCars addObject:carName];
            newCar.name = carName;
            break;

        case 3:
            newCar.zRotation = -0.5*M_PI;
            newCar.position = CGPointMake(415, -100);
            carVelocity = CGVectorMake(0, speed);
            bottomEdgeCarTotal++;
            carName = [NSString stringWithFormat:@"bottom%i", bottomEdgeCarTotal];
            [bottomEdgeCars addObject:carName];
            newCar.name = carName;
            break;

        default:
            break;
    }

    newCar.physicsBody.velocity = carVelocity;

    NSLog(@"Spawned a %@ %@ from the %@, Speed of %i after a %g sec delay", [carColorsForDisplay objectAtIndex:color], newCar.vehicleType, [edgeOfScreenForDisplay objectAtIndex:screenEdge], speed, delay);

    return newCar;

}