原文地址:
http://blog.csdn.net/a8467562/article/details/7580443
1 : 首先 是 構建 世界 ,即b2World 你也可能用到 debug 調試:GLESDebugDraw
b2Vec2 gravity;
gravity.Set(0.0f, -9.8f);
_world = new b2World(gravity, true);
構建 需要兩個參數 ,第一個為 世界中 的重力 參數,這里設置為:-9.8f,第二個參數為 是否設置 沒有碰撞的物體處于 休眠狀態
下面的代碼是需要調試時用到了,
GLESDebugDraw *_render;
//---------------
_render = new GLESDebugDraw([Box2DHelper pointsPerMeter]);
_world->SetDebugDraw(_render);
uint32 flags = 0;
flags += b2Draw::e_shapeBit;
_render->SetFlags(flags);
上面代碼一般會放在 下面代碼之間,
#ifdef DRAW_BOX2D_WORLD
#endif
意思是說如果 你定義了DRAW_BOX2D_WORLD 便執行 里面 的代碼,如果沒有定義則不執行
定義實現
#define DRAW_BOX2D_WORLD
////-----------------------
#ifdef DRAW_BOX2D_WORLD
_render=newGLESDebugDraw(1/32.0);
_world->SetDebugDraw(_render);
uint32 flags = 0;
flags += b2Draw::e_shapeBit;
_render->SetFlags(flags);
#endif
到目前為止 就已經定義了 box2d 的虛擬事件
為世界 創建 邊界 ,即 別讓 物體走出屏幕之外就行了
b2BodyDef groundBodyDef;
groundBodyDef.position.Set (0, 0); // 左下覬
// 創建物體對象
b2Body* groundBody = world->CreateBody(&groundBodyDef);
// 定丿大地的幾何尺寸,本例就是 4 條邊。
b2PolygonShape groundBox;
// 設定“下邊”坐標,創建返條線
groundB ox.Se tAsEdge(b2Vec2(0,0),b2Vec2(scree nSize.width/PT M_RAT IO,0) );
groundBody->CreateFixture( &groundBox ,0);
//設定“上邊”坐標,創建返條線
groundBox.SetAsEdge(b2Vec2 (0,screenSize.height /PTM_ RATIO),b2Vec2( screenSize.width/PT M_RATIO,screenSize.height /PTM_ RATIO ));
groundBody-> Creat eFixture( &groundBox ,0);
//設定“左邊”坐標,創建返條線
groundBox.Se tAsEdge(b2Vec2(0,screenSize.height /PTM_ RATIO),b2Vec2(0,0)) ;
groundBody->CreateFixture(&groundBox,0);
//設定“史邊”坐標,創建返條線
groundBox.SetAsEdge(b2Vec2 (screenSize.width/PT M_RAT IO,screenSize.height/ PTM_RATIO) ,b2Vec2( screenSize width/PTM_RATI O,0)) ;
groundBody->CreateFixture(&groundBox ,0);
這里順便要說一下 的是PTM_RATIO ,這個數一般定義為: 32.0,在box 世界中 是以 米 為單位的,這里是將坐標兌換為box世界中的米,即除以 PTM_RATIO
2:創建 世界中主角人物 ,
b2Body * _body;
b2BodyDef bd;
bd.type = b2_dynamicBody;//
bd.linearDamping = 0.05f;
bd.fixedRotation = true;
// start position
CGPoint p = ccp(0, _game.screenH/2+_radius);
CCLOG(@"start position = %f, %f", p.x, p.y);
bd.position.Set(p.x * [Box2DHelper metersPerPoint], p.y * [Box2DHelper metersPerPoint]);//此為設置物體的坐標,
_body = _game.world->CreateBody(&bd);
b2CircleShape shape;//設置 形狀,次為 圓形
shape.m_radius = _radius * 1/32.0;
b2FixtureDef fd;//
fd.shape = &shape;
fd.density = 0.3f;//質量
fd.restitution = 0; // bounce
fd.friction = 0;//不反彈,最大為 1
_body->CreateFixture(&fd);
/*
1)靜態物體(b2_staticBody)。質量為 0,丌可以秱勱,通帪模擬我們游戲的物理邊
界:大地、墻壁等。
2)平臺物體(b2_kinematicBody)。按照固定路線運勱的物體,比如說電梯,運勱的
滾梯,運行的火車等等。
3)動態物體(b2_dynamicBody)。我們最帪見的精靈對象對應的物體。
*/
接下來創建 金幣 道具
CGPoint setPoint=ccp(position.x/2 , (position.y+20)/2 );
CCSprite * sprite =[CCSprite spriteWithFile:@"collect_number1.png"];
sprite.position=ccp(setPoint.x, setPoint.y);
sprite.tag=200;
[self addChild:sprite];
b2BodyDef testBodyDef;
testBodyDef.type = b2_staticBody;
testBodyDef.userData=sprite;
testBodyDef.position.Set(setPoint.x/PTM_RATIO, (setPoint.y)/PTM_RATIO);
b2Body * testBody = world->CreateBody(&testBodyDef);
b2CircleShape testBodyShape;
b2FixtureDef testFixtureDef;
testBodyShape.m_radius =5.0/PTM_RATIO;
testFixtureDef.isSensor=YES;
testFixtureDef.shape =&testBodyShape;
testBody->CreateFixture(&testFixtureDef);
這里 需要說 的是 isSensor 屬性 ,如果你想讓物體檢測到碰撞,但沒有碰撞效果(沒有反彈效果),就將 isSensor 設置為 YES ,
好了,現在說一下 重點 ,碰撞問題
下面我實現的 碰撞監聽 類
在 .h 中
#import "Box2D.h"
#import <vector>
#import <algorithm>
#define kMaxAngleDiff 2.4f // in radians
@class Hero;
struct MyContact {
b2Fixture *fixtureA;
b2Fixture *fixtureB;
bool operator==(const MyContact& other) const
{
return (fixtureA == other.fixtureA) && (fixtureB == other.fixtureB);
}
};
class HeroContactListener : public b2ContactListener {
public:
Hero *_hero;
std::vector<MyContact>_contacts;
HeroContactListener(Hero* hero);
~HeroContactListener();
void BeginContact(b2Contact* contact);
void EndContact(b2Contact* contact);
void PreSolve(b2Contact* contact, const b2Manifold* oldManifold);
void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse);
};
實現 監聽 一定要繼承b2ContactListener ,并實現其方法,然后 在 _world 世界中添加 監聽 就可以了
.mm文件中的 內容為:
#import "HeroContactListener.h"
#import "Hero.h"
#import "GameConfig.h"
HeroContactListener::HeroContactListener(Hero* hero) {
_hero = [hero retain];
}
HeroContactListener::~HeroContactListener() {
[_hero release];
}
void HeroContactListener::BeginContact(b2Contact* contact) {
//在這里 檢測碰撞 金幣
CCSprite * sprite=(CCSprite *)contact->GetFixtureA()->GetBody()->GetUserData();
if (sprite !=NULL) {
}
MyContact myContact = { contact->GetFixtureA(), contact->GetFixtureB() };
_contacts.push_back(myContact);
}
void HeroContactListener::EndContact(b2Contact* contact) {
MyContact myContact = { contact->GetFixtureA(), contact->GetFixtureB() };
std::vector<MyContact>::iterator pos;
pos = std::find(_contacts.begin(), _contacts.end(), myContact);
if (pos != _contacts.end()) {
_contacts.erase(pos);
}
}
void HeroContactListener::PreSolve(b2Contact* contact, const b2Manifold* oldManifold) {
b2WorldManifold wm;
contact->GetWorldManifold(&wm);
b2PointState state1[2], state2[2];
b2GetPointStates(state1, state2, oldManifold, contact->GetManifold());
if (state2[0] == b2_addState) {
const b2Body *b = contact->GetFixtureB()->GetBody();
b2Vec2 vel = b->GetLinearVelocity();
float va = atan2f(vel.y, vel.x);
float na = atan2f(wm.normal.y, wm.normal.x);
if (na - va > kMaxAngleDiff) {
[_hero hit];
}
}
}
void HeroContactListener::PostSolve(b2Contact* contact, const b2ContactImpulse* impulse) {
}
在這里 需要說的是在 監聽中不能 刪除 碰撞物體
循環 執行 次方法 [self schedule:@selector(tick:)];
-(void)tick:(ccTime)time{
// Loop through all of the box2d bodies that are currently colliding, that we have
// gathered with our custom contact listener...
std::vector<b2Body *>toDestroy;
std::vector<MyContact>::iterator pos;
for(pos = _contactListener->_contacts.begin(); pos != _contactListener->_contacts.end(); ++pos) {
MyContact contact = *pos;
// Get the box2d bodies for each object
b2Body *bodyA = contact.fixtureA->GetBody();
if (bodyA->GetUserData() != NULL) {
CCSprite *spriteA = (CCSprite *) bodyA->GetUserData();
// Is sprite A a cat and sprite B a car? If so, push the cat on a list to be destroyed...
if (spriteA.tag == 200) {
toDestroy.push_back(bodyA);
//播放 碰撞 道具 聲音
[[SimpleAudioEngine sharedEngine] playEffect:GetJinBiSound];
NSLog(@"碰到一個 金幣 。。。。。");
[GameDate shareGameDate].jinBiNumber++;
_game.jinBiLabel.string=[NSString stringWithFormat:@"%d",[GameDate shareGameDate].jinBiNumber];
}
// Is sprite A a car and sprite B a cat? If so, push the cat on a list to be destroyed...
}
}
// Loop through all of the box2d bodies we wnat to destroy...
std::vector<b2Body *>::iterator pos2;
for(pos2 = toDestroy.begin(); pos2 != toDestroy.end(); ++pos2) {
b2Body *body = *pos2;
// See if there's any user data attached to the Box2D body
// There should be, since we set it in addBoxBodyForSprite
if (body->GetUserData() != NULL) {
// We know that the user data is a sprite since we set
// it that way, so cast it...
CCSprite *sprite = (CCSprite *) body->GetUserData();
// Remove the sprite from the scene
[sprite removeFromParentAndCleanup:YES];
}
// Destroy the Box2D body as well
_game.world->DestroyBody(body);
}
}
這樣 當box 世界中 發生碰撞了,就會監聽到,并可以 刪除掉 碰撞的金幣精靈 和 對應的 box世界中物體
最后 就是用 在世界中添加監聽了
_contactListener = new HeroContactListener(self);
_game.world->SetContactListener(_contactListener);
這樣便添加了碰撞 監聽了