cocos2d-x之box2d使用笔记

 下午学了一下box2d相关的内容,这里做个笔记。

1、创建头文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
#include "GLES-Render.h"
USING_NS_CC;
class  HelloWorld :  public  cocos2d::CCLayer
{
public :
    HelloWorld();
    ~HelloWorld();
                      
     static  cocos2d::CCScene* scene();
     CCSize _screenSize;
     void  initPhysics();
     virtual  void  draw();
     void  update( float  dt);
     virtual  void  ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent);
     void  addNewSpriteAtPosition(CCPoint p);
private :
     cocos2d::CCTexture2D* m_pSpriteTexture;
     b2World* world;
     GLESDebugDraw* m_debugDraw;
};
#endif // __HELLOWORLD_SCENE_H__


2、编写实现cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "HelloWorldScene.h"
#include "VisibleRect.h"
USING_NS_CC;
#define PTM_RATIO 32.0f
enum {
     kTagParentNode = 1
};
HelloWorld::HelloWorld():m_pSpriteTexture(NULL),world(NULL){
     _screenSize = CCDirector::sharedDirector()->getVisibleSize();
     setTouchEnabled( true );
     this ->initPhysics();
     //使用批处理方式加载图片
     CCSpriteBatchNode *parent = CCSpriteBatchNode::create( "blocks.png" ,100);
     m_pSpriteTexture = parent->getTexture();
     addChild(parent,0,kTagParentNode);
     //在中间添加一个精灵
     addNewSpriteAtPosition(ccp(_screenSize.width/2,_screenSize.height/2));
     scheduleUpdate();
}
HelloWorld::~HelloWorld(){
     delete  world;
     world = NULL;
     delete  m_debugDraw;
}
void  HelloWorld::initPhysics(){
     b2Vec2 gravity;
     gravity.Set(0.0f,-10.0f);
     //创建世界
     world =  new  b2World(gravity);
     //允许睡眠
     world->SetAllowSleeping( true );
     world->SetContinuousPhysics( true );
     //创建GLESDebugDraw
      m_debugDraw =  new  GLESDebugDraw( PTM_RATIO );
      world->SetDebugDraw(m_debugDraw);
     uint32 flags = 0;
     flags += b2Draw::e_shapeBit;
     flags += b2Draw::e_jointBit;
      m_debugDraw->SetFlags(flags);
     //定义边框
     b2BodyDef groundBodyDef;
     groundBodyDef.position.Set(0,0);
     b2Body* groundBody = world->CreateBody(&groundBodyDef);
     //定义ground box shape
     b2EdgeShape groundBox;
     //bottom
     groundBox.Set(b2Vec2(0,0),
         b2Vec2(_screenSize.width/PTM_RATIO,0/PTM_RATIO));
     groundBody->CreateFixture(&groundBox,0);
     //top
     groundBox.Set(b2Vec2(0,_screenSize.height/PTM_RATIO),
         b2Vec2(_screenSize.width/PTM_RATIO,_screenSize.height/PTM_RATIO));
     groundBody->CreateFixture(&groundBox,0);
     //left
     groundBox.Set(b2Vec2(0,_screenSize.height/PTM_RATIO),
         b2Vec2(0,0));
     groundBody->CreateFixture(&groundBox,0);
     //right
     groundBox.Set(b2Vec2(_screenSize.width/PTM_RATIO,_screenSize.height/PTM_RATIO),
         b2Vec2(_screenSize.width/PTM_RATIO,0));
     groundBody->CreateFixture(&groundBox,0);
}
void  HelloWorld::draw(){
     CCLayer::draw();
          
     glDisable(GL_TEXTURE_2D);
     glDisableClientState(GL_COLOR_ARRAY);
     glDisableClientState(GL_TEXTURE_COORD_ARRAY);
      //进行绘制
      world->DrawDebugData();
     // restore default GL states
     glEnable(GL_TEXTURE_2D);
     glEnableClientState(GL_COLOR_ARRAY);
     glEnableClientState(GL_TEXTURE_COORD_ARRAY);  
}
void  HelloWorld::update( float  dt){
     world->Step(dt,8,1);
     //同步精灵的物理动作
     for (b2Body* b = 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));
             sprite->setRotation(-1*CC_RADIANS_TO_DEGREES(b->GetAngle()));
         }
     }
}
CCScene* HelloWorld::scene()
{
     // 'scene' is an autorelease object
     CCScene *scene = CCScene::create();
             
     // 'layer' is an autorelease object
     HelloWorld *layer =  new  HelloWorld();
     // add layer as a child to scene
     scene->addChild(layer);
     layer->release();
     // return the scene
     return  scene;
}
/************************************************************************/
/* 触摸方法                                                                     */
/************************************************************************/
void  HelloWorld::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent){
     CCSetIterator it;
     CCTouch* touch;
     for (it = pTouches->begin();it!=pTouches->end();it++){
         touch = (CCTouch*)(*it);
         if (!touch){
             break ;
         }
         CCPoint location = touch->getLocation();
         //触摸后添加精灵
         addNewSpriteAtPosition(location);
     }
}
/************************************************************************/
/* 添加精灵                                                                     */
/************************************************************************/
void  HelloWorld::addNewSpriteAtPosition(CCPoint p){
     CCLOG( "Add sprite %0.2f x %02.f" ,p.x,p.y);
     CCNode* parent =  this ->getChildByTag(kTagParentNode);
     int  idx=(CCRANDOM_0_1()>0.5f?0:1);
     int  idy=(CCRANDOM_0_1()>0.5f?0:1);
     //随机创建精灵
     CCSprite* sprite = CCSprite::createWithTexture(m_pSpriteTexture,CCRectMake(32*idx,32*idy,32,32));
     sprite->setPosition(p);
     parent->addChild(sprite);
     //定义动态刚体
     b2BodyDef bodyDef;
     bodyDef.type = b2_dynamicBody;
     bodyDef.position.Set(p.x/PTM_RATIO,p.y/PTM_RATIO);
     bodyDef.userData = sprite;
     //创建刚体
     b2Body* body = world->CreateBody(&bodyDef);
     //创建长方形
     b2PolygonShape dynamicBox;
     dynamicBox.SetAsBox(sprite->getContentSize().width/PTM_RATIO/2,sprite->getContentSize().width/PTM_RATIO/2);
     //定义夹角
     b2FixtureDef fixtureDef;
     fixtureDef.shape = &dynamicBox;
     fixtureDef.density = 1.0f;  //密度
     fixtureDef.friction = 0.3f; //摩擦力
     body->CreateFixture(&fixtureDef);
          
}

素材来源与cocos2d-x自带demo

wKioL1MDKoTThQaHAABnByiZG-g802.jpg


本文转自xuzw13 51CTO博客,原文链接:http://blog.51cto.com/xuzhiwei/1360206,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值