0

我创建了一个游戏,它使用颜色编码的图像来创建不同的身体/固定装置。因此,例如,如果像素是绿色的,它将被存储为 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;
4

1 回答 1

1

我看到有两件事可能会导致问题。首先,也是最重要的,明显使用指向本地/临时变量的指针:

b2BodyDef Enemy_BodyDef;
...
m_enemyBody = m_world->CreateBody(&Enemy_BodyDef);

b2PolygonShape Enemy_dynamicBox;
...
Enemy_fixtureDef.shape = &Enemy_dynamicBox;

b2FixtureDef Enemy_fixtureDef;
...
m_enemyFixture=m_enemyBody->CreateFixture(&Enemy_fixtureDef);

离开此函数/方法后,这些局部变量不再存在,指向它们的任何指针都无效。此时尝试访问指向这些局部变量的指针将导致未定义的行为。如果您确实需要创建多个相互引用的对象,则需要使用动态内存:

b2PolygonShape* pEnemy_dynamicBox = new b2PolygonShape;
Enemy_fixtureDef.shape = Enemy_dynamicBox;

尽管您需要记住删除分配的内存。更惯用的 C++ 解决方案将使用std::shared_ptr<>.

另一个问题可能是最后两行:

enemyBody a;
m_enemybodies.push_back(a);

这似乎只是将一个新的、未初始化/设置的敌人身体推到向量上。m_enemyBody您刚刚在前面几行中设置的内容发生了什么?你的意思是做类似的事情:

m_enemybodies.push_back(*m_enemyBody);

反而?

更新

更仔细地查看您的渲染循环,很明显为什么您只显示一个身体。这段代码:

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);

只需将向量中的每个物体设置为相同的精确坐标和尺寸。这与您将空推enemyBody入向量的困惑一起,是您出现问题的原因。

要修复它,请在创建函数中更改一行(不包括使用临时变量指针的修复):

 m_enemybodies.push_back(*m_enemyBody);

在您的渲染循环中只需执行以下操作:

for(unsigned int i=0; i<m_enemybodies.size(); ++i)
{
    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");
}

请注意,如果您使用的是与 C++11 兼容的编译器,则此循环可以更简单地表示为:

for (auto & body: m_enemybodies) {
    ...
    m_transform.setScale(body.m_enemy_dimention.m_x, body.m_enemy_dimention.m_y, 0.1);
    m_transform.setPosition(body.m_enemy_position.m_x, body.m_enemy_position.m_y, 0.0);
    ...
}
于 2015-04-28T12:04:35.923 回答