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

}

原文地址:https://www.cnblogs.com/daochong/p/5270107.html