0

In this project there are two players and both can shoot. When the opposite players bullets hits a ship, it lowers their health and disappears, this works fine. The problem is I don't want either the ships or the bullets to react to colliding into each other.

Here is the code for making the first player:

player = [SKSpriteNode spriteNodeWithImageNamed:filePath];
    player.size = CGSizeMake(100, 100);
    player.position = CGPointMake(150, 250);
    player.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:player.size];
    player.physicsBody.categoryBitMask = playerCategory;
    player.physicsBody.collisionBitMask = wallCategory;
    player.physicsBody.friction = 1.0f;
    player.physicsBody.restitution = 1.0f;
    player.physicsBody.linearDamping = 1.0f;
    player.physicsBody.allowsRotation = NO;
    [self addChild:player];

And second player:

playerTwo = [SKSpriteNode spriteNodeWithImageNamed:filePath];
        playerTwo.size = CGSizeMake(100, 100);
        playerTwo.position = CGPointMake((384*2)-150, (512*2)-250);
        playerTwo.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:playerTwo.size];
        playerTwo.physicsBody.categoryBitMask = playerTwoCategory;
        playerTwo.physicsBody.collisionBitMask = wallCategory;
        playerTwo.physicsBody.friction = 1.0f;
        playerTwo.physicsBody.restitution = 1.0f;
        playerTwo.physicsBody.linearDamping = 1.0f;
        playerTwo.physicsBody.allowsRotation = NO;
        [self addChild:playerTwo];

the initializing of the categories:

static const uint32_t playerCategory = 2;
static const uint32_t playerTwoCategory =   5;
static const uint32_t wallCategory =   1;
static const uint32_t bulletCategory =   4;
static const uint32_t bulletTwoCategory =   7;

The bullet making for player one:

    SKSpriteNode * bullet = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor]     size:CGSizeMake(5, 5)];
    bullet.position =  [gunOne convertPoint:CGPointMake(0,0) toNode:self];
    [self addChild:bullet];
    bullet.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:bullet.frame.size];
    bullet.physicsBody.categoryBitMask = bulletCategory;
    bullet.physicsBody.dynamic = YES;
    bullet.physicsBody.contactTestBitMask = playerTwoCategory | wallCategory;
    bullet.physicsBody.collisionBitMask = wallCategory;
    bullet.physicsBody.usesPreciseCollisionDetection = YES;

The bullet making for player two:

    SKSpriteNode * bulletTwo = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:CGSizeMake(5, 5)];
    bulletTwo.position =  [gunOneTwo convertPoint:CGPointMake(0,0) toNode:self];
    [self addChild:bulletTwo];
    bulletTwo.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:bulletTwo.frame.size];
    bulletTwo.physicsBody.categoryBitMask = bulletTwoCategory;
    bulletTwo.physicsBody.dynamic = YES;
    bulletTwo.physicsBody.contactTestBitMask = playerCategory | wallCategory;
    bulletTwo.physicsBody.collisionBitMask = wallCategory;
    bulletTwo.physicsBody.usesPreciseCollisionDetection = YES;

Is there any visible problem with the way this is done that would cause JUST the first player and the bullets react to collisions? Thanks in advance for the help, and sorry for the long read.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Lucas Bullen
  • 211
  • 5
  • 14

2 Answers2

0

The 'static const uint32_t' is the problem, they in fact stat the order of the categories, instead What you must do is value the ships the same number and high then the bullets, which also must be the same number. To differentiate between them in the didBeginContact, give each a name and use that instead.

Lucas Bullen
  • 211
  • 5
  • 14
0

I would define the bits and masks like this:

#define BIT_WALL        (0x01 << 0)   // 0000 0001
#define BIT_PLAYER_1    (0x01 << 1)   // 0000 0010
#define BIT_PLAYER_2    (0x01 << 2)   // 0000 0100
#define BIT_BULLET_1    (0x01 << 3)   // 0000 1000
#define BIT_BULLET_2    (0x01 << 4)   // 0001 0000

// Just Bit groups for players and bullets
#define MASK_PLAYERS    (BIT_PLAYER_1 | BIT_PLAYER_2)  // 0000 0110
#define MASK_BULLETS    (BIT_BULLET_1 | BIT_BULLET_2)  // 0001 1000

// Players and bullets collide with wall, but not with each other
#define COLLISION_MASK_WALL       (MASK_PLAYERS | MASK_BULLETS) 
#define COLLISION_MASK_PLAYER_1   (BIT_WALL)
#define COLLISION_MASK_PLAYER_2   (BIT_WALL)
#define COLLISION_MASK_BULLET_1   (BIT_WALL)
#define COLLISION_MASK_BULLET_2   (BIT_WALL)

// Contact is detected if anything hits wall, or, if bullet1 hits player2 (and vice versa)
#define CONTACT_MASK_WALL         (MASK_PLAYERS | MASK_BULLETS)
#define CONTACT_MASK_PLAYER_1     (BIT_WALL | BIT_BULLET_2)
#define CONTACT_MASK_PLAYER_2     (BIT_WALL | BIT_BULLET_1)
#define CONTACT_MASK_BULLET_1     (BIT_WALL | BIT_PLAYER_2)
#define CONTACT_MASK_BULLET_2     (BIT_WALL | BIT_PLAYER_1)


// Creating objects
{
    wall.physicsBody.categoryBitMask = BIT_WALL;
    wall.physicsBody.collisionBitMask = COLLISION_MASK_WALL;
    wall.physicsBody.contactTestBitMask = CONTACT_MASK_WALL;

    player1.physicsBody.categoryBitMask = BIT_PLAYER_1;
    player1.physicsBody.collisionBitMask = COLLISION_MASK_PLAYER_1;
    player1.physicsBody.contactTestBitMask = CONTACT_MASK_PLAYER_1;

    bullet1.physicsBody.categoryBitMask = BIT_BULLET_1;
    bullet1.physicsBody.collisionBitMask = COLLISION_MASK_BULLET_1;
    bullet1.physicsBody.contactTestBitMask = CONTACT_MASK_BULLET_1;

    player2.physicsBody.categoryBitMask = BIT_PLAYER_2;
    player2.physicsBody.collisionBitMask = COLLISION_MASK_PLAYER_2;
    player2.physicsBody.contactTestBitMask = CONTACT_MASK_PLAYER_2;

    bullet2.physicsBody.categoryBitMask = BIT_BULLET_2;
    bullet2.physicsBody.collisionBitMask = COLLISION_MASK_BULLET_2;
    bullet2.physicsBody.contactTestBitMask = CONTACT_MASK_BULLET_2;
}
JKallio
  • 903
  • 5
  • 15