-1

I'm trying to create a 3D quad and moving around. I could not figure out the direction of my quad facing.

Here is the code I had so far:

void draw(){
    glPushMatrix();
    //update the central point of the quad
    newX = 0.5 + xr; 
    newZ = -1.0 + zr;

    glTranslatef(newX, 0.0, newZ);
    glRotatef(robotAngle, 0.0, 1.0, 0.0);
    glTranslatef(-newX, 0.0, -newZ);
    drawQuad();
    glPopMatrix();
}

void drawQuad(){
    glPushMatrix();
    // Front
    glBegin(GL_QUADS);
    glColor3f(1.0f, 0.0f, 0.0f);    
    glVertex3f(0.0f + xr, 0.0f, 0.0f + zr);
    glVertex3f(1.0f + xr, 0.0f, 0.0f + zr);
    glVertex3f(1.0f + xr, 1.0f, 0.0f + zr);
    glVertex3f(0.0f + xr, 1.0f, 0.0f + zr);
    glEnd();
    // Back
    glBegin(GL_QUADS);
    glColor3f(1.0f, 0.0f, 0.0f);     
    glVertex3f(0.0f + xr, 0.0f, -2.0f + zr);
    glVertex3f(1.0f + xr, 0.0f, -2.0f + zr);
    glVertex3f(1.0f + xr, 1.0f, -2.0f + zr);
    glVertex3f(0.0f + xr, 1.0f, -2.0f + zr);
    glEnd();

    glColor3f(1, 0, 1);
    glBegin(GL_QUAD_STRIP);
    glVertex3f(0.0f + xr, 0.0f, 0.0f + zr);
    glVertex3f(0.0f + xr, 0.0f, -2.0f + zr);

    glVertex3f(1.0f + xr, 0.0f, 0.0f + zr);
    glVertex3f(1.0f + xr, 0.0f, -2.0f + zr);

    glVertex3f(1.0f + xr, 1.0f, 0.0f + zr);
    glVertex3f(1.0f + xr, 1.0f, -2.0f + zr);

    glVertex3f(0.0f + xr, 1.0f, 0.0f + zr);
    glVertex3f(0.0f + xr, 1.0f, -2.0f + zr);
    glEnd();
}

void specialkey(unsigned char key, int x, int y)
{
    switch (key) {
    case 's':
        robotAngle += 1.0;
        glutPostRedisplay();
        break;
    case 'S':
        robotAngle -= 1.0;
        glutPostRedisplay();
        break;
        }
}

void directionkey(int key, int x, int y)
{
    switch (key) {
    case GLUT_KEY_LEFT:
        xr -= 0.05;
        glutPostRedisplay();
        break;
    case GLUT_KEY_RIGHT:
        xr += 0.05;
        glutPostRedisplay();
        break;
   }
}

The quad can rotate around its center point at the original position. However, once I start to move the quad, it will only move along with the x-axis, not the direction my quad facing.

How could I solve this problem?

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • 1
    If you want the z position to change, then you need to change zr. How to change xr and zr in a way that makes the robot go in the direction it's facing? For that, it's time for some maths! – user253751 Oct 16 '20 at 15:53
  • 2
    @tadman that's a simple way to rotate around a point other than the origin – user253751 Oct 16 '20 at 15:55
  • @user253751 yes I'm trying to figure it out the direction vector now, I have the center point of my quad both x and z point. – Kanzaki Aria Echo Oct 16 '20 at 16:09
  • @user253751 Fair enough. I guess I'm used to having parenting taken care of. – tadman Oct 16 '20 at 16:17
  • @KanzakiAriaEcho `pivotX` and `pivotZ` depend on `xr` and `zr`. When you change `xr` and `zr`, then you've to change `pivotX` and `pivotZ`, too. – Rabbid76 Oct 16 '20 at 16:18
  • @Rabbid76 sorry that's was a typo, I put both `newX` and `newZ` in the function already. and it will only move along with the x-axis. – Kanzaki Aria Echo Oct 16 '20 at 16:20
  • see [Understanding 4x4 homogenous transform matrices](https://stackoverflow.com/a/28084380/2521214) at the end you got links to few answers with C++/GL examples ... – Spektre Oct 17 '20 at 08:48

1 Answers1

0

Do not modify the vertex coordinates by xr and zr:

glVertex3f(0.0f + xr, 0.0f, 0.0f + zr);

glVertex3f(0.0f, 0.0f, 0.0f);

Use glTranslatef to move the model after the model has been rotated. In the following pivotX and pivotZ are constant a do not change:

void draw(){
    
    float pivotX = 0.5f; // center of the model
    float pivotZ = -1.0f;

    glPushMatrix();
    
    glTranslatef(xr, 0.0, zr);

    glTranslatef(pivotX, 0.0, pivotZ);
    glRotatef(robotAngle, 0.0, 1.0, 0.0);
    glTranslatef(-pivotX, 0.0, -pivotZ);
    
    drawQuad();
    
    glPopMatrix();
}

The matrix multiplication is not Commutative the order matters.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174