I'm working on an iPhone game, and the ground is a long, repeating rectangular sprite object that extends about 30 pixels above the bottom of the screen. I'm trying to align my ground edge shape 30 pixels so it when the player sprite lands on the ground from a jump, he will land on top of ground image. When I tried adding the following code, it didn't work:
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0, 30/PTM_RATIO);
b2Body *groundBody=world->CreateBody(&groundBodyDef);
b2EdgeShape groundLine;
groundLine.Set(b2Vec2(0, 30/PTM_RATIO), b2Vec2(screenSize.width/PTM_RATIO, 30/PTM_RATIO));
groundBody->CreateFixture(&groundLine, 0);
I realize I could probably use a rectangle shape for the ground, but 开发者_如何学运维since my player sprite is only colliding with the top of the image, I would rather get away with an edge shape.
When you set the positions of shapes (eg groundLine.Set() in your example) you are setting the position relative to the position of the body it will be attached to. So your body is already at y=30, then your shape will be another 30 above it.
For ground bodies I recommend just leaving the body position at 0,0 and then adding the fixtures at the position you want them - typically there is only one ground body.
I also recommend using the default debug draw so you can see what is really going on, would show you the problem instantly.
精彩评论