物理引擎一

#include "HelloWorldScene.h"

//物理引擎標識  
int ball1Mask = 1 << 0;//球1  
int ball2Mask = 1 << 1;//球2  
int wallMask = 1 << 2;//地面

Scene* HelloWorld::createScene()
{
    //建立有物理空間的場景  
    Scene* scene=Scene::createWithPhysics();  
    //設置Debug模式,你會看到物體的表面被線條包圍,主要爲了在調試中更容易地觀察  
    scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);  
    
    // 'layer' is an autorelease object
    auto layer = HelloWorld::create();
    //把空間保持咱們建立的層中,就是上面所說m_world的做用,方便後面設置空間的參數  
    //layer->setPhyWorld(scene->getPhysicsWorld()); 
    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

void HelloWorld::onEnter()
{
    Layer::onEnter();  
    //添加監聽器  
    auto contactListener=EventListenerPhysicsContact::create();  
    //設置監聽器的碰撞開始函數  
    contactListener->onContactBegin = CC_CALLBACK_1(HelloWorld::onContactBegin, this);  
    //添加到事件分發器中  
    _eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener, this);  
}

bool HelloWorld::onContactBegin(const PhysicsContact& contact)  
{  

    if ((contact.getShapeA()->getBody()->getCategoryBitmask() & ball1Mask) == ball1Mask) {  
        CCLOG("ball1 touch something");  
    }  
    if ((contact.getShapeB()->getBody()->getCategoryBitmask() & ball1Mask) == ball1Mask) {  
        CCLOG("ball1 touch something");  
    }  


    if ((contact.getShapeA()->getBody()->getCategoryBitmask() & ball2Mask) == ball2Mask) {  
        CCLOG("ball2 touch something");  
    }  
    if ((contact.getShapeB()->getBody()->getCategoryBitmask() & ball2Mask) == ball2Mask) {  
        CCLOG("ball2 touch something");  
    }  

    return true;  
}  

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
    /*
    auto animation = Animation::create();
    for (int i = 1; i <8; i++)
    {
    char szName[100] = {0};
    sprintf(szName, "bird_%d.png",i);
    animation->addSpriteFrameWithFile(szName);
    }

    animation->setDelayPerUnit(0.08f);
    animation->setRestoreOriginalFrame(true);
    auto action = Animate::create(animation);

    CCSprite* pBird = CCSprite::create("bird_1.png");
    pBird->setPosition(ccp(480,320));
    this->addChild(pBird);
    pBird->runAction(CCRepeatForever::create(Sequence::create(action,action->reverse(),NULL)));
    */
    
    CCSprite* ballOne;
    CCSprite* ballTwo;

    Size visibleSize=Director::getInstance()->getVisibleSize();  
    Point origin=Director::getInstance()->getVisibleOrigin();  

    ballOne=Sprite::create();  
    ballOne->setContentSize(Size(50, 50));  
    ballOne->setPosition(visibleSize.width/2,visibleSize.height/2);  
    //建立物體,而且物體的形狀爲圓形,第一參數爲半徑,第二個參數爲物體材質  
    //第三個參數爲邊的厚度,就是在Debug模式下看到的物體外面線條的厚度,默認爲0  
    PhysicsBody* ballBodyOne=PhysicsBody::createCircle(ballOne->getContentSize().width/2,PHYSICSBODY_MATERIAL_DEFAULT, Vec2(1,1));  
    //  
    //ballBodyOne->setCategoryBitmask(1);  
    
    //是否設置物體爲靜態  
    //ballBody->setDynamic(false);  
    //設置物體的恢復力  
    ballBodyOne->getShape(0)->setRestitution(1.0f);  
    //設置物體的摩擦力  
    ballBodyOne->getShape(0)->setFriction(0.0f);  
    //設置物體密度  
    ballBodyOne->getShape(0)->setDensity(1.0f);  
    //設置質量  
    //ballBodyOne->getShape(0)->setMass(5000);  
    //設置物體是否受重力系數影響  
    ballBodyOne->setGravityEnable(false);  

    //設置物體的衝力  
    Vect force=Vect(500000.0f, 500000.0f);  
    ballBodyOne->applyImpulse(force);  
    //把物體添加到精靈中  
    ballOne->setPhysicsBody(ballBodyOne);  
    //設置標誌  
    ballOne->setTag(1);  
    this->addChild(ballOne);  

    //設置第二個球  
    ballTwo=Sprite::create();  
    ballTwo->setContentSize(Size(100, 100));  
    ballTwo->setPosition(visibleSize.width/3,visibleSize.height/3);  
    PhysicsBody* ballBodyTwo=PhysicsBody::createCircle(ballOne->getContentSize().width/2,PHYSICSBODY_MATERIAL_DEFAULT);  
    //是否設置物體爲靜態  
    //ballBodyTwo->setDynamic(false);  
    ballBodyTwo->getShape(0)->setRestitution(1.0f);  
    ballBodyTwo->getShape(0)->setFriction(0.0f);  
    ballBodyTwo->getShape(0)->setDensity(1.0f);  

    ballBodyTwo->setGravityEnable(false);  

    force=Vect(-500000.0f, -500000.0f);  
    ballBodyTwo->applyImpulse(force);  
    ballTwo->setPhysicsBody(ballBodyTwo);  
    ballTwo->setTag(2);  
    this->addChild(ballTwo);  

    //建立一個盒子,用來碰撞  
    Sprite* edgeSpace=Sprite::create();  
    PhysicsBody* boundBody=PhysicsBody::createEdgeBox(visibleSize,PHYSICSBODY_MATERIAL_DEFAULT,3);  
    boundBody->getShape(0)->setFriction(0.0f);  
    boundBody->getShape(0)->setRestitution(1.0f);  

    edgeSpace->setPhysicsBody(boundBody);  
    edgeSpace->setPosition(Point(visibleSize.width/2,visibleSize.height/2));  
    this->addChild(edgeSpace);  
    edgeSpace->setTag(0);  

    ballBodyOne->setCategoryBitmask(ball1Mask);  
    ballBodyOne->setCollisionBitmask(wallMask|ball1Mask|ball2Mask);  
    ballBodyOne->setContactTestBitmask(wallMask|ball1Mask|ball2Mask);  

    ballBodyTwo->setCategoryBitmask(ball2Mask);  
    ballBodyTwo->setCollisionBitmask(wallMask|ball1Mask|ball2Mask);  
    ballBodyTwo->setContactTestBitmask(wallMask|ball1Mask|ball2Mask);  

    boundBody->setCategoryBitmask(wallMask);  
    boundBody->setCollisionBitmask(wallMask | ball1Mask|ball2Mask);  
    boundBody->setContactTestBitmask(wallMask|ball1Mask|ball2Mask);  

    return true;
}


void HelloWorld::menuCloseCallback(Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
    MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
    return;
#endif

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

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}
相關文章
相關標籤/搜索