1

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.

Scott
  • 324
  • 2
  • 4
  • 15
  • This seems fine. There must be some other issue with the code. What issue occur? Your player passes through the ground or what?? what body type (dynamic?) of your player? and how are you moving your player? – Tayyab Apr 04 '11 at 08:26
  • I'm dropping my player (dynamic body) down with the force of gravity. it ignores the 30 y point and instead collides with the 0 y point – Scott Apr 04 '11 at 15:36

1 Answers1

4

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.

iforce2d
  • 8,194
  • 3
  • 29
  • 40