Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Game with Sprite Kit Physics and Collision Contact Detection using SKPhysicsContactDelegate

Eduardo Rojas
Eduardo Rojas
4,084 Points

Error while detecting

My contact with the "Ground" is being detected until it reaches half of the object "ground"

1 Answer

Neil Shweky
Neil Shweky
5,022 Points

Paste your code, I'll take a look!

Eduardo Rojas
Eduardo Rojas
4,084 Points

The declaration:

typedef NS_OPTIONS(uint32_t, VACollisionCategories){ VACollisionCategoryFigure = 1 << 0, // 0000 VACollisionCategoryTrampoline = 1 << 1, // 0010 VACollisionCategoryGround = 1 << 2, // 0100

};

My figure setUpPhysics method:

-(void)SetUpPhysicsBody{

self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.size];
self.physicsBody.categoryBitMask = VACollisionCategoryFigure;
self.physicsBody.affectedByGravity = NO;
self.physicsBody.collisionBitMask = VACollisionCategoryGround;
self.physicsBody.contactTestBitMask = VACollisionCategoryGround | VACollisionCategoryTrampoline;
self.physicsBody.usesPreciseCollisionDetection = YES;

}

My trampoline setUpPhysics method:

-(void)setUpPhysicsBody{ self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.size]; self.physicsBody.affectedByGravity = NO; self.physicsBody.dynamic = NO; self.physicsBody.categoryBitMask = VACollisionCategoryTrampoline; self.physicsBody.collisionBitMask = 0; self.physicsBody.contactTestBitMask = VACollisionCategoryFigure; self.physicsBody.usesPreciseCollisionDetection = YES; }

And the detection in the view:

  • (void) didBeginContact:(SKPhysicsContact *)contact { SKPhysicsBody *firstBody, *secondBody; SKAction *moveByY = [SKAction moveToY:CGRectGetMaxY(self.frame)+32 duration:2];

    if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask ) { firstBody = contact.bodyA; secondBody = contact.bodyB; } else { firstBody = contact.bodyB; secondBody = contact.bodyA; }

    if ( firstBody.categoryBitMask == VACollisionCategoryFigure && secondBody.categoryBitMask == VACollisionCategoryTrampoline ) { NSLog(@"BAM!"); VAFigures *figure = (VAFigures *)firstBody.node;

    [figure runAction:moveByY];
    [figure setUpPhysics];
    
    [self addPoints:1];
    

    } else if ( firstBody.categoryBitMask == VACollisionCategoryFigure && secondBody.categoryBitMask == VACollisionCategoryGround ) { NSLog(@"Hit ground!"); VAFigures *figure = (VAFigures *)firstBody.node; [figure removeFromParent];

    }

}

//it also has this in the initWithSize method:

    self.physicsWorld.contactDelegate = self;
    self.physicsWorld.gravity = CGVectorMake(0.0, 0.0);

Thanks for checking and sorry i got busy this days...