OpenGL object does not convert to rotation axis

I ran into a problem in 2 turns. A triangle is drawing using the opengldrawing primitive GL_LINES.

Image traffic

Scenario: I want to rotate this triangle using the Eand keys and Rtranslate this triangle along its axis of rotation by pressing the arrow keys up down left right.

Image 2

But now the triangle rotates, but does not translate it along the axis of rotation. For example, in simple words, I rotate the object at an angle of 90 degrees, when the user presses the arrow key, he does not translate this triangle at an angle of 90 degrees, he simply translates this object just up and down. I am stuck on this.
This is my code:

#include<iostream>
#include <cstdlib>
#include<GL\freeglut.h> 

using namespace std;    
float posX = 0.0f, posY = 0.0f, move_unit = 0.01f, angle = 0.0f;    
void init(void) {

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);    
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-1.0, 1.0, -1.0, 1.0, 1.0, -1.0);    
}

void drawFigure() {
    //LINES Make traingle
    glColor3f(1.0f, 1.0f, 0.0f);    
    glBegin(GL_LINES);    
    glVertex2f(0.1f, 0.0f);
    glVertex2f(-0.1f, 0.0f);
    glVertex2f(0.0f, 0.1f);
    glVertex2f(-0.1f, 0.0f);
    glVertex2f(0.0f, 0.1f);
    glVertex2f(0.1f, 0.0f);
    glColor3f(0.0f, 1.0f, 0.0f);
    glVertex2f(0.0f, 0.1f);
    glVertex2f(0.0f, 0.0f);    
    glEnd();    
}

void SpecialKeys(int key, int xpos, int ypos) {

    if (key == GLUT_KEY_UP) {    
        if (posY < 0.95f) {
            posY += move_unit;
        }
    }
    else if (key == GLUT_KEY_DOWN) {
        if (posY > -1.0f) {
            posY -= move_unit;
        }
    }
    else if (key == GLUT_KEY_RIGHT) {
        if (posX < 0.95f) {
            posX += move_unit;
        }

    }
    else if (key == GLUT_KEY_LEFT) {
        if (posX > -0.95f) {
            posX = posX - move_unit;
        }
    }
    glutPostRedisplay();
}


void KeysFun(unsigned char key, int xpos, int ypos) {
    if (key == 'e' || key == 'E') {
        angle++;
    }
    else if (key == 'r' || key == 'R') {
        angle--;
    }

    glutPostRedisplay();    
}


void display() {    
    glClear(GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glPushMatrix();
    glTranslatef(posX, posY, 0.0f); 
    glRotatef(angle, 0.0f, 0.0f, 1.0f);   
    drawFigure();    
    glPopMatrix();    

    glFlush();    
}

int main(int argc, char**argv) {
    glutInit(&argc, argv);
    glutInitWindowSize(600, 600);
    glutInitWindowPosition(450, 50);
    glutCreateWindow("C");
    init();
    glutDisplayFunc(display);
    glutSpecialFunc(&SpecialKeys);
    glutKeyboardFunc(&KeysFun);
    glutMainLoop();

    return EXIT_SUCCESS;
}
+2
1

, , , .

, , - , TRS ( ). , , .

, , , , , ( ..), , . .

rotate( angle, 0.0f, 0.0f, 1.0f );
translate( posX, posY, 0.0f );

. , , .

translate( centerX, centerY, 0.0f );
rotate( angle, 0.0f, 0.0f, 1.0f );
translate( posX, posY, 0.0f );

: , (posX, posY), (0,0,1) (centerX, centerY).

Edit2: , , centerX centerY (0,0), . (0,0).

Triangle motion example

Edit3:

. , , .. .

// Let assume we have a vector2 class that stores two floats
vector2 forward;
forward.x = sin( -_angle * DEG_TO_RADIANS );
forward.y = cos( -_angle * DEG_TO_RADIANS );

vector2 right;
right.x = forward.y;
right.y = -forward.x;

// Move one tenth of the length of the forward vector
float velocity = 0.1f;

if ( key == GLUT_KEY_UP ) {
    pos += forward * velocity;
}
else if ( key == GLUT_KEY_DOWN ) {
    pos -= forward * velocity;
}
else if ( key == GLUT_KEY_RIGHT ) {
    pos += right * velocity;
}
else if ( key == GLUT_KEY_UP ) {
    pos -= right * velocity;
}

, . , . , , 0 direction (0,1) 90 , .

Triangle moving as a player character

-1

All Articles