Box 2D - 有没有办法将地面 b2EdgeShape 向上移动
我正在开发一款 iPhone 游戏,地面是一个长的、重复的矩形精灵对象,在屏幕底部上方延伸约 30 像素。我正在尝试将地面边缘形状对齐 30 像素,这样当玩家精灵从跳跃着陆到地面时,他将着陆到地面图像的顶部。当我尝试添加以下代码时,它不起作用:
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'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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您设置形状的位置(例如示例中的 groundLine.Set())时,您正在设置相对于它将附加到的主体位置的位置。所以你的身体已经在 y=30 处,那么你的形状将在它上面再高出 30。
对于地面物体,我建议将物体位置保留在 0,0,然后在您想要的位置添加固定装置 - 通常只有一个地面物体。
我还建议使用默认的调试绘制,这样您就可以看到到底发生了什么,并立即向您显示问题。
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.