开发者

Using glRotate and glTranslate with collision detection

开发者 https://www.devze.com 2022-12-25 02:02 出处:网络
Say I use glRotate to translate the current view based on some arbitrary user input (i.e, if key left is pressed then rt开发者_开发知识库ri+=2.5f)

Say I use glRotate to translate the current view based on some arbitrary user input (i.e, if key left is pressed then rt开发者_开发知识库ri+=2.5f)

glRotatef(rtri,0.0f,1.0f,0.0f);

Then I draw the triangle in the rotated position:

glBegin(GL_TRIANGLES);  // Drawing Using Triangles
    glVertex3f( 0.0f, 1.0f, 0.0f);  // Top
    glVertex3f(-1.0f,-1.0f, 0.0f);  // Bottom Left
    glVertex3f( 1.0f,-1.0f, 0.0f);  // Bottom Right
glEnd();  // Finished Drawing The Triangle

How do I get the resulting translated vertexes for use in collision detection? Or will I have to manually apply the transform myself and thus doubling up the work?

The reason I ask is that I wouldn't mind implementing display lists.


The objects you use for collision detection are usually not the objects you use for display. They are usually simpler and faster.
So yes, the way to do it is to maintain the transformation you're using manually but you wouldn't be doubling up the work because the objects are different.


Your game loop should look like (with c++ syntax) :

void Scene::Draw()
{
    this->setClearColor(0.0f, 0.0f, 0.0f);

    for(std::vector<GameObject*>::iterator it = this->begin(); it != this->end(); ++it)
    {
        this->updateColliders(it);
        glPushMatrix();
            glRotatef(it->rotation.angle, it->rotation.x, it->rotation.y, it->rotation.z);
            glTranslatef(it->position.x, it->position.y, it->position.z);
            glScalef(it->scale.x, it->scale.y, it->scale.z);

            it->Draw();

        glPopMatrix();
    }

    this->runNextFrame(this->Draw, Scene::MAX_FPS);
}

So, for instance, if i use a basic box collider with a cube the draw method will :

  • Fill the screen with a black color (rgb : (0,0,0))
  • For each object
    • Compute the collisions with position and size informations
    • Save the actual ModelView matrix state
    • Transform the ModelView matrix (rotate, translate, scale)
    • Draw the cube
    • Restore the ModelView matrix state
    • Check the FPS and run the next frame at the right time

** The class Scene inherits from the vector class

I hope it will help ! :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号