Detect position of triangle object on plane
My code is below; I want to get the coordinate position of a triangle which is moving on a plane after pressing a key. If my code is wrong please correct it. I'm a total beginner in OpenGL.
float rot=90;
float forwd=0;
float uprwd=0;
switch (key) {
case 'q':
exit(0);
break;
case 'a':
{
forwd-=0.2;
}
break;
case 'd':
{
forwd+=0.2;
}
break;
case 'w':
{
upr开发者_StackOverflow中文版wd+=0.2;
}
break;
case 's':
{
uprwd-=0.2;
}
break;
case 'r':
{
rot+=20;
}
break;
case 't':
{
rot-=20;
}
break;
}
void display (void) {
{
glPushMatrix();
glLoadIdentity();
glTranslatef(forwd,uprwd,-6);
glRotatef(rot, 0.0, 0.0, 1.0);
glBegin(GL_TRIANGLES);
glVertex3f( 0.2, 0.0, 0.0 );
glVertex3f( 0.0, 0.2, 0.0 );
glVertex3f( 0.0, 0.0, 0.2 );
glEnd();
glPopMatrix();
glutSwapBuffers();
}
How can I find the coordinate position of the triangle on the plane? It is rotating and moving on plane as well. Thanks.
When you rotate and/or translate the triangle you are changing its coordinates. glTransltef and glRotatef are applying transformation matrices to the basic position of your triangle, (0.2, 0.0, 0.0) for example. If you intend to track the new position of your triangle, you also must apply these transformations (Rotation Matrix, Translation) to a vector that is tracking the coordinate of your triangle.
精彩评论