I want an event listener to detect not just a collision, but to detect if a sprite bounding box is completely in side another sprite bounding box, I've used
bool GameScene::onContactBegin(cocos2d::PhysicsContact &contact)
returning false because I don't want any sort of collision physics but got only triggered on first contact.
I want to use physics because its fast and accurate for my purposes. any ideas ??
One way to achieve this is by storing your colliding sprites in a vector:
std::vector<std::pair<coco2d::Sprite*, cocos2d::Sprite*>> collisionVector;
You can get the 2 colliding sprite objects from contact this way:
auto spriteA = dynamic_cast<Sprite*>(contact.getShapeA()->getBody()->getNode());
auto spriteB = dynamic_cast<Sprite*>(contact.getShapeB()->getBody()->getNode());
Store them in collision vector if neither sprite is null. Do this every time you get a physics contact. Now you can use this in your update() function to check for collision. If there is a collision, you can fire a custom event. You can also use update function to cleanup the list when the collision is over or you deleted a sprite.
To check for collision you need to get the bounding box of each sprite and compare them. You can get bounding box like so:
cocos2d::Rect boundingBox = sprite->getBoundingBox();
Now you can use check both boundingBoxes against each other by testing to see if the 4 corners of the box are within the other box. You can use containsPoint() function of Rect to do this.
Finally, if you found the boundingBoxs are inside one of the other, fire a custom collision event:
void onCollisionInside(Sprite* spriteA, Sprite* spriteB)
{
//do something
}