初步看box2d的应用,小记一下,错误请指正,谢谢。
1、Box2dLayer.h文件
#include "cocos2d.h"
#include "Box2D/Box2D.h"
USING_NS_CC;
class Box2dLayer : public CCLayer{
public:
Box2dLayer(void);
~Box2dLayer(void);
CREATE_FUNC(Box2dLayer);
virtual bool init();
void addSprite(CCPoint point);
void update(float dt);
//注册单点触摸
void registerWithTouchDispatcher();
virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
private:
b2World* m_world;
};
2、Box2dLayer.cpp文件
#include "Box2dLayer.h"
#define PTM_RATIO 32
Box2dLayer::Box2dLayer(void){
}
Box2dLayer::~Box2dLayer(void){
delete m_world;
m_world = NULL;
}
bool Box2dLayer::init(){
bool bRet = false;
do
{
CC_BREAK_IF(!CCLayer::init());
this->setTouchEnabled(true);
this->setAccelerometerInterval(true);// 在层里启用重力感应
CCSize size = CCDirector::sharedDirector()->getWinSize();
b2Vec2 gravity;
gravity.Set(0.0f,-10.0f);//设置重力方向
m_world = new b2World(gravity);
m_world->SetAllowSleeping(true);
m_world->SetContinuousPhysics(true);
//定义地表物体(限制刚体的运动范围)
b2BodyDef groundBodyDef;
b2Body* groundBody = m_world->CreateBody(&groundBodyDef);
//定义形状
b2PolygonShape groundBox;
//墻底
groundBox.SetAsBox(size.width/PTM_RATIO,0,b2Vec2(0,0),0);
groundBody->CreateFixture(&groundBox,0);
//墻顶
groundBox.SetAsBox(size.width/PTM_RATIO,0,b2Vec2(0,size.height/PTM_RATIO),0);
groundBody->CreateFixture(&groundBox,0);
//左墻
groundBox.SetAsBox(0,size.height/PTM_RATIO,b2Vec2(0,0),0);
groundBody->CreateFixture(&groundBox,0);
//右墻
groundBox.SetAsBox(0,size.height/PTM_RATIO,b2Vec2(size.width/PTM_RATIO,0),0);//参1为width,参2为height,参3貌似为重心位置(物体看为一个质点)
groundBody->CreateFixture(&groundBox,0);
CCSpriteBatchNode* batch = CCSpriteBatchNode::create("CloseNormal.png");
batch->setPosition(CCPointZero);
this->addChild(batch,2,0);
scheduleUpdate();
bRet = true;
} while (0);
return bRet;
}
void Box2dLayer::addSprite(CCPoint point){
CCSpriteBatchNode* batch = (CCSpriteBatchNode*)this->getChildByTag(0);
CCSprite* sprite = CCSprite::createWithTexture(batch->getTexture());
sprite->setPosition(ccp(point.x,point.y));
this->addChild(sprite);
//创建刚体结构体
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(point.x/PTM_RATIO,point.y/PTM_RATIO);
bodyDef.userData = sprite;//将sprite精灵和物体对象绑定
//创建刚体
b2Body* body = m_world->CreateBody(&bodyDef);
//定义形状
b2PolygonShape dynamicBox;
dynamicBox.SetAsBox(0.5f,0.5f);
//定制器
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicBox;//形状
fixtureDef.density = 1.0;//密度
fixtureDef.friction = 0.3f;//摩擦系数
//fixtureDef.restitution = 0.8f;//恢复,用于弹力
body->CreateFixture(&fixtureDef);
}
void Box2dLayer::update(float dt){
m_world->Step(dt,8,1);
for (b2Body* b=m_world->GetBodyList();b;b=b->GetNext())
{
if (b->GetUserData() != NULL)
{
CCSprite* sprite = (CCSprite*)b->GetUserData();
sprite->setPosition(ccp(b->GetPosition().x*PTM_RATIO,b->GetPosition().y*PTM_RATIO));
}
}
}
void Box2dLayer::registerWithTouchDispatcher(){
CCDirector* pDirector=CCDirector::sharedDirector();
pDirector->getTouchDispatcher()->addTargetedDelegate(this,0,true);
}
bool Box2dLayer::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent){
return true;
}
void Box2dLayer::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent){
CCPoint location = pTouch->getLocationInView();
location = CCDirector::sharedDirector()->convertToGL(location);
addSprite(location);
}