#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
#include "Box2D/Box2D.h" ①
#define PTM_RATIO 32 ②
class HelloWorld : public cocos2d::Layer
{
b2World* world; ③
public:
static cocos2d::Scene* createScene();
virtual bool init();
virtual void update(float dt); ④
virtual bool onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event); ⑤
CREATE_FUNC(HelloWorld);
void initPhysics(); ⑥
void addNewSpriteAtPosition(cocos2d::Vec2 p); ⑦
};
#endif // __HELLOWORLD_SCENE_H__bool HelloWorld::init()
{
if ( !Layer::init() )
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
// 初始化物理引擎
this->initPhysics(); ①
setTouchEnabled(true);
//设置为单点触摸
setTouchMode(Touch::DispatchMode::ONE_BY_ONE);
//开始游戏循环
scheduleUpdate(); ②
return true;
}void HelloWorld::initPhysics()
{
Size s = Director::getInstance()->getVisibleSize();
//重力参数
b2Vec2 gravity; ①
gravity.Set(0.0f, -10.0f); ②
//创建世界
world = new b2World(gravity); ③
// 允许物体是否休眠
world->SetAllowSleeping(true); ④
// 开启连续物理测试
world->SetContinuousPhysics(true); ⑤
//地面物体定义
b2BodyDef groundBodyDef; ⑥
//左下角
groundBodyDef.position.Set(0, 0); ⑦
//创建地面物体
b2Body* groundBody = world->CreateBody(&groundBodyDef); ⑧
//定义一个有边的形状
b2EdgeShape groundBox; ⑨
// 底部
groundBox.Set(b2Vec2(0,0), b2Vec2(s.width/PTM_RATIO,0)); ⑩
//使用夹具固定形状到物体上
groundBody->CreateFixture(&groundBox,0); ?
// 顶部
groundBox.Set(b2Vec2(0,s.height/PTM_RATIO),
b2Vec2(s.width/PTM_RATIO,s.height/PTM_RATIO));
groundBody->CreateFixture(&groundBox,0);
// 左边
groundBox.Set(b2Vec2(0,s.height/PTM_RATIO), b2Vec2(0,0)); groundBody->CreateFixture(&groundBox,0);
// 右边
groundBox.Set(b2Vec2(s.width/PTM_RATIO,s.height/PTM_RATIO),
b2Vec2(s.width/PTM_RATIO,0));
groundBody->CreateFixture(&groundBox,0);
}void HelloWorld::addNewSpriteAtPosition(Vec2 p)
{
log("Add sprite %0.2f x %02.f",p.x,p.y);
//创建物理引擎精灵对象
auto sprite = Sprite::create("BoxA2.png"); ①
sprite->setPosition( Vec2( p.x, p.y) );
this->addChild(sprite);
//物体定义
b2BodyDef bodyDef; ②
bodyDef.type = b2_dynamicBody; ③
bodyDef.position.Set(p.x/PTM_RATIO, p.y/PTM_RATIO); ④
b2Body *body = world->CreateBody(&bodyDef); ⑤
body->SetUserData(sprite); ⑥
// 定义2米见方的盒子形状
b2PolygonShape dynamicBox; ⑦
dynamicBox.SetAsBox(1, 1); ⑧
// 夹具定义
b2FixtureDef fixtureDef; ⑨
//设置夹具的形状
fixtureDef.shape = &dynamicBox; ⑩
//设置密度
fixtureDef.density = 1.0f; ?
//设置摩擦系数
fixtureDef.friction = 0.3f; ?
//使用夹具固定形状到物体上
body->CreateFixture(&fixtureDef); ?
}void HelloWorld::update(float dt)
{
float timeStep = 0.03f;
int32 velocityIterations = 8;
int32 positionIterations = 1;
world->Step(timeStep, velocityIterations, positionIterations);
for (b2Body* b = world->GetBodyList(); b; b = b->GetNext()) ①
{
if (b->GetUserData() != nullptr) {
Sprite* sprite = (Sprite*)b->GetUserData();
sprite->setPosition( Vec2( b->GetPosition().x *
PTM_RATIO, b->GetPosition().y * PTM_RATIO) );
sprite->setRotation( -1 * CC_RADIANS_TO_DEGREES(b->GetAngle()) );
}
} ②
}其中代码①~②这段代码可以同步物理引擎中的物体与精灵位置和状态。
欢迎关注智捷iOS课堂微信公共平台
实例介绍Cocos2d-x中Box2D物理引擎:HelloBox2D
原文:http://blog.csdn.net/tonny_guan/article/details/39897127