Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Well I am trying to learn openGL and want to make a triangle move one unit (0.1) everytime I press one of the keyboard arrows. However i want the triangle to turn first pointing the direction where i will move one unit. So if my triangle is pointing up and I press right the it should point right first and then move one unit in the x axis. I have implemented the code to move the object one unit in any direction, however I can not get it to turn pointing to the direction it is going. The initial position of the Triangle is pointing up.

#define LENGTH 0.05

float posX = -0.5, posY = -0.5, posZ = 0;
float inX = 0.0 ,inY = 0.0 ,inZ = 0.0; // what values????

void rect(){
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glPushMatrix();
    glTranslatef(posX,posY,posZ);
    glRotatef(rotate, inX, inY, inZ);
    glBegin(GL_TRIANGLES);
    glColor3f(0.0, 0.0, 1.0);
    glVertex2f(-LENGTH,-LENGTH);
    glVertex2f(LENGTH-LENGTH, LENGTH);
    glVertex2f(LENGTH, -LENGTH);
    glEnd();
    glPopMatrix();

}
share|improve this question
    
Try put inZ = 1. the last three values of the glRotatef are the values of the vector to which you want to rotate from. You are looking from up, you want to rotate according to z direction. See Notes of cs.rutgers.edu/~decarlo/428/gl_man/rotate.html –  J. C. Leitão Nov 2 '12 at 15:02
    
Thank you for your response. I have tried this already, I tried putting one in all (x,y,z) however I cannot get the right image. When i put 1.0 in z and pres right i goes looking 45 degrees and becomes a different triangle. –  Vico Pelaez Nov 2 '12 at 15:09
    
try rotating first and then translating. –  Ray Tayek Nov 7 '12 at 5:52
add comment

2 Answers 2

Every rotation in gl is according to a given vector. Dependending from where you are looking at the triangle, you will have to rotate according to that vector. For instance, if you are looking from z>0, you have to rotate your triangle with

inZ = 1

such that the triangle rotates according to the vector (0,0,1).

UPDATE:

Generally speaking, this code needs some attention. In openGL, you must understand the concept of MODELVIEW, VIEWPORT, and PROJECTION, all the three are matrixes. You are using a PROJECTION matrix to draw your object, which conceptually is not correct (even if you can). In general lines, you have to define your viewport and projection, and only then you can draw a model (e.g. a triangle, or a cat). See this answer, which has some insight about these concepts.

share|improve this answer
    
Thank you for your response! I tried with every Vector and nothing goes ok. Is there a way to know where I am looking from, or maybe set it up in my code? Thanks again! –  Vico Pelaez Nov 2 '12 at 15:11
1  
Thank you, yeh I am sarting this so I do not have everything really clear. See I think I have achieved something. When I set the x to 1 this works with the up and down button however not with the left or right button. I am finding this really hard to understand. –  Vico Pelaez Nov 2 '12 at 16:09
    
@Vico Pelaez In this link: sites.google.com/site/jorgecarleitao/code-reservoir/opengl I present a minimal example for using openGL with Qt. Even if you are not using QT, the scene.cpp's methods updateViewPort, updateModelView, updateProjection and drawBackground should be a good starting point to implement in glut. –  J. C. Leitão Nov 2 '12 at 16:16
add comment

glRotatef() creates a rotation around the axis specified by the vector. Try glRotatef(angle, 0, 0, 1); to rotate around the Z-axis.

For example:

// Set up orthogonal 2D Projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, screenWidth, 0, screenHeight, -1, 1);

// Set up View matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(-cameraX, -cameraY, -cameraZ);

// Set up Model * View matrix
glPushMatrix();
glTranslatef(modelX, modelY, modelZ);
glRotatef(modelAngle, 0, 0, 1);
// draw model
glPopMatrix();

Pay attention to the order of matrix operations.

share|improve this answer
    
Thank you for your response! I tried with every Vector and nothing goes ok. Is there a way to know where I am looking from, or maybe set it up in my code? Thanks again! –  Vico Pelaez Nov 2 '12 at 15:50
    
The default camera in OpenGL is set up to show everything inside a cube [-1 .. 1] in all dimensions, looking down the positive Z axis. Anything outside this cube will get clipped away. Set up the projection (size of the world to be shown on the screen) with the GL_PROJECTION matrix and glOrtho(). Then move the camera to where you want it using the GL_MODELVIEW matrix and glRotate and glTranslate (or gluLookAt). Then rotate your objects, again using the GL_MODELVIEW matrix. –  ccxvii Nov 6 '12 at 19:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.