cocos2d-x中box2d的简单应用实例(点击屏幕产生刚体)

来源:互联网 发布:nothing软件邀请码 编辑:程序博客网 时间:2024/04/30 03:47

      初步看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 32Box2dLayer::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);}



0 0
原创粉丝点击