cocos2dx之Box2D
Box2D是與Cocos2d-x一起釋出的一套開源物理引擎,也是Cocos2d-x遊戲需要使用物理引擎時的首選。二者同樣提供C++開發介面,所使用的座標系也一致,因此Box2D與Cocos2d-x幾乎可以做到無縫對接。
Box2D是一套基於剛體模擬的物理引擎,它的核心概念為世界、物體、形狀、約束和關節.
void MyBox2DLayer::initLayer() {
CCSize size = CCDirector::sharedDirector()->getWinSize();
this->setTouchEnabled(true);
this->setAccelerometerEnabled(true);// 設定加速鍵
/*
*Init the box2d physics
*/
initPhysics();
//
CCSpriteBatchNode *batch = CCSpriteBatchNode::create("Images/ball-hd.png", 100);
m_SpriteNode = batch->getTexture();
this->addChild(batch, 0, KSPRITE_BATCH);
addAnNewSprite(ccp(size.width / 2.0f, size.height / 2.0f));
scheduleUpdate();// update the time,時刻更新物體狀態
}
void MyBox2DLayer::initPhysics() {
CCSize size = CCDirector::sharedDirector()->getWinSize();
b2Vec2 gravity;// step: 1
/*
|^(y) gravity ,if the second param is negative number that the sprite is running down(y is decreasing)or is running up(y is increasing)
|
|
|
|
|________________________________________________________>(x)
(0, 0)
*/
gravity.Set(0.0f, -10.0f); // direction of the sprite runs,base on the GL position,
world = new b2World(gravity);
//step: 2
world->SetAllowSleeping(true); // pause all ,init the box2d world
world->SetContinuousPhysics(true); // resume the box2d
//step: 3, defint the body
b2BodyDef groundbody;
groundbody.position.Set(0, 0);
b2Body *body = world->CreateBody(&groundbody);
//step: 3, define the edge
// define the 4 edges
b2EdgeShape edgeBox;
edgeBox.Set(b2Vec2(0, size.height / PTM_RATIO), b2Vec2(0, 0));
body->CreateFixture(&edgeBox, 0);
edgeBox.Set(b2Vec2(0, 0), b2Vec2(size.width / PTM_RATIO, 0));
body->CreateFixture(&edgeBox, 0);
edgeBox.Set(b2Vec2(0, size.height / PTM_RATIO), b2Vec2(size.width / PTM_RATIO, size.height / PTM_RATIO));
body->CreateFixture(&edgeBox, 0);
edgeBox.Set(b2Vec2(size.width / PTM_RATIO, size.height / PTM_RATIO), b2Vec2(size.width / PTM_RATIO, 0));
body->CreateFixture(&edgeBox, 0);
}
void MyBox2DLayer::addAnNewSprite(const CCPoint &point) {
CCSize size = CCDirector::sharedDirector()->getWinSize();
CCNode* parent = getChildByTag(KSPRITE_BATCH);
sprite = new MyBox2DSprite;
sprite->initWithTexture(m_SpriteNode);
sprite->setAnchorPoint(ccp(0.5f, 0.5f));
sprite->setPosition(point);
parent->addChild(sprite, 1);
sprite->retain();
//
b2BodyDef bodydef;
bodydef.type = b2_dynamicBody; // set the kind of the sprite
bodydef.position.Set(point.x / PTM_RATIO, point.y / PTM_RATIO);
//body
b2Body *body = world->CreateBody(&bodydef);
//shape
b2PolygonShape dynamicShape;
dynamicShape.SetAsBox(.5f, .5f);
b2FixtureDef fixtureDef;//set the 精靈新增夾具
fixtureDef.shape = &dynamicShape;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.3f;
fixtureDef.restitution = 0.8f;
//set bodydef values;
body->CreateFixture(&fixtureDef);
sprite->setPhysicsBody(body);
}
void MyBox2DLayer::update(float dt) {
int velocityIterations = 1;
int positionIterations = 1;
world->Step(dt, velocityIterations, positionIterations);
}
//物體
MyBox2DSprite::MyBox2DSprite() {
}
void MyBox2DSprite::setPhysicsBody(b2Body * body) {
m_pBody = body;
}
bool MyBox2DSprite::isDirty(void) {
return true;
}
CCAffineTransform MyBox2DSprite::nodeToParentTransform(void) {
b2Vec2 pos = m_pBody->GetPosition();
float x = pos.x * PTM_RATIO;
float y = pos.y * PTM_RATIO;
if ( isIgnoreAnchorPointForPosition() ) {
x += m_tAnchorPointInPoints.x;
y += m_tAnchorPointInPoints.y;
}
// Make matrix
float radians = m_pBody->GetAngle();
float c = cosf(radians);
float s = sinf(radians);
if( ! CCPoint::CCPointEqualToPoint(m_tAnchorPointInPoints, CCPointZero) ){
x += c*-m_tAnchorPointInPoints.x + -s*-m_tAnchorPointInPoints.y;
y += s*-m_tAnchorPointInPoints.x + c*-m_tAnchorPointInPoints.y;
}
// Rot, Translate Matrix
m_tTransform = CCAffineTransformMake( c, s,
-s, c,
x, y );
return m_tTransform;
}
相關文章
- cocos2dx之CCTextureCache
- cocos2dx之CCSpriteFrameCache
- cocos2dx之儲存截圖圖片
- cocos2dx之引入Sqlite3資料庫SQLite資料庫
- cocos2dx之CCUserDefault及其資料進行加密解密加密解密
- cocos2dx之獲取當前的輸入法語言
- CoCos2dx開發:中文亂碼
- cocos2dx 場景切換特效特效
- cocos2dx遮蔽列表的觸控
- cocos2dx 很好的原始碼分析博文原始碼
- Cocos2dx中精靈(CCSprite)、動畫建立動畫
- COCOs2dx中KEY_KP與KEY的區別
- cocos2dx update scheduleUpdate to update or schedule(schedule_selector(fun),dt)
- win10搭建cocos2dx開發環境怎麼安裝_win10搭建cocos2dx開發環境的步驟Win10開發環境
- HTML5之2D物理引擎 Box2D for javascript Games 系列 翻外篇--如何結合createJS應用box2d.jsHTMLJavaScriptGAMJS
- android層java如何呼叫cocos2dx c++程式碼 步驟AndroidJavaC++
- 2D物理引擎 Box2D for javascript Games 第六章 關節和馬達JavaScriptGAM
- 2D物理引擎 Box2D for javascript Games 第七章 子彈和感應器JavaScriptGAM
- CoCos2dx開發:更換匯出的app名稱和圖示APP
- CoCos2dx開發:tile地圖繪製和Tiled工具的基本使用地圖
- 2D物理引擎 Box2D for javascript Games 第四章 將力作用到剛體上JavaScriptGAM
- cocos2dx遊戲開發——微信打飛機學習筆記(九)——BulletLayer的搭建遊戲開發筆記
- CoCos2dx開發:PC端除錯執行正常但打包apk檔案後在手機上點選閃退除錯APK
- 漢字之美,拼音之韻
- PHP之string之ord()函式使用PHP函式
- 深入Spring之IOC之載入BeanDefinitionSpringBean
- JavaScript之thisJavaScript
- 若之
- 計算方法之祖沖之的精度
- Kubernetes安裝之八:配置master之schedulerAST
- vue 兄弟元件之間傳值之busVue元件
- 《碼農翻身》之浪潮之巔的WebWeb
- 揭秘ORACLE備份之----RMAN之五(CATALOG)Oracle
- React之元件(component)之間的通訊React元件
- PHP之string之str_split()函式使用PHP函式
- AI犯錯誰之過?切勿盲目相信之AI
- Java常用資料結構之Set之TreeSetJava資料結構
- PHP之string之str_pad()函式使用PHP函式