I'm a newbie of Cocos2d-x 3.2 Physic Engine (as Cocos2d-x said, this engine is base on Chipmuk). I made a sample "Egg shooting" game of Popcap. When detecting contact of 2 eggs, I met a problem, dynamic 'egg' seems sink to static 'egg' when contact event is throwed. I set for eggs:
This is image:
Detect contact ball event
auto contactBallListener = EventListenerPhysicsContact::create();
contactBallListener->onContactBegin = CC_CALLBACK_1(IngameScene::onContactBallsBegin, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(contactBallListener, this);
.....
bool IngameScene::onContactBallsBegin(PhysicsContact& contact)
{
auto objA = contact.getShapeA()->getBody()->getNode();
auto objB = contact.getShapeB()->getBody()->getNode();
}
Physical Setup
if (group == BallGroupTarget) {
//Green ball
body->setCategoryBitmask(0xFFFFFFF0);
body->setContactTestBitmask(0x0000000F);
body->setCollisionBitmask(0xFFFFFFFF);
} else if (group == BallGroupShooting){
//Pink ball
body->setContactTestBitmask(0xFFFFFFFF);
body->setGravityEnable(false);
}
Can you help me to solve this problem?
Thanks a lot!
From www.cocos2d-x.org
When CategoryBitmask of one body and with ContactTestBitmask of another body with the result doesn't equal to zero, the contact event will be sent, otherwise the contact event won't be sent.
When CategoryBitmask of one body and with CollisionBitmask of another body with the result doesn't equal to zero, they will collied, overwise it won't.
You should notice that by default, CategoryBitmask value is 0xFFFFFFFF, ContactTestBitmask value is 0x00000000, and CollisionBitmask value is 0xFFFFFFFF, which means all bodies will collide with each other but without sending contact event by default.
Observe the above SECOND and third point what it says. Set the bits accordingly in your game.
to make a static body do
pinkBall->setDynamic(false); //No need to set gravity false here. Now pink ball becomes static with zero gravity effect
bool IngameScene::onContactBallsBegin(PhysicsContact& contact)
This method should return a bool. Yours doesn't return anything. Pretty sure the compiler complains about that. Not sure what C++ defaults to, it might even return garbage.
Return true if you want the bodies to collide, return false if they should pass through one another.
User contributions licensed under CC BY-SA 3.0