PDA

View Full Version : Movement and Coordinate System



cwzcwz
06-25-2009, 09:20 AM
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!





#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
}

Makinis
06-25-2009, 09:50 AM
// 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...

cwzcwz
06-25-2009, 10:00 AM
Nope, its not working yet. Now it just shows a whole big red rectangle filling up the whole screen.

_NK47
06-25-2009, 10:06 AM
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.

Makinis
06-25-2009, 10:27 AM
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()