我正在使用 Box2D 在 Cocos2d 中开发游戏。目前我已经建立了一个有一些界限的系统。他们是为了一个球。我希望流星从屏幕外进入并击打球。
边界在我的 init 方法中定义如下:
// Define the ground body.
b2BodyDef groundBodyDef;
groundBodyDef.type = b2_staticBody;
groundBodyDef.position.Set(0, 0); // bottom-left corner
// Call the body factory which allocates memory for the ground body
// from a pool and creates the ground box shape (also from a pool).
// The body is also added to the world.
b2Body* groundBody = world->CreateBody(&groundBodyDef);
// Define the ground box shape.
b2PolygonShape groundBox;
// bottom
groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(screenSize.width/PTM_RATIO,0));
groundBody->CreateFixture(&groundBox,0);
// top
groundBox.SetAsEdge(b2Vec2(0,screenSize.height/PTM_RATIO), b2Vec2(screenSize.width/PTM_RATIO,screenSize.height/PTM_RATIO));
groundBody->CreateFixture(&groundBox,0);
// left
groundBox.SetAsEdge(b2Vec2(0,screenSize.height/PTM_RATIO), b2Vec2(0,0));
groundBody->CreateFixture(&groundBox,0);
// right
groundBox.SetAsEdge(b2Vec2(screenSize.width/PTM_RATIO,screenSize.height/PTM_RATIO), b2Vec2(screenSize.width/PTM_RATIO,0));
groundBody->CreateFixture(&groundBox,0);
//Collision filter stuff here. Not working.
b2Filter filter;
filter.groupIndex = -2;
groundBody->GetFixtureList()[0].SetFilterData(filter);
我的球定义如下:
//Physics object.
b2BodyDef ballBodyDef;
ballBodyDef.position.Set(ball.position.x/PTM_RATIO, ball.position.y/PTM_RATIO);
ballBodyDef.type = b2_dynamicBody;
ballBodyDef.userData = ball;
ballBodyDef.bullet = true;
ballBody = world->CreateBody(&ballBodyDef);
b2CircleShape ballShape;
ballShape.m_radius = 10.0/PTM_RATIO;
b2FixtureDef ballFix;
ballFix.restitution = 1.1f;
ballFix.density = 0.0f;
ballFix.shape = &ballShape;
ballBody->CreateFixture(&ballFix);
流星是用两种方法制作的,较短的方法是放置流星。较长的人实际上使用较短的人给出的点来创建它们:
-(void)createMeteorAtPoint:(CGPoint)point {
//Create particle sprite representation.
CCParticleMeteor *meteor = [[CCParticleMeteor alloc] initWithTotalParticles:200];
meteor.gravity = ccp(-200,0);
meteor.life = 0.5;
meteor.lifeVar = 0.25;
meteor.position = point;
meteor.tag = 2;
meteor.startSize = 5.0;
meteor.startSizeVar = 3.0;
[self addChild:meteor];
//Make meteor physics body.
b2BodyDef meteorBodyDef;
meteorBodyDef.position.Set(point.x/PTM_RATIO, point.y/PTM_RATIO);
meteorBodyDef.type = b2_dynamicBody;
meteorBodyDef.userData = meteor;
b2Body *meteorBody = world->CreateBody(&meteorBodyDef);
meteorBody->SetBullet(true);
b2CircleShape meteorShape;
meteorShape.m_radius = 7.5/PTM_RATIO;
b2FixtureDef meteorFix;
meteorFix.shape = &meteorShape;
meteorFix.density = 1;
//Give it the same negative group index of the boundaries to prevent collision.
//Not working.
meteorFix.filter.groupIndex = -2;
meteorBody->CreateFixture(&meteorFix);
//Give it a motion.
b2Vec2 F;
F.Set(CCRANDOM_0_1()*2, 0);
meteorBody->SetLinearVelocity(F);
}
//Create a bunch of meteors that will sweep from left to right.
-(void)createMeteors {
for (int i = 0; i < 8*40; i += 20) {
[self createMeteorAtPoint:ccp(-40.0f, i + 2)];
}
NSLog(@"HERE");
}
我可以看到物理对象堆积在屏幕的一侧,因为我设置了调试标志。但是一个:尽管是相同的负组指数,但他们并没有越过边界。第二:球决定它不喜欢右边的墙,然后穿过它。
如果你认为我遗漏了一些重要的东西,请告诉我。