我创建了一个游戏,它使用颜色编码的图像来创建不同的身体/固定装置。因此,例如,如果像素是绿色的,它将被存储为 7 的数组,然后程序将创建一个名为敌人的物体。如果有 10 个绿色像素,则会创建 10 个敌人:
else if (array[w][h]==7)
{
b2BodyDef Enemy_BodyDef;
Enemy_BodyDef.type = b2_kinematicBody;
Enemy_BodyDef.position.Set(x,y);
m_enemyBody = m_world->CreateBody(&Enemy_BodyDef);
m_enemyBody->SetAngularVelocity(0.0);
m_enemyBody->SetAngularDamping(0.3f);
m_enemyBody->SetLinearDamping(5.0f);
m_enemyBody->SetFixedRotation(true);
b2PolygonShape Enemy_dynamicBox;
Enemy_dynamicBox.SetAsBox(2.5f, 2.5f);
b2FixtureDef Enemy_fixtureDef;
Enemy_fixtureDef.shape = &Enemy_dynamicBox;
Enemy_fixtureDef.density = 1.0f;
Enemy_fixtureDef.friction = 0.1f;
Enemy_fixtureDef.restitution=.0f;
m_enemyFixture=m_enemyBody->CreateFixture(&Enemy_fixtureDef);
enemyBody a;
m_enemybodies.push_back(a);
}
在渲染中:
ngl::ShaderLib *shader9=ngl::ShaderLib::instance();
(*shader9)["nglDiffuseShader"]->use();
for(unsigned int i=0; i<m_enemybodies.size(); ++i)
{
m_enemybodies[i].m_enemy_position.m_x = m_enemyBody->GetPosition().x;
m_enemybodies[i].m_enemy_position.m_y = m_enemyBody->GetPosition().y;
m_enemybodies[i].m_enemy_dimention.set(5.0f,5.0f);
m_transform.reset();
{
shader9->setShaderParam4f("Colour",1.0f,0.0f,0.0f,1.0f);
m_transform.setScale(m_enemybodies[i].m_enemy_dimention.m_x,m_enemybodies[i].m_enemy_dimention.m_y,0.1);
m_transform.setPosition(m_enemybodies[i].m_enemy_position.m_x,m_enemybodies[i].m_enemy_position.m_y,0.0);
loadMatricesToShader();
prim->draw("cube");
}
}
b2Fixtures 被放置在正确的位置,但是只有最后一个被渲染。当我尝试设置它们的线速度时也是如此,只有数组中的最后一个被移动和渲染。
使用它们在向量中的事实,我如何迭代并让它们渲染和移动?
编辑:
enemyBody a;
m_enemybodies.push_back(a);
这指的是我为放置和渲染主体创建的结构:
typedef struct
{
ngl::Vec2 m_enemy_position;
ngl::Vec2 m_enemy_dimention;
}enemyBody;