Reputation: 302
I want my spritenode
to have multiple physicbodies
. I used bodyWithbodies
and added the phyisicbodies
. But no collision is detected.
Below the method for creating the spritenode
which should have more physicbodies
.
-(void)spawnBrick
{
SKPhysicsBody *brickLeftEdge = [SKPhysicsBody bodyWithEdgeFromPoint:CGPointMake(brick.size.width - brick.size.width, brick.size.height - brick.size.height) toPoint:CGPointMake(brick.size.width - brick.size.width, brick.size.height)];
brickLeftEdge.categoryBitMask = brickLeftEdgeCategory;
brickLeftEdge.contactTestBitMask = leftEdgeCategory;
SKPhysicsBody *brickRightEdge = [SKPhysicsBody bodyWithEdgeFromPoint:CGPointMake(brick.size.width, brick.size.height - brick.size.height) toPoint:CGPointMake(brick.size.width, brick.size.height)];
brickRightEdge.categoryBitMask = brickRightEdgeCategory;
brickRightEdge.contactTestBitMask = rightEdgeCategory;
brick = [[BrickNodeOne alloc] init];
brick.name = @"Brick";
NSArray *array = [self objectForKeyedSubscript:@"Brick"];
BrickNodeOne *oldBrick;
if ([array count] > 0) {
oldBrick = [array objectAtIndex:([array count]-1)];
}
brick.position = CGPointMake(brick.size.width / 2, (brick.size.height + oldBrick.position.y)-15);
brick.physicsBody = [SKPhysicsBody bodyWithBodies:@[brickLeftEdge, brickRightEdge]];
brick.physicsBody.collisionBitMask = 0;
[self addChild:brick];
}
In DidMoveToView
I declared the left and right edge of the screen for collision detection with the spritenode
// setup edges
_leftEdge = [[SKNode alloc] init];
// heigt + 1000 = for spawned objects above screen
_leftEdge.physicsBody = [SKPhysicsBody bodyWithEdgeFromPoint:CGPointMake(0.0, 0.0) toPoint:CGPointMake(0.0, self.size.height + 1000)];
_leftEdge.physicsBody.dynamic = NO;
_leftEdge.physicsBody.categoryBitMask = leftEdgeCategory;
_leftEdge.physicsBody.contactTestBitMask = brickLeftEdgeCategory;
_leftEdge.position = CGPointZero;
[self addChild:_leftEdge];
_rightEdge = [[SKNode alloc] init];
// heigt + 1000 = for spawned objects above screen
_rightEdge.physicsBody = [SKPhysicsBody bodyWithEdgeFromPoint:CGPointMake(self.size.width, 0.0) toPoint:CGPointMake(self.size.width, self.size.height + 1000)];
_rightEdge.physicsBody.dynamic = NO;
_rightEdge.physicsBody.categoryBitMask = rightEdgeCategory;
_rightEdge.physicsBody.contactTestBitMask = brickRightEdgeCategory;
[self addChild:_rightEdge];
The following code contains the bitmask information:
I declared SKPhysicsContactDelegate
in the header file. Set the delegate in didMoveToView
static const uint32_t leftEdgeCategory = 0x1 << 0;
static const uint32_t rightEdgeCategory = 0x1 << 1;
static const uint32_t brickLeftEdgeCategory = 0x1 << 2;
static const uint32_t brickRightEdgeCategory = 0x1 << 3;
static const uint32_t kCarCategory = 0x1 << 4;
Information for comment
So I changed the physicsbody
from edge to rectangle, unfortunately no contact was detected.
SKPhysicsBody *brickLeftEdge = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(40, brick.size.height) center:CGPointMake((brick.size.width - brick.size.width) + 20 , self.size.height / 2)];
brickLeftEdge.categoryBitMask = brickLeftEdgeCategory;
brickLeftEdge.contactTestBitMask = leftEdgeCategory;
SKPhysicsBody *brickRightEdge = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(40, brick.size.height) center:CGPointMake(brick.size.width - 20, brick.size.height / 2)];
brickRightEdge.categoryBitMask = brickRightEdgeCategory;
brickRightEdge.contactTestBitMask = rightEdgeCategory;
Upvotes: 1
Views: 110
Reputation: 13665
The problem you have is related to the fact that you are using static physics bodies. This is from the docs about bodyWithEdgeFromPoint: toPoint method:
An edge has no volume or mass and is always treated as if the dynamic property is equal to NO. Edges may only collide with volume-based physics bodies.
Means, collisions can happen only when at least one body is defined as dynamic (volume-based physics bodies are dynamic by default).
Upvotes: 1