現在、Cocos2d-Box2dプロジェクトは、b2Vec2を使用して、ゲームのエッジ用のバウンディングボックスを作成しています。このため、バウンディングボックスは、力の影響を受けないボディであるキネマティックボディには影響しません(つまり、ボディは通常、画面から飛び出します)。キネマティックボディを画面に接続する方法があるかどうかを確認しようとしています。そうでない場合は、誰かが画面の隅に静的なボディを備えたバウンディングボックスを作成する方法を説明してくれれば幸いです。
1 に答える
0
ここに2つの方法があります...いずれかを試してください
//メソッド-1
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0, 0);
b2Body *mGroundBody ;
mGroundBody = self.world->CreateBody(&groundBodyDef);
NSString *strId = @"Ground Body";
mGroundBody->SetUserData(strId);
b2EdgeShape groundBox;
//bottom
groundBox.Set(b2Vec2(0.0f,0.0f), b2Vec2(mS.width/PTM_RATIO,0.0f));
mGroundBody->CreateFixture(&groundBox,0);
// top
groundBox.Set(b2Vec2(0,mS.height/PTM_RATIO), b2Vec2(mS.width/PTM_RATIO, mS.height/PTM_RATIO));
mGroundBody->CreateFixture(&groundBox,0);
// left
groundBox.Set(b2Vec2(0,mS.height/PTM_RATIO), b2Vec2(0,0));
mGroundBody->CreateFixture(&groundBox,0);
// right
groundBox.Set(b2Vec2(mS.width/PTM_RATIO,mS.height/PTM_RATIO), b2Vec2(mS.width/PTM_RATIO,0));
mGroundBody->CreateFixture(&groundBox,0);
//メソッド-2
//create 4 box2d walls...
float bW = (IS_IPAD) ? (8) : 2 ;
//top
{
b2BodyDef bodyDef;
bodyDef.type = b2_staticBody;
bodyDef.position.Set((mS.width*0.5f)/PTM_RATIO, (mS.height)/PTM_RATIO);
bodyDef.linearDamping = 0.0f;
bodyDef.angularDamping = 0.0f;
bodyDef.userData = strId ;
b2PolygonShape box;
box.SetAsBox( ((mS.width*0.5f)/PTM_RATIO), (bW)/PTM_RATIO);
b2FixtureDef fixDef;
fixDef.shape = &box;
fixDef.density = 1.0f;
fixDef.friction = 0.1f;
fixDef.restitution = 1.0f;
fixDef.isSensor = false;
b2Body *topBody = self.world->CreateBody(&bodyDef);
topBody->CreateFixture(&fixDef);
}
//bottom
{
b2BodyDef bodyDef;
bodyDef.type = b2_staticBody;
bodyDef.position.Set((mS.width*0.5f)/PTM_RATIO, 0);
bodyDef.linearDamping = 0.0f;
bodyDef.angularDamping = 0.0f;
bodyDef.userData = strId ;
b2PolygonShape box;
box.SetAsBox( ((mS.width*0.5f)/PTM_RATIO), (bW)/PTM_RATIO);
b2FixtureDef fixDef;
fixDef.shape = &box;
fixDef.density = 1.0f;
fixDef.friction = 0.1f;
fixDef.restitution = 1.0f;
fixDef.isSensor = false;
b2Body *topBody = self.world->CreateBody(&bodyDef);
topBody->CreateFixture(&fixDef);
}
//left
{
b2BodyDef bodyDef;
bodyDef.type = b2_staticBody;
bodyDef.position.Set(0, (mS.height*0.5f)/PTM_RATIO);
bodyDef.linearDamping = 0.0f;
bodyDef.angularDamping = 0.0f;
bodyDef.userData = strId ;
b2PolygonShape box;
box.SetAsBox( ((bW)/PTM_RATIO), (mS.height*0.5f)/PTM_RATIO);
b2FixtureDef fixDef;
fixDef.shape = &box;
fixDef.density = 1.0f;
fixDef.friction = 0.1f;
fixDef.restitution = 1.0f;
fixDef.isSensor = false;
b2Body *topBody = self.world->CreateBody(&bodyDef);
topBody->CreateFixture(&fixDef);
}
//right
{
b2BodyDef bodyDef;
bodyDef.type = b2_staticBody;
bodyDef.position.Set((mS.width)/PTM_RATIO, (mS.height*0.5f)/PTM_RATIO);
bodyDef.linearDamping = 0.0f;
bodyDef.angularDamping = 0.0f;
bodyDef.userData = strId ;
b2PolygonShape box;
box.SetAsBox( ((bW)/PTM_RATIO), (mS.height*0.5f)/PTM_RATIO);
b2FixtureDef fixDef;
fixDef.shape = &box;
fixDef.density = 1.0f;
fixDef.friction = 0.1f;
fixDef.restitution = 1.0f;
fixDef.isSensor = false;
b2Body *topBody = self.world->CreateBody(&bodyDef);
topBody->CreateFixture(&fixDef);
}
于 2012-12-12T05:08:29.663 に答える