0

私はすべてを検索しましたが、ダイナミックボディがグラウンドボディと衝突しない理由がまだわかりません私のコードは

  _debugDraw = new GLESDebugDraw( PTM_RATIO );
  b2Vec2 gravity = b2Vec2(0.0f, -8.0f); 
  world = new b2World(gravity);
  world->SetContinuousPhysics(true);


       // Enable debugging flags.
       uint32 flags = 0;
       flags += b2Draw::e_shapeBit;
       //flags += b2Draw::e_jointBit;
       //flags += b2Draw::e_aabbBit;
       //flags += b2Draw::e_pairBit;
       //flags += b2Draw::e_centerOfMassBit;
       _debugDraw->SetFlags(flags); 
              world->SetDebugDraw(_debugDraw); 

     bool doSleep = false;

   world->SetAllowSleeping(doSleep);
  //////////////////
   b2BodyDef myBodyDef;
   myBodyDef.type = b2_staticBody;

  myBodyDef.position.Set(0, 0);
  b2Body* staticBody = world->CreateBody(&myBodyDef);
  b2PolygonShape polygonShape;
  b2FixtureDef myFixtureDef;
  myFixtureDef.shape = &polygonShape;
  myFixtureDef.density = 1;
  myFixtureDef.friction=0.3f;
//add four walls to the static body
  polygonShape.SetAsBox( 5.5, 0.5, b2Vec2(6, 2), 0);//ground
  staticBody->CreateFixture(&myFixtureDef);



  polygonShape.SetAsBox( 5.5,0.5, b2Vec2(6, 8), 0);//ceiling
  staticBody->CreateFixture(&myFixtureDef);

  polygonShape.SetAsBox( 0.5, 3, b2Vec2(0.6, 5), 0);//left wall
  staticBody->CreateFixture(&myFixtureDef);

  polygonShape.SetAsBox( 0.5, 3, b2Vec2(11.5, 5), 0);//right wall
  staticBody->CreateFixture(&myFixtureDef);

   ///////////////
    b2BodyDef playerspriteBodyDef;
    playerspriteBodyDef.type = b2_dynamicBody;
    playerspriteBodyDef.position.Set( 4,4);
//  playerspriteBodyDef.userData = playerSprite;
    playerspriteBody = world->CreateBody(&playerspriteBodyDef);

    b2PolygonShape playerspriteShape;
    playerspriteShape.SetAsBox(1,1);

    b2FixtureDef playerspriteShapeDef;
    playerspriteShapeDef.shape = &playerspriteShape;
    playerspriteShapeDef.density = 10.0;
    playerspriteShapeDef.friction=0.3;
    playerspriteShapeDef.isSensor = true;
    playerspriteBody->CreateFixture(&playerspriteShapeDef);
    //////////////////////////
    b2BodyDef obstacle1spriteBodyDef;
    obstacle1spriteBodyDef.type = b2_dynamicBody;

    obstacle1spriteBodyDef.position.Set(10,4);  
    obstacle1spriteBody = world->CreateBody(&obstacle1spriteBodyDef);

    b2PolygonShape obstacle1spriteShape;
    obstacle1spriteShape.SetAsBox(0.5,0.5);

    b2FixtureDef obstacle1spriteShapeDef;
    obstacle1spriteShapeDef.friction=0.3;
    obstacle1spriteShapeDef.shape = &obstacle1spriteShape;
    obstacle1spriteShapeDef.density = 10.0;
    obstacle1spriteShapeDef.isSensor = true;
    obstacle1spriteBody->CreateFixture(&obstacle1spriteShapeDef);
    contactListener = new MyContactListener();
    world->SetContactListener(contactListener);


schedule( schedule_selector(HelloWorld::tick) );
    bRet = true;
 } while (0);

return bRet;
 }

 void HelloWorld::draw()
 {
glDisable(GL_TEXTURE_2D);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);

world->DrawDebugData();

// restore default GL states
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);    

    } 

      void HelloWorld::tick(float dt)
  {

int velocityIterations = 8;//8
int positionIterations = 1;//1

// Instruct the world to perform a single step of simulation. It is
// generally best to keep the time step and iterations fixed.
world->Step(dt, velocityIterations, positionIterations);
world->DrawDebugData();


     }

これには連絡先リスナーを書く必要がありますか。コンタクトリスナーの使い方は知っているが、使うかどうか迷っている

返信をお待ちしております

4

1 に答える 1

2

問題は、プレーヤーのボディフィクスチャをセンサーにしたことです。

playerspriteShapeDef.isSensor = true;

また、positionIterationsを増やします(例:6)。

于 2013-03-09T12:45:16.093 に答える