zoukankan      html  css  js  c++  java
  • cocos2d-x之Box2d初试

    物理引擎:用来模拟一套物理事件的物理代码。

    #ifndef __HELLOWORLD_SCENE_H__

    #define __HELLOWORLD_SCENE_H__

    #include "cocos2d.h"

    #include <Box2D/Box2D.h>

    #define RATIO 80.0f

    class HelloWorld : public cocos2d::Layer,public b2ContactListener

    {

    private:

        b2World *world;

        b2Body *groundBody;

    public:

        static cocos2d::Scene* createScene();

        virtual bool init();

        void menuCloseCallback(cocos2d::Ref* pSender);

        virtual void update(float dt);

        virtual void BeginContact(b2Contact* contact);

        void addRect(float x,float y,b2BodyType type);

        void addGround();

        CREATE_FUNC(HelloWorld);

    };

    #endif // __HELLOWORLD_SCENE_H__

    #include "HelloWorldScene.h"

    USING_NS_CC;

    Scene* HelloWorld::createScene()

    {

        auto scene = Scene::create();

        auto layer = HelloWorld::create();

        scene->addChild(layer);

        return scene;

    }

    bool HelloWorld::init()

    {

        if ( !Layer::init() )

        {

            return false;

        }

        

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

        Vec2 origin = Director::getInstance()->getVisibleOrigin();

        

        //创建世界

        world=new b2World(b2Vec2(0,-10));

        world->SetContactListener(this);

        

        addGround();

        addRect(5,5,b2_dynamicBody);

        //addRect(1,5,b2_kinematicBody);//漂浮物体,不受重力影响

        

        scheduleUpdate();

        

        return true;

    }

    void HelloWorld::update(float dt){

        world->Step(dt,8,3);

        Sprite *s;

        for (b2Body *b=world->GetBodyList();b;b=b->GetNext()) {

            //if (b->GetType()==b2_dynamicBody) {

                //log("%f",b->GetPosition().y);

                if (b->GetUserData()) {

                    s=(Sprite*)b->GetUserData();

                    

                    s->setPosition(b->GetPosition().x*RATIO,b->GetPosition().y*RATIO);

                }

            //}

        }

    }

    void HelloWorld::BeginContact(b2Contact *contact){

        if (contact->GetFixtureA()->GetBody()==groundBody||contact->GetFixtureB()->GetBody()==groundBody) {

            log("有物体落在了地板上");

        }

    }

    void HelloWorld::addRect(float positionX,float positionY,b2BodyType type){

        //config box2d

        b2BodyDef def;

        def.position=b2Vec2(positionX,positionY);

        //def.linearVelocity=b2Vec2(1,0);

        //def.linearVelocity=b2Vec2(0,10);

        def.type=type;

        

        groundBody=world->CreateBody(&def);

        

        b2PolygonShape shape;

        shape.SetAsBox(0.5,0.5);

        

        b2FixtureDef fixtureDef;

        fixtureDef.density=1;

        fixtureDef.friction=0.3;

        fixtureDef.shape=&shape;

        groundBody->CreateFixture(&fixtureDef);

        

        //config cocos shape

        auto s=Sprite::create();

        s->setTextureRect(Rect(0,0,0.5*2*RATIO,0.5*2*RATIO));

        addChild(s);

        

        //s->setPosition(Point(def.position.x*RATIO,def.position.y*RATIO));

        

        groundBody->SetUserData(s);

    }

    void HelloWorld::addGround(){

        b2BodyDef def;

        def.position=b2Vec2(400/RATIO,0);

        def.type=b2_staticBody;

        

        b2Body *body=world->CreateBody(&def);

        

        b2PolygonShape groundShape;

        groundShape.SetAsBox(400/RATIO,0.5);

        

        b2FixtureDef fixureDef;

        fixureDef.density=1;

        fixureDef.friction=0.3;

        fixureDef.shape=&groundShape;

        body->CreateFixture(&fixureDef);

    }

    void HelloWorld::menuCloseCallback(Ref* pSender)

    {

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

    #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

        exit(0);

    #endif

    }

  • 相关阅读:
    bzoj1072: [SCOI2007]排列perm
    bzoj1226: [SDOI2009]学校食堂Dining
    bzoj3208: 花神的秒题计划Ⅰ
    bzoj1079: [SCOI2008]着色方案
    bzoj3573: [Hnoi2014]米特运输
    bzoj1040: [ZJOI2008]骑士
    bzoj 1369: [Baltic2003]Gem
    bzoj2818: Gcd
    bzoj2705: [SDOI2012]Longge的问题
    整数分解
  • 原文地址:https://www.cnblogs.com/daochong/p/5270107.html
Copyright © 2011-2022 走看看