Domanda

Firstly I added

skView.showsPhysics = YES;

To see physics body circuit.

BUT

I have a problem with loosing physics effect O_o... I have ball which affected by gravity and falls down.

// Ball     
self.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:self.size.height/2];
        self.physicsBody.categoryBitMask = ballCategory;
        self.physicsBody.contactTestBitMask = bottomCategory;

Also I've created bottom edged physical body to get collision messages:

// Bottom edge 
    CGRect bottomRect = CGRectMake(self.frame.origin.x, self.frame.origin.y + 25, self.frame.size.width, 10);
        SKNode* bottom = [SKNode node];
        bottom.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:bottomRect];
        [self addChild:bottom];

which correctly interacted with each other before I added bitMask to bottom edge

bottom.physicsBody.categoryBitMask = bottomCategory;
bottom.physicsBody.collisionBitMask = ballCategory;

Masks:

static const uint32_t ballCategory  = 0x1 << 0;  // 00000000000000000000000000000001
static const uint32_t bottomCategory = 0x1 << 1; // 00000000000000000000000000000010

It stopped work. Hm...

I commented out assigning mask to bottom edge - it works. Then I assigned to bottom edge another - ball mask - it doesn't work again.

I guess, I missed something in documentation but now I can't find answer why it happens.

enter image description here

È stato utile?

Soluzione

bottom.physicsBody.categoryBitMask = bottomCategory;

This makes bottom only contact/collide with bodies whose contactBitMask contains the bottomCategory bit.

So if you add this category to your balls they should collide with the bottom again:

ball.physicsBody.contactTestBitMask = ballCategory | bottomCategory;

For more details refer to the Box2D manual Chapter 7.2 - Filtering.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top