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 回答