原文同步发布于我的wiki,查看原文或更新请移步:
点击打开链接
auto scene = Scene::createWithPhysics();
scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);//debug模式
Vect gravity = Vect(0.0f, 0.0f);//0重力
scene->getPhysicsWorld()->setGravity(gravity);
//定义世界的边界
auto body = PhysicsBody::createEdgeBox(visibleSize,
PHYSICSBODY_MATERIAL_DEFAULT,5.0f);
auto edgeNode = Node::create();
edgeNode->setPosition(Vec2(visibleSize.width/2,visibleSize.height/2));
edgeNode->setPhysicsBody(body);
this->addChild(edgeNode);
// 注册物理碰撞事件监听
auto listener = EventListenerPhysicsContact::create();
listener->onContactBegin = [this](PhysicsContact& contact)
{
return true;
};
listener->onContactPreSolve = [] (PhysicsContact& contact,
PhysicsContactPreSolve& solve) {
// log("onContactPreSolve");
return true;
};
listener->onContactPostSolve = [] (PhysicsContact& contact,
const PhysicsContactPostSolve& solve) {
// log("onContactPostSolve");
};
listener->onContactSeperate = [](PhysicsContact& contact) {
log("onContactSeperate");
};
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(
listener, 1);
//设置物理body
auto body = PhysicsBody::createBox(sp->getContentSize());
body->setContactTestBitmask(0xFFFFFFFF);
sp->setPhysicsBody(body);
cocos2d-x-3.3-020-碰撞检测2-物理引擎初探
原文地址:http://blog.csdn.net/cheyiliu/article/details/43560685