1
votes

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:

  • Mass: 10.f
  • PHYSICSSHAPE_MATERIAL_DEFAULT
  • applyImpulse about (0,900)

This is image:

enter image description here

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!

2
please add the physics setup and collision detection code to your questionLearnCocos2D

2 Answers

1
votes

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

0
votes
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.