Cocos2d-x 3.2 Physic Engine: Physic Bodies contact not exactly

1

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!

cocos2d-x
game-physics
cocos2d-x-3.0
asked on Stack Overflow Nov 7, 2014 by Mục Đồng • edited Nov 11, 2014 by Mục Đồng

2 Answers

1

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

answered on Stack Overflow Nov 12, 2014 by SaffronState • edited Jun 20, 2020 by Community
0
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.

answered on Stack Overflow Nov 11, 2014 by LearnCocos2D

User contributions licensed under CC BY-SA 3.0