Question

I'm drawing a stick figure in OpenGL and I have the body (which I'm using GL_LINES for) and the head (which I'm using GL_LINE_LOOP for). After drawing these out, they all show up perfectly fine. However, when I go to move the stick figure all over the screen, the head disconnects from the body. I need these to stay as one for game purposes. Can anyone help me out and explain it as well?

If you need me to explain further as well, just let me know.

//Drawing body
glBegin(GL_LINES);
    glColor3f(1,1,1);
    glVertex2f(xpos, ypos);
    glVertex2f(xpos - xdim/2, ypos - ydim);             
    glVertex2f(xpos, ypos);
    glVertex2f(xpos + xdim/2, ypos - ydim);             
    glVertex2f(xpos, ypos);
    glVertex2f(xpos, ypos + ydim);                  
    glVertex2f(xpos - xdim/2, ypos + ydim/2);
    glVertex2f(xpos, ypos + ydim);                  
    glVertex2f(xpos - xdim/2, ypos + ydim/2);       
    glVertex2f(xpos - xdim/4, ypos + ydim/2);           
    glVertex2f(xpos - xdim/4, ypos + ydim/2);   
    glVertex2f(xpos - xdim/4, ypos + ydim/3);   
    glVertex2f(xpos - xdim/4, ypos + ydim/2);
    glVertex2f(xpos + xdim, ypos + ydim/2);         
    glVertex2f(xpos + xdim, ypos + ydim/2);
    glVertex2f(xpos + xdim, ypos + ydim/3);         
    glVertex2f(xpos + xdim, ypos + ydim/2);
    glVertex2f(xpos + xdim + xdim/2, ypos + ydim/2);  
    glVertex2f(xpos + xdim/2, ypos + ydim/2);
    glVertex2f(xpos, ypos + ydim);                      
    glVertex2f(xpos, ypos + ydim + ydim/4);
    glVertex2f(xpos, ypos + ydim);                      
glEnd();

//Drawing head
glBegin(GL_LINE_LOOP); 
    glColor3f(1,1,1);
    for (i = 0; i < 100; i++){
        j = 2*3.14*i/100;
        glVertex2f(200 + 15 * cos(j), 254 + 15 * sin(j));
    }
glEnd();
Was it helpful?

Solution

That's quite obvious: You need to apply same transforms to all of your figure parts in order keep them together.

Depending on the figure hierarchy:

  • if it is individual parts: add the same offset to each part. Watch to avoid matrix stack changes in between (in FFP glPush/glPop);
  • if you use a list/tree-like parts hierarchy - make sure parts are parented properly and you apply transform only once to the root.

In your code sample: You seem to use xpos / ypos to move the body, but you don't add them to the head - hence it's not moving along.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top