Reputation: 1372
I have a bunch of SKSpriteNode's set up. My hero's node isn't supposed to run into walls and if it does, it will trigger a collision. That works fine but the collision point isn't exactly on the sprite's image.
Here is the hero's code:
hero = [SKSpriteNode spriteNodeWithImageNamed:@"hero"];
hero.name = heroCategoryName;
hero.position = CGPointMake(100, CGRectGetMidY(self.frame));
hero.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:hero.frame.size.width];
hero.physicsBody.friction = 1.0f;
hero.physicsBody.restitution = 0.0f;
hero.physicsBody.linearDamping = 0.1f;
hero.physicsBody.allowsRotation = NO;
hero.physicsBody.categoryBitMask = heroCategory;
hero.physicsBody.contactTestBitMask = wallCategory;
hero.physicsBody.usesPreciseCollisionDetection = YES;
hero.physicsBody.mass = 0.2f;
[self addChild:hero];
Here are the walls:
SKSpriteNode *wall = [SKSpriteNode spriteNodeWithImageNamed:@"wall"];
wall.name = wallCategoryName;
wall.hidden = YES;
wall.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:wall.size];
wall.physicsBody.dynamic = NO;
wall.physicsBody.collisionBitMask = 0;
wall.physicsBody.usesPreciseCollisionDetection = YES;
wall.physicsBody.categoryBitMask = wallCategory;
wall.physicsBody.contactTestBitMask = heroCategory;
This happens no matter if the collision is on top, left or right. I've gone and made sure that my images were all crapped as much as they can be. I'm not sure what else it could be. It just seems like the hitbox for the body is bigger than it should be.
Upvotes: 1
Views: 516
Reputation: 6707
What are the dimensions of the hero sprite? If it's a square image, then change the following
hero.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:hero.frame.size.width];
into
hero.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:hero.frame.size.width/2.0f];
Upvotes: 2