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

    }

  • 相关阅读:
    新一代MQ apache pulsar的架构与核心概念
    Flutter使用fluwx实现微信分享
    BZOJ3622 已经没有什么好害怕的了 动态规划 容斥原理 组合数学
    NOIP2016提高组Day1T2 天天爱跑步 树链剖分 LCA 倍增 差分
    Codeforces 555C Case of Chocolate 其他
    NOIP2017提高组Day2T3 列队 洛谷P3960 线段树
    NOIP2017提高组Day2T2 宝藏 洛谷P3959 状压dp
    NOIP2017提高组Day1T3 逛公园 洛谷P3953 Tarjan 强连通缩点 SPFA 动态规划 最短路 拓扑序
    Codeforces 873F Forbidden Indices 字符串 SAM/(SA+单调栈)
    Codeforces 873E Awards For Contestants ST表
  • 原文地址:https://www.cnblogs.com/daochong/p/5270107.html
Copyright © 2011-2022 走看看