Results 1 to 5 of 5

Thread: Movement and Coordinate System

  1. #1
    Junior Member Newbie
    Join Date
    Jun 2009
    Posts
    5

    Movement and Coordinate System

    Hi

    I am trying to create a program that simulates the movement of robots. However, I suspect that there may be some problems with the coordinate system that I have set up. When I drew the axes to check, they seem to follow the centre of the rectangle that I drew.

    What I would like is to fix the axes such that the rectangle moves based on a coordinate system.

    Any help is much appreciated.

    Thanks!



    Code :
    #include <math.h>
    #include <stdlib.h>
    #include <windows.h>
    #include <gl/Gl.h>
    #include <gl/Glu.h>
    #include <gl/glut.h>
     
    void RenderScene();                    // drawing function
    void Simulate();                       // simulation function
    void init();
     
    int istep = 0, nstep= 2000;
    float V = 1.0;
    float a = 10;
    float t = 0, dt = 0.001;
    float x[3] = {0,0,0}; // x[0]: x position; x[1]: y position; x[2]: bearing
     
     
    int main(int argc, char ** argv){
        glutInit(&amp;argc, argv);
        glutInitWindowSize(800,600);				 // width by height in pixels
        glutInitWindowPosition(10,50);
        glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);   // double buffers to remove flickering image
        glutCreateWindow("Basic Movement");
        init();
     
        glutDisplayFunc(Simulate);
    	glutMainLoop();
     
        return 0;
    }
     
    void init(){
        int w=500;
        glMatrixMode(GL_PROJECTION);  /* Start modifying the projection matrix. */
        glClearColor(0,0,0,0);         // Background color 
        gluOrtho2D(-w,w,-w,w);         // Define viewing area (left, right, bottom, up)
    }
     
    void RenderScene(){
     
    	float r = 10;					// size of robot in physical units
     
        glClear(GL_COLOR_BUFFER_BIT);
        glColor3f(1,0,0);
     
        // draw axes
        glBegin(GL_LINES);
            glVertex2f(0, 0);
            glVertex2f(0, 500);
        glEnd();
     
        glBegin(GL_LINES);
            glVertex2f(0, 0);
            glVertex2f(500, 0);
        glEnd();
     
        // draw bots
    	glPushMatrix();
     
            glBegin(GL_QUADS);
                glVertex2f( r, r);		
                glVertex2f( r,-r);
                glVertex2f(-r,-r);
                glVertex2f(-r, r);
            glEnd();
     
        glPopMatrix();
        glFlush();
     
    	glutSwapBuffers();		    // transfer to front buffer when drawing is complete
        glutPostRedisplay();
    }
     
    void Simulate(){
        // compute bot new position x, y
    	if( istep <= nstep) {  
     
            x[0] = x[0] + dt*V*cos(x[2]);
            x[1] = x[1] + dt*V*sin(x[2]);
            x[2] = x[2] + dt*a/V;
    		t = t + istep*dt;
    		istep += 1; 
     
    		glTranslatef(x[0],x[1],0);			// translate to new x,y coords
    	}
     
        RenderScene();				// draw to background buffer
    }

  2. #2
    Member Newbie
    Join Date
    May 2009
    Location
    Portugal
    Posts
    45

    Re: Movement and Coordinate System

    Code :
    // draw axes
    glLoadIdentity();
    glPushMatrix();
        glBegin(GL_LINES);
            glVertex2f(0, 0);
            glVertex2f(0, 500);
        glEnd();
     
        glBegin(GL_LINES);
            glVertex2f(0, 0);
            glVertex2f(500, 0);
        glEnd();
    glPopMatrix();

    I think this will be enough to solve your problem...
    "If you can't be a good example, you'll have to serve as a good warning..."

  3. #3
    Junior Member Newbie
    Join Date
    Jun 2009
    Posts
    5

    Re: Movement and Coordinate System

    Nope, its not working yet. Now it just shows a whole big red rectangle filling up the whole screen.

  4. #4
    Senior Member Frequent Contributor _NK47's Avatar
    Join Date
    Mar 2008
    Posts
    574

    Re: Movement and Coordinate System

    even though i dont get it the code would look prettier this way:

    // draw axes
    glPushMatrix();
    glLoadIdentity();
    glBegin(GL_LINES);
    glVertex2f(0, 0);
    glVertex2f(0, 500);
    glEnd();

    glBegin(GL_LINES);
    glVertex2f(0, 0);
    glVertex2f(500, 0);
    glEnd();
    glPopMatrix();

    otherwise you push the identity matrix.

  5. #5
    Member Newbie
    Join Date
    May 2009
    Location
    Portugal
    Posts
    45

    Re: Movement and Coordinate System

    Code :
            glBegin(GL_QUADS);
                glTranslatef(x[0],x[1],0);
                glVertex2f( r, r);		
                glVertex2f( r,-r);
                glVertex2f(-r,-r);
                glVertex2f(-r, r);
            glEnd();

    And remove glTranslatef(x[0],x[1],0); from Simulate()
    "If you can't be a good example, you'll have to serve as a good warning..."

Similar Threads

  1. camera movement in particle system
    By debinair in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 07-30-2014, 02:23 PM
  2. what about coordinate system?
    By beloni in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 03-16-2007, 12:21 PM
  3. 3D coordinate system
    By woodstock in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 03-11-2005, 04:16 AM
  4. Coordinate System
    By in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 04-19-2002, 06:19 PM
  5. GDI Coordinate system
    By DVazanias in forum OpenGL: Advanced Coding
    Replies: 3
    Last Post: 03-05-2002, 06:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean