1

最近、Box2D バージョン 2.1 を Allegro5 と組み合わせて使用​​し始めました。現在、地面と4つのボックスでテストを構築しました。3つの箱が積み上げられ、もう1つの箱がつぶれてひっくり返る。このデモンストレーション中に、2 つの不具合があることに気付きました。

1 つは、Box2D "SetAsBox( width, height )" でボックスを作成すると、allegro を使用して画面に描画される通常のボックスの半分のサイズしか得られないことです。例: Box2D で、(15, 15) のサイズのボックスを作成します。allegro を使用して形状を描画するときは、y に -15 のオフセットを作成し、形状をそのサイズの 2 倍にスケーリングする必要があります。

もう 1 つの問題は、衝撃によってボックスが回転している間の衝突検出中です。ほとんどの正方形は地面にぶつかりますが、一部の正方形は高さが地面からオフセットされて浮いています。

これが私の箱を作るためのコードです:

cBox2D::cBox2D( int width, int height ) {

  // Note: In Box2D, 30 pixels = 1 meter
  velocityIterations  = 10;
  positionIterations  = 10;
  worldGravity  = 9.81f;
  timeStep    = ( 1.0f / 60.0f );
  isBodySleep   = false;

  gravity.Set( 0.0f, worldGravity );
  world = new b2World( gravity, isBodySleep );

  groundBodyDef.position.Set( 0.0f, height ); // ground location
  groundBody = world->CreateBody( &groundBodyDef );

  groundBox.SetAsBox( width, 0.0f ); // Ground size
  groundBody->CreateFixture( &groundBox, 0.0f );

 }

 cBox2D::~cBox2D( void ) {}

 void cBox2D::makeSquare( int width, int height, int locX, int locY, float xVelocity, float yVelocity, float angle, float angleVelocity ) {

  sSquare square;

  square.bodyDef.type = b2_dynamicBody;
  square.bodyDef.position.Set( locX, locY ); // Box location
  square.bodyDef.angle = angle; // Box angle
  square.bodyDef.angularVelocity = angleVelocity;
  square.bodyDef.linearVelocity.Set( xVelocity, yVelocity ); // Box Velocity
  square.body = world->CreateBody( &square.bodyDef );

  square.dynamicBox.SetAsBox( width, height ); // Box size
  square.fixtureDef.shape = &square.dynamicBox;
  square.fixtureDef.density = 1.0f;
  square.fixtureDef.friction = 0.3f;
  square.fixtureDef.restitution = 0.0f; // Bouncyness
  square.body->CreateFixture( &square.fixtureDef );

  squareVec.push_back( square );

 }

 int cBox2D::getVecSize( void ) {
  return squareVec.size();
 }

 b2Body* cBox2D::getSquareAt( int loc ) {
  return squareVec.at( loc ).body;
 }

 void cBox2D::update( void ) {

  world->Step(timeStep, velocityIterations, positionIterations);
  world->ClearForces();

 }

編集: 最初の問題を説明してくれた Chris Burt-Brown に感謝します。2 番目の問題については、良いアイデアでしたが、解決しませんでした。あなたが私に示した両方の丸め方法を試しました。

編集:2番目の問題に対する答えを見つけたと思います。Allegro には OpenGL とは異なる座標系があることが判明しました。その結果、-gravity の代わりに +gravity を実行する必要があり、Box2D が不安定になり、動作がおかしくなりました。

編集:悪いことに、それが問題だと思っていましたが、何も変わらなかったことがわかりました。

4

1 に答える 1

4

それは実際にSetAsBox(halfwidth, halfheight)です。奇妙に聞こえるかもしれませんが、中を見てくださいSetAsBox。パラメータ 15 と 15 を渡すと、角が (-15,-15) と (15,15) のボックス、つまりサイズが 30x30 のボックスが得られます。

最適化を意図したものだと思いますが、かなりばかげています。

round他の問題の原因はわかりませんが、Allegro でボックスを描画するときは、座標を指定したときに修正されるかどうかを確認してください。(それがうまくいかない場合は、試してくださいceil。)

于 2011-01-25T20:37:26.430 に答える