我有一个桶,我想在其中添加 box2d body 。不是在整个桶上,而是在左、右和底部,这样我就可以把球扔进桶里。这是我的桶。我在桶的左侧和右侧添加了 box2d 主体。像这样
它对我来说很好但是当我在底部添加身体时,我的游戏就会崩溃。
这是我在带有注释底部主体的桶上添加 2 个主体的代码。
-(b2Body *) createBucket
{
CCSprite* bucket = [CCSprite spriteWithFile:@"simple-bucket-md.png"];
[self addChild:bucket z:3];
b2Body* b_bucket;
//set this to avoid updating this object in the tick schedule
bucket.userData = (void *)YES;
b2BodyDef bodyDef;
bodyDef.type = b2_staticBody;
CGPoint startPos = ccp(400,150);
bodyDef.position = [self toMeters:startPos];
bodyDef.userData = bucket;
bodyDef.gravityScale = 0;
b2PolygonShape dynamicBox;
//----------------------------------
// THis IS body for Left Side
//----------------------------------
int num = 5;
b2Vec2 verts[] = {
b2Vec2(-29.1f / PTM_RATIO, 25.0f / PTM_RATIO),
b2Vec2(-25.4f / PTM_RATIO, -14.9f / PTM_RATIO),
b2Vec2(-18.7f / PTM_RATIO, -14.9f / PTM_RATIO),
b2Vec2(-21.8f / PTM_RATIO, 26.7f / PTM_RATIO),
b2Vec2(-28.9f / PTM_RATIO, 25.1f / PTM_RATIO)
};
dynamicBox.Set(verts, num);
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicBox;
fixtureDef.friction = 0.7;
fixtureDef.density = 10.0f;
fixtureDef.restitution = 0.7;
b_bucket = world->CreateBody(&bodyDef);
b_bucket->CreateFixture(&fixtureDef);
//----------------------------------
// THis IS body for Right Side
//----------------------------------
int num1 = 5;
b2Vec2 verts1[] = {
b2Vec2(16.8f / PTM_RATIO, 27.0f / PTM_RATIO),
b2Vec2(15.9f / PTM_RATIO, -11.5f / PTM_RATIO),
b2Vec2(22.1f / PTM_RATIO, -10.7f / PTM_RATIO),
b2Vec2(24.6f / PTM_RATIO, 26.9f / PTM_RATIO),
b2Vec2(16.9f / PTM_RATIO, 26.7f / PTM_RATIO)
};
dynamicBox.Set(verts1, num1);
fixtureDef.shape = &dynamicBox;
b_bucket-> CreateFixture(&fixtureDef);
//----------------------------------
// THis IS body for Bottom
//----------------------------------
/*
int num2 = 5;
b2Vec2 verts2[] = {
b2Vec2(-23.0f / PTM_RATIO, -21.6f / PTM_RATIO),
b2Vec2(18.9f / PTM_RATIO, -21.0f / PTM_RATIO),
b2Vec2(18.2f / PTM_RATIO, -26.1f / PTM_RATIO),
b2Vec2(-22.8f / PTM_RATIO, -25.9f / PTM_RATIO),
b2Vec2(-23.0f / PTM_RATIO, -21.7f / PTM_RATIO)
};
dynamicBox.Set(verts2, num2);
fixtureDef.shape = &dynamicBox;
b_bucket-> CreateFixture(&fixtureDef);
*/
return b_bucket;
}