Box2d物理引擎实战开发(共6部分)

第四阶段:功能扩展1
1.Box2d物理引擎:Box2d介绍 
  官网 http://box2d.org/
定义NDK_ROOT   终端打开proj.android 然后  sudo nano /etc/profile回车输入密码  export NDK_ROOT=
eclipse-sdks- android -android-ndk-r9c- ndk-build右键的路径 之后 退出-保存 重启终端
2.Box2d物理引擎:创建世界

HelloWorldScene.h
#include <Box2D/Box2D.h>
private :
    b2World *world;
public :
    virtual void update( float dt);

HelloWorldScene.cpp
HelloWorld::init()中:
world = new b2World(b2Vec2( 0 ,- 10 ));

// float32 timeStep模拟物理世界一步一步之间的时间差 这里用每帧之间的时间差dt     
// int32 velocityIterations数据迭代(重新计算的次数来避免误差) 次数越多越精确但也耗费资源 官方定为8
// int32 positionIterations 位置迭代官方建议为3

void HelloWorld::update( float dt){
    world->Step(dt, 8, 3);
}

3.Box2d物理引擎:创建运动的物体
 glview->setDesignResolutionSize( 800 , 600 , ResolutionPolicy::SHOW_ALL);

b2_staticBody = 0静态的物体
b2_kinematicBody漂浮的物体
b2_dynamicBody 动态的物体
4.Box2d物理引擎:创建静止的物体
5.Box2d物理引擎:创建漂浮的物体
6.Box2d物理引擎:物体碰撞检测
#include "cocos2d.h"
#include
<Box2D/Box2D.h>


#define RATIO 80.0f

class HelloWorld : public cocos2d::Layer, public b2ContactListener
{
   
private :
    b2World *world;
    b2Body *groundBody;
   
public :
   
// there's no 'id' in cpp, so we recommend returning the class instance pointer
   
static cocos2d::Scene* createScene();

   
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
   
virtual bool init(); 
   
   
// a selector callback
   
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();
   
   
// implement the "static create()" method manually
    CREATE_FUNC(HelloWorld);
};



#include "HelloWorldScene.h"

USING_NS_CC;

Scene* HelloWorld::createScene()
{
   
// 'scene' is an autorelease object
   
auto scene = Scene::create();
   
   
// 'layer' is an autorelease object
   
auto layer = HelloWorld::create();
   
   
// add layer as a child to scene
    scene->addChild(layer);
   
   
// return the scene
   
return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
   
//
   
// 1. super init first
   
if ( !Layer::init() )
    {
       
return false ;
    }
   
    world =
new b2World(b2Vec2( 0 ,- 10 ));
    world->SetContactListener(
this );
   
   
    addGround();
    addRect(
5 , 3 , 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->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(
0 , 10 );
    def.type = type;
   
    b2Body* body = world->CreateBody(&def);
   
    b2PolygonShape shape;
    shape.SetAsBox(
0.5 , 0.5 );
   
    b2FixtureDef fixtureDef;
    fixtureDef.density =
1 ;
    fixtureDef.friction =
0.3 ;
    fixtureDef.shape = &shape;
    body->CreateFixture(&fixtureDef);
   
   
//config cocos shape
   
auto s = Sprite::create();
    s->setTextureRect(Rect(
0 , 0 , 0.5 * 2 *RATIO, 0.5 * 2 *RATIO));
    addChild(s);
   
    body->SetUserData(s);
}


void HelloWorld::addGround(){
   
    b2BodyDef def;
    def.position = b2Vec2(
400 /RATIO, 0 );
    def.type = b2_staticBody;
   
    groundBody = world->CreateBody(&def);
   
    b2PolygonShape groundShape;
    groundShape.SetAsBox(
400 /RATIO, 0.5 );
   
    b2FixtureDef fixureDef;
    fixureDef.density =
1 ;
    fixureDef.friction =
0.3 ;
    fixureDef.shape = &groundShape;
    groundBody->CreateFixture(&fixureDef);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值