我只调用了body-> CreateFixture(& fixtureDef)一次,但Testbed继续创建实体。这是我的代码:
#ifndef FOOTEST_H
#define FOOTEST_H
#define DEGTORAD 0.0174532925199432957f
#define RADTODEG 57.295779513082320876f
class FooTest : public Test
{
public:
FooTest() { } //do nothing, no scene yet
void Step(Settings* settings)
{
//run the default physics and rendering
Test::Step(settings);
b2BodyDef myBodyDef;
myBodyDef.type = b2_dynamicBody;
myBodyDef.position.Set(0, 20);
b2Body *dynamicBody = m_world->CreateBody(&myBodyDef);
b2PolygonShape polygonShape;
b2FixtureDef myFixtureDef;
myFixtureDef.shape = &polygonShape;
myFixtureDef.density = 1;
for(int i=0;i<4;i++){
b2Vec2 pos(sinf(i*90*DEGTORAD), cos(i*90*DEGTORAD));
polygonShape.SetAsBox(1, 1, pos, 0);
dynamicBody->CreateFixture(&myFixtureDef);
}
myBodyDef.type = b2_staticBody;
myBodyDef.position.Set(0, 0);
b2Body *staticBody = m_world->CreateBody(&myBodyDef);
b2EdgeShape edgeShape;
edgeShape.Set( b2Vec2(-15,0), b2Vec2(15,3) );
myFixtureDef.shape = &edgeShape;
staticBody->CreateFixture(&myFixtureDef);
}
static Test* Create()
{
return new FooTest;
}
};
#endif
我正在关注本教程: iForce2D 该教程的屏幕截图仅显示了创建的fixture的一个实例。我在这里错过了什么吗?
答案 0 :(得分:2)
“Testbed”会反复调用Step
,并且您在每次调用中都会创建实例
请注意,本教程在FooTest
构造函数中进行了所有设置,因此它只运行一次。
由于某种原因,您已将设置从构造函数移动到Step
函数
在该教程中稍早一点,它说“如果你没有做任何特殊的渲染或物理控制,你不需要覆盖Step()”。