So, for the application I'm working on, I came across two cubes. I check this in a standard way. The application tells me when they come across in my "didBeginContact" method.
-(void)didBeginContact:(SKPhysicsContact *)contact { if (contact.bodyA.categoryBitMask == WALL_CATEGORY && contact.bodyB.categoryBitMask == CHARACTER_CATEGORY) { CGPoint point = contact.contactPoint; } }
So, I know where the collision occurs, but since it is two squares, it can be at any point along the side, including the corners. So, how would I check if the collision is left / right / top / bottom?
Edit: correct answer: This may not be the cleanest way, but it works. Hope this helps someone in the future.
m_lNode = [SKNode node]; m_lNode.position = CGPointMake(-(CHARACTER_SIZE / 2), 0); m_lNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(1, m_character.size.height)]; m_lNode.physicsBody.allowsRotation = NO; m_lNode.physicsBody.usesPreciseCollisionDetection = YES; m_lNode.physicsBody.categoryBitMask = CHARACTER_L_CATEGORY; m_rNode = [SKNode node]; m_rNode.position = CGPointMake((CHARACTER_SIZE / 2), 0); m_rNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(1, m_character.size.height)]; m_rNode.physicsBody.allowsRotation = NO; m_rNode.physicsBody.usesPreciseCollisionDetection = YES; m_rNode.physicsBody.categoryBitMask = CHARACTER_R_CATEGORY; m_tNode = [SKNode node]; m_tNode.position = CGPointMake(0, (CHARACTER_SIZE / 2)); m_tNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(m_character.size.width , 1)]; m_tNode.physicsBody.allowsRotation = NO; m_tNode.physicsBody.usesPreciseCollisionDetection = YES; m_tNode.physicsBody.categoryBitMask = CHARACTER_T_CATEGORY; m_bNode = [SKNode node]; m_bNode.position = CGPointMake(0, -(CHARACTER_SIZE / 2)); m_bNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(m_character.size.width, 1)]; m_bNode.physicsBody.allowsRotation = NO; m_bNode.physicsBody.usesPreciseCollisionDetection = YES; m_bNode.physicsBody.categoryBitMask = CHARACTER_B_CATEGORY; [m_character addChild:m_tNode]; [m_character addChild:m_bNode]; [m_character addChild:m_lNode]; [m_character addChild:m_rNode]; -(void)didBeginContact:(SKPhysicsContact *)contact { if (contact.bodyA.categoryBitMask == WALL_CATEGORY) { switch (contact.bodyB.categoryBitMask) { case CHARACTER_T_CATEGORY: NSLog(@"Top"); m_isHitTop = true; break; case CHARACTER_B_CATEGORY: NSLog(@"Bottom"); m_isHitBottom = true; break; case CHARACTER_L_CATEGORY: NSLog(@"Left"); m_isHitLeft = true; break; case CHARACTER_R_CATEGORY: NSLog(@"Right"); m_isHitRight = true; break; } } }
Corresponding code added. This is my code, so there are variables among others, but you must understand this.
source share