cocos2d-x之物理引擎之碰撞监测

Posted 一生不过烟花

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cocos2d-x之物理引擎之碰撞监测相关的知识,希望对你有一定的参考价值。

#include "HelloWorldScene.h"

 

USING_NS_CC;

 

#define RED_BIT_MASK    0b0100

#define GREEN_BIT_MASK  0b0010

#define BLUE_BIT_MASK   0b0001

#define EDGE_BIT_MASK   0b1000

 

Scene* HelloWorld::createScene()

{

    auto scene = Scene::createWithPhysics();

    scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);

    scene->getPhysicsWorld()->setGravity(Vec2(0,-1000));

    

    auto layer = HelloWorld::create();

 

    scene->addChild(layer);

 

    return scene;

}

 

bool HelloWorld::init()

{

    if ( !Layer::init() )

    {

        return false;

    }

    visibleSize=Director::getInstance()->getVisibleSize();

    

    auto touchListener=EventListenerTouchOneByOne::create();

    touchListener->onTouchBegan =[this](Touch * t,Event * e){

        this->addBox(t->getLocation());

        return false;

    };

    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphpriority(touchListener,this);

    

    auto contactListener=EventListenerPhysicsContact::create();

    contactListener->onContactBegin=[](PhysicsContact & contact){

        switch (contact.getShapeA()->getBody()->getContactTestBitmask()|contact.getShapeB()->getBody()->getContactTestBitmask()) {

            case RED_BIT_MASK|BLUE_BIT_MASK:

                log("red rect contact blue rect");

                break;

            case RED_BIT_MASK|GREEN_BIT_MASK:

                log("red rect contact green rect");

                break;

            case BLUE_BIT_MASK|GREEN_BIT_MASK:

                log("blue rect contact green rect");

                break;

            case RED_BIT_MASK|EDGE_BIT_MASK:

                log("red rect hit edge");

                break;

            case GREEN_BIT_MASK|EDGE_BIT_MASK:

                log("green rect hit edge");

                break;

            case BLUE_BIT_MASK|EDGE_BIT_MASK:

                log("blue rect hit edge");

                break;

            case GREEN_BIT_MASK:

                log("two green rect hit");

                break;

            default:

                break;

        }

        return true;

    };

    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(contactListener,this);

    

    return true;

}

 

void HelloWorld::onEnter(){

    Layer::onEnter();

    

    auto bounds=Node::create();

    bounds->setContentSize(visibleSize);

    bounds->setPhysicsBody(PhysicsBody::createEdgeBox(bounds->getContentSize()));

    bounds->getPhysicsBody()->setContactTestBitmask(EDGE_BIT_MASK);

    bounds->setPosition(visibleSize/2);

    addChild(bounds);

    

    addBox(visibleSize/2);

}

 

void HelloWorld::addBox(Vec2 position){

    auto r=Sprite::create();

    r->setTextureRect(Rect(50,50,50,50));

    r->setPhysicsBody(PhysicsBody::createBox(r->getContentSize()));

    addChild(r);

    r->setPosition(position);

    

//    switch (rand()%6) {

//        case 0:

//            r->setColor(Color3B(255,0,255));

//            break;

//        case 1:

//            r->setColor(Color3B(255,0,0));

//            break;

//        case 2:

//            r->setColor(Color3B(0,255,0));

//            break;

//        case 3:

//            r->setColor(Color3B(0,0,255));

//            break;

//        case 4:

//            r->setColor(Color3B(0,255,255));

//            break;

//        case 5:

//            r->setColor(Color3B(255,255,0));

//            break;

//        default:

//            break;

//    }

    switch (rand()%3) {

        case 0:

            r->setColor(Color3B(255,0,0));

            r->getPhysicsBody()->setContactTestBitmask(RED_BIT_MASK);

            break;

        case 1:

            r->setColor(Color3B(0,255,0));

            r->getPhysicsBody()->setContactTestBitmask(GREEN_BIT_MASK);

            break;

        case 2:

            r->setColor(Color3B(0,0,255));

            r->getPhysicsBody()->setContactTestBitmask(BLUE_BIT_MASK);

            break;

        default:

            break;

    }

}

 

void HelloWorld::menuCloseCallback(Ref* pSender)

{

    Director::getInstance()->end();

 

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ios)

    exit(0);

#endif

}

 

以上是关于cocos2d-x之物理引擎之碰撞监测的主要内容,如果未能解决你的问题,请参考以下文章

实例介绍Cocos2d-x中Box2D物理引擎:碰撞检測

SPRITEKIT游戏框架之关于PHYSICS物理引擎属性

关于Cocos2d-x物理引擎用到的类和使用

cocos2d-x box2d物理引擎深入研究 第一篇之旋转关节详解(b2RevoluteJoint)

Cocos2d-x 3.1.1 学习日志13--物理引擎登峰造极之路

Unity之碰撞体组件