Nathan Campos
Nathan Campos

Reputation: 29497

Move 2D Object to a Point in OpenGL

How to move a 2D object in the direction of a point (not a GL_POINTS, but coordinates) using OpenGL?

For a better understanding of my code:

I've splited most of my code into different source codes, but this is the one that is actually creating the shapes and setting the scene:

void setupScene(int clearColor[]) {
    glClearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
    //glClearColor(250, 250, 250, 1.0);  //  Set the cleared screen colour to black.
    glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);  // This sets up the viewport so that the coordinates (0, 0) are at the top left of the window.
    
    // Set up the orthographic projection so that coordinates (0, 0) are in the top left.
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, WINDOW_WIDTH, WINDOW_HEIGHT, 0, -10, 10);
    
    // Back to the modelview so we can draw stuff.
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the screen and depth buffer.
}

void drawScene() {
    setupScene((int[]){250, 250, 250, 1});
    
    triangle(210, WINDOW_WIDTH, WINDOW_HEIGHT);
    
    glBegin(GL_QUADS);
    glColor3f(RGB(80), RGB(80), RGB(80));

    glPushMatrix();
    glTranslatef(400, 400, 0);
    glVertex2d(200, 100);
    glVertex2d(100, 100);
    glVertex2d(100, 200);
    glVertex2d(200, 200);
    glPopMatrix();
    glEnd();
    
    glutSwapBuffers();  // Send the scene to the screen.
}

void update(int value) {
    glutPostRedisplay();  // Tell GLUT that the display has changed.
    glutTimerFunc(25, update, 0);  // Tell GLUT to call update again in 25 milliseconds.
}

Upvotes: 1

Views: 9127

Answers (2)

Sabito
Sabito

Reputation: 5065

Answer on the behalf of the OP:

Thanks @paddy, I was trying to understand the use of glTranslatef and came with the solution. Here is the working code, it will create a square at 100x100 and will move it until 400x200:

void setupScene(int clearColor[]) {
    glClearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
    //glClearColor(250, 250, 250, 1.0);  //  Set the cleared screen colour to black.
    glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);  // This sets up the viewport so that the coordinates (0, 0) are at the top left of the window.
    
    // Set up the orthographic projection so that coordinates (0, 0) are in the top left.
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, WINDOW_WIDTH, WINDOW_HEIGHT, 0, -10, 10);
    
    // Back to the modelview so we can draw stuff.
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the screen and depth buffer.
}

int a = 100;
int b = 200;
int x = 100;
int y = 100;

void drawScene() {
    setupScene((int[]){250, 250, 250, 1});
    
    triangle(210, WINDOW_WIDTH, WINDOW_HEIGHT);

    glPushMatrix();
    glTranslatef(x, y, 0);

    glBegin(GL_QUADS);
    glColor3f(RGB(80), RGB(80), RGB(80));

    glVertex2d(b, a);
    glVertex2d(a, a);
    glVertex2d(a, b);
    glVertex2d(b, b);

    glEnd();

    glPopMatrix();
    
    glutSwapBuffers();  // Send the scene to the screen.
}

void update(int value) {
    if (x != 400 && y != 200) {
        x += 4;
        y += 2;
    }
    glutPostRedisplay();  // Tell GLUT that the display has changed.
    glutTimerFunc(25, update, 0);  // Tell GLUT to call update again in 25 milliseconds.
}

Upvotes: 0

paddy
paddy

Reputation: 63451

You need to translate the modelview matrix. Assuming you're in modelview mode already:

glPushMatrix();
glTranslatef(x, y, z);
// Draw your shape
glPopMatrix();

[Edit]

@paddy: Something like this? I tried this but the square isn't moving. pastebin.com/2PCsy5kC

Try explicitly selecting the modelview matrix. Your example does not tell us which mode it's currently in:

glSetMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(x, y, z);
// Draw your shape
glPopMatrix();

Normally at the beginning of your render you reset everything... So you enter the GL_PROJECTION mode, call glLoadIdentity() to reset it and set up your camera, then do this for the GL_MODELVIEW matrix as well.

Upvotes: 3

Related Questions