动态的身体正在通过地面箱

时间:2013-03-09 12:40:50

标签: box2d cocos2d-x

我搜索了所有东西,但仍然没有得到为什么动态的身体不会与地面体碰撞 我的代码是

  _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();


     }

我是否需要为此编写联系人监听器。我知道如何使用联系人监听器,但仍然想知道是否使用它

期待回复

1 个答案:

答案 0 :(得分:2)

你的问题是你已经将玩家身体固定装置变成了传感器:

playerspriteShapeDef.isSensor = true;

同时增加positionIterations,例如:6。