Question

I have 2 images without background with triangles, I want connect one triangle with another, but Sprite kit thinking that my images is square not a triangle, and call me didBeginContact not in right time, for example:

Imgur

I create my sprite like this:

self.sprite = [SKSpriteNode spriteNodeWithImageNamed:spriteName];

[self.sprite setSize:CGSizeMake(40, 40)];
self.sprite.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.sprite.size];
self.sprite.physicsBody.mass = 0;
self.sprite.physicsBody.usesPreciseCollisionDetection = YES;    
self.sprite.physicsBody.categoryBitMask = Category;
self.sprite.physicsBody.collisionBitMask = 0;
self.sprite.physicsBody.contactTestBitMask = Category;   
self.sprite.physicsBody.dynamic = YES;
Was it helpful?

Solution

Contacts between two bodies depend upon their physicsBody, not their texture. So, when you use

self.sprite.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.sprite.size];

You are attaching a rectangular physicsBody.

You need to create a triangular physicsBody using the method:

[SKPhysicsBody bodyWithPolygonFromPath:path];

Read up on the method here.

In order to get the values for creating a path, you can use the SKPhysicsBody Path Generator tool.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top