原文地址:
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);
这样便添加了碰撞 监听了