【发布时间】:2012-02-05 17:40:10
【问题描述】:
我在使用 Box2D(在 iOS 下)时遇到了这个非常奇怪的问题。我有一个放在屏幕底部的身体。如果我使用body->SetType(b2_staticBody) 将主体从 b2_dynamicBody 设置为 b2_staticBody,然后将其设置回 b2_dynamicBody,它会在屏幕底部快速落下(我可以看到它向下移动了几帧),然后被推回穿过地板并正确地休息。
这种行为似乎只发生在屏幕底部。与其他坐在一起的身体做同样的事情不会产生这种行为。
我将屏幕边缘定义如下(screenRect 已经调整到世界空间):
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0,0);
b2Body* groundBody = globalWorld->CreateBody(&groundBodyDef);
b2PolygonShape groundBox;
b2FixtureDef boxShapeDef;
boxShapeDef.shape = &groundBox;
groundBox.SetAsEdge(b2Vec2(screenRect.p1.x,screenRect.p1.y), b2Vec2(screenRect.p2.x, screenRect.p1.y));
groundBody->CreateFixture(&boxShapeDef);
groundBox.SetAsEdge(b2Vec2(screenRect.p1.x,screenRect.p1.y), b2Vec2(screenRect.p1.x, screenRect.p2.y));
groundBody->CreateFixture(&boxShapeDef);
groundBox.SetAsEdge(b2Vec2(screenRect.p1.x,screenRect.p2.y), b2Vec2(screenRect.p2.x, screenRect.p2.y));
groundBody->CreateFixture(&boxShapeDef);
groundBox.SetAsEdge(b2Vec2(screenRect.p2.x,screenRect.p2.y), b2Vec2(screenRect.p2.x, screenRect.p1.y));
groundBody->CreateFixture(&boxShapeDef);
最奇怪的部分是,我将身体从动态切换到静态的次数越多,在被推回之前,它在地板上掉得越远。
除了从动态到静态之外,我没有改变身体的其他任何东西。如果我移动身体并将其放回地板上,开始将其从动态切换到静态,它会重置它落入地板的数量,但我切换的次数越多,它就会再次堆积。
除非我创建不正确,否则我无法弄清楚为什么会像这样从地板上掉下来。游戏中的其他一切都运行良好。
【问题讨论】: