Skip to content

Instantly share code, notes, and snippets.

@maxchuquimia
Last active August 29, 2015 14:00
Show Gist options
  • Save maxchuquimia/11306381 to your computer and use it in GitHub Desktop.
Save maxchuquimia/11306381 to your computer and use it in GitHub Desktop.
Cocos2D/Box2D/LevelHelper Manual Collision Detection
/* Put this in a subclass of LHSprite!
* Then, in your layer's update: method you can check if a sprite is touching another sprite:
* [mySpriteClassInstance isCollidedWithSprite:anotherSprite];
*/
- (BOOL)isCollidedWithSprite:(LHSprite *)spriteB
{
BOOL isTouching = FALSE;
for (b2Fixture *fA = self.body->GetFixtureList(); fA; fA = fA->GetNext())
{
b2Shape *shapeA = fA->GetShape();
for (b2Fixture *fB = spriteB.body->GetFixtureList(); fB; fB = fB->GetNext())
{
b2Shape *shapeB = fB->GetShape();
if (b2TestOverlap(shapeA, 0, shapeB, 0, self.body->GetTransform(), spriteB.body->GetTransform()))
{
isTouching = TRUE;
break;
}
}
if (isTouching)
{
break;
}
}
return isTouching;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment