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
- cocos2d-x中box2d的简单应用实例(点击屏幕产生刚体)
- Cocos2d-x Box2d中实现刚体的移动
- [cocos2d-x]box2d的简单应用
- cocos2d-x box2d 世界刚体初始化流程
- Cocos2d-X Box2D的使用实例
- coco2d-x 中box2d选中刚体
- Cocos2d-x--Box2D使用GLES-Render.h渲染查看刚体
- cocos2d-x中box2d的关节
- Box2D中切割刚体效果的实现一览(一)
- Box2D中切割刚体效果的实现一览(二)
- Box2D中切割刚体效果的实现一览(完)
- cocos2d-x Box2D 简单例子
- 实例介绍Cocos2d-x中Box2D物理引擎:HelloBox2D
- 实例介绍Cocos2d-x中Box2D物理引擎:碰撞检测
- 实例介绍Cocos2d-x中Box2D物理引擎:使用关节
- 实例介绍Cocos2d-x中Box2D物理引擎:HelloBox2D
- 实例介绍Cocos2d-x中Box2D物理引擎:碰撞检测
- 实例介绍Cocos2d-x中Box2D物理引擎:使用关节
- Ora-01536:超出了表空间users的空间限量(转)
- struts的内部运行机制详解
- PHP学习笔记----PHP文件编程
- 接口测试小工具(HTTP + XML)
- java中如何实现多线程
- cocos2d-x中box2d的简单应用实例(点击屏幕产生刚体)
- android开发之应用splash screen
- 快速排序法详解
- 自定义带倒影和偏转的超炫Gallery
- Android Intent 序列化反序列化
- 数据库连接池:Druid
- PHP学习笔记----正则表达式
- S5PV210硬件相关“问与答”
- 老罗的2012年的Android之旅:梦想、学习、坚持、自信、淡定