我正在使用带有cocos2dx的Box2d。我在每个帧上创建一个动态的身体,并在创建150个动态身体后,游戏性能下降。这是我用来执行此操作的代码:
Settings()
{
viewCenter.Set(0.0f, 20.0f);
hz = 10.0f;
velocityIterations = 8;
positionIterations = 3;
drawShapes = 1;
drawJoints = 1;
drawAABBs = 0;
drawContactPoints = 0;
drawContactNormals = 0;
drawContactImpulse = 0;
drawFrictionImpulse = 0;
drawCOMs = 0;
drawStats = 0;
drawProfile = 0;
enableWarmStarting = 1;
enableContinuous = 1;
enableSubStepping = 0;
enableSleep = 1;
pause = 0;
singleStep = 0;
}
class Tumbler : public Test
{
public:
enum
{
e_count = 800
};
Tumbler()
{
b2BodyDef bd;
bd.allowSleep = true;
bd.gravityScale = 9.8;
ground = m_world->CreateBody(&bd);
m_count = 0;
m_count_obj = 0;
bd2.type = b2_dynamicBody;
bd2.allowSleep = true;
bd2.gravityScale = 9.8f;
bd2.position.Set(512,600);
shape_.m_p.Set(0,0);
shape_.m_radius = 4;
body = NULL;
}
void Step(Settings* settings,cocos2d::Sprite* data)
{
Test::Step(settings,data);
if (m_count_obj<=e_count && 0 == (m_count%15))
{
m_count_obj++;
body = m_world->CreateBody(&bd2);
body->SetUserData(data);
body->SetBullet(false);
body->SetLinearVelocity(b2Vec2(150,1500));
body->CreateFixture(&shape_, 1.0f);
}
++m_count;
}
static Test* Create()
{
return new Tumbler;
}
b2Body* ground = NULL;
int32 m_count;
int32 m_count_obj;
b2EdgeShape shape;
b2BodyDef bd2;
b2CircleShape shape_;
b2Body* body;
};
我需要创建大约3000个动态物体。我从cocos2dx示例中获得了此代码。