zoukankan      html  css  js  c++  java
  • 物理引擎一

    #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
    }
  • 相关阅读:
    spring注解之@Lazy
    HttpClient之EntityUtils对象
    HTTP协议(Requset、Response)
    SpringBoot SpringSession redis SESSION
    Spring-session redis 子域名 session
    Spring Boot Servlet
    版本管理
    Spring AOP @Aspect
    Spring 事务配置的五种方式
    Spring <tx:annotation-driven>注解 JDK动态代理和CGLIB动态代理 区别。
  • 原文地址:https://www.cnblogs.com/newlist/p/4363507.html
Copyright © 2011-2022 走看看