Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8330319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:05:20+00:00 2026-06-09T02:05:20+00:00

How to move a 2D object in the direction of a point (not a

  • 0

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.
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T02:05:21+00:00Added an answer on June 9, 2026 at 2:05 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Android opengl I need to move an object from point A to point
I wrote a piece of code to move an object in a Random Direction
I am using the IPhone's Accelerometer to make a object move. I want to
I need to move object (UIImageView) from point A to point B, then rotate
I am trying to move an object by checking if the mouse is colliding
I'm trying to move an object on the click of a mouse while the
Is there any way to move an object forward in Three.js? Maybe I should
I have three lists, I need to move Animal object from list animalSource to
I have a object A move with Velocity (v1, v2, v3) in 3D space.
I have found this question; Can I move a Flash object within the DOM

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.