Original Post
Hey people,
I have a maze and some agents moving through it using A star search. I'm using GLUT to do the rendering. After completing the path the agent returns to its original position and the cycle continues. The problem is in the render loop:
if the number of agents = 2, at first the 2nd agent gets rendered, then it kind of vanishes so that only 1 agent is shown. This agent completes the path and then when it restarts from its original position, agent 2 shows up and then both of them move. Even when I increase the number of agents to more than 2, initially all of them show up and then not more than 2 agents are shown at a time. Here's my code:
This is the main render loop:
static void render(void)
{
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// gluLookAt(-20.0f,5.0f,0.0f,-20.0+cos(270.0),5.0f,0.0+sin(270.0f),0.0,1.0,0.0);
glPushMatrix();
glLoadIdentity();
//glTranslatef(-27,-10,-85); //setting the appropriate camera look at (do it in a separate function)
//glRotatef(-90.0,1,0,0);
renderScene(-27,-10,-85);
renderAgent();
glPopMatrix();
// glDrawPixels(DIM,DIM,GL_RGBA,GL_UNSIGNED_BYTE,0);
glutSwapBuffers(); //swaps the front and back buffers
}
Render scene is:
void renderScene(float x, float y, float z)
{
glPushMatrix();
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0f,1.0f,1.0f);
glTranslatef(x,y,z);
glRotatef(-90.0, 1, 0, 0);
pMaze->displayMaze();
glPopMatrix();
}
My update function: I m using glTimer:
void updateScreen(int value)
{
moveAgent();
glutPostRedisplay();
glutTimerFunc(500,updateScreen,0);
}
and this is the move agent function:
void moveAgent()
{
for(int i = 0; i < numRows; i++)
{
//reset the current position of the agent and then move it
int a = pAgents.Position.x;
int b = pAgents.Position.y;
pMaze->m_Maze[a].setCellValue(UNOCCUPIED);
pAgents.positionIndex++;
if(pAgents.positionIndex >= pAgents.PathInMaze.size())
pAgents.positionIndex = 0;
pAgents.Position.x = pAgents.PathInMaze.at(pAgents.positionIndex).getCellX();
pAgents.Position.y = pAgents.PathInMaze.at(pAgents.positionIndex).getCellY();
a = pAgents.Position.x;
b = pAgents.Position.y;
pMaze->m_Maze[a].setCellValue(OCCUPIED);
}
}
Here is the renderAgent function:
void renderAgent()
{
for(int i = 0; i < numRows; i++)
{
float x1,z1;
x1 = -3.75 + ((float)pAgents.Position.x * 2.50f);
z1 = -9.0 + 1.25 + 2.50f + ((float)pAgents.Position.y * 2.50f);
glPushMatrix();
glColor3f(1.0f,0.0f,1.0f);
glTranslatef(-27,-10,-85);
glRotatef(-90.0, 1,0,0);
// glScalef(0.5,0.5,0.5);
glBegin(GL_QUADS);
glVertex3f((x1 + 0.25f) , 0, (z1 - 1.25f));
glVertex3f((x1 - 0.25f) , 0 , (z1 - 1.25f));
glVertex3f((x1 - 0.25f) , 0 , (z1 + 1.25f));
glVertex3f((x1 + 0.25f) , 0 , (z1 + 1.25f));
glEnd();
// Bottom
glBegin(GL_QUADS);
glVertex3f((x1+0.25) ,-0, (z1+1.25)); // Top Right Of The Quad (Bottom)
glVertex3f((x1-0.25) ,-0, (z1+1.25)); // Top Left Of The Quad (Bottom)
glVertex3f((x1-0.25) ,-0, (z1-1.25)); // Bottom Left Of The Quad (Bottom)
glVertex3f((x1+0.25) ,-0, (z1-1.25)); // Bottom Right Of The Quad (Bottom)
glEnd();
// Front
glBegin(GL_QUADS);
glVertex3f((x1+0.25), 0, (z1+1.25)); // Top Right Of The Quad (Front)
glVertex3f((x1-0.25), 0, (z1+1.25)); // Top Left Of The Quad (Front)
glVertex3f((x1-0.25),-0, (z1+1.25)); // Bottom Left Of The Quad (Front)
glVertex3f((x1+0.25),-0, (z1+1.25)); // Bottom Right Of The Quad (Front)
glEnd();
// Back
glBegin(GL_QUADS);
glVertex3f((x1-0.25),0,(z1-1.25)); // Top Right Of The Quad (Back)
glVertex3f((x1+0.25),0,(z1-1.25)); // Top Left Of The Quad (Back)
glVertex3f((x1+0.25),-0,(z1-1.25)); // Bottom Left Of The Quad (Back)
glVertex3f((x1-0.25),-0,(z1-1.25)); // Bottom Right Of The Quad (Back)
glEnd();
// left of cube
glBegin(GL_QUADS);
glVertex3f((x1-0.25), 0,(z1+1.25)); // Top Right Of The Quad (Left)
glVertex3f((x1-0.25), 0,(z1-1.25)); // Top Left Of The Quad (Left)
glVertex3f((x1-0.25),-0,(z1-1.25)); // Bottom Left Of The Quad (Left)
glVertex3f((x1-0.25),-0,(z1+1.25)); // Bottom Right Of The Quad (Left)
glEnd();
// Right of cube
glBegin(GL_QUADS);
glVertex3f((x1+0.25), 0,(z1-1.25)); // Top Right Of The Quad (Right)
glVertex3f((x1+0.25), 0,(z1+1.25)); // Top Left Of The Quad (Right)
glVertex3f((x1+0.25),-0,(z1+1.25)); // Bottom Left Of The Quad (Right)
glVertex3f((x1+0.25),-0,(z1-1.25)); // Bottom Right Of The Quad (Right)
glEnd();
glPopMatrix();
}
}
So I m not sure why they get rendered only 2 at a time. If anyone can point out what is going wrong, I would be appreciate it
I have a maze and some agents moving through it using A star search. I'm using GLUT to do the rendering. After completing the path the agent returns to its original position and the cycle continues. The problem is in the render loop:
if the number of agents = 2, at first the 2nd agent gets rendered, then it kind of vanishes so that only 1 agent is shown. This agent completes the path and then when it restarts from its original position, agent 2 shows up and then both of them move. Even when I increase the number of agents to more than 2, initially all of them show up and then not more than 2 agents are shown at a time. Here's my code:
This is the main render loop:
static void render(void)
{
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// gluLookAt(-20.0f,5.0f,0.0f,-20.0+cos(270.0),5.0f,0.0+sin(270.0f),0.0,1.0,0.0);
glPushMatrix();
glLoadIdentity();
//glTranslatef(-27,-10,-85); //setting the appropriate camera look at (do it in a separate function)
//glRotatef(-90.0,1,0,0);
renderScene(-27,-10,-85);
renderAgent();
glPopMatrix();
// glDrawPixels(DIM,DIM,GL_RGBA,GL_UNSIGNED_BYTE,0);
glutSwapBuffers(); //swaps the front and back buffers
}
Render scene is:
void renderScene(float x, float y, float z)
{
glPushMatrix();
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0f,1.0f,1.0f);
glTranslatef(x,y,z);
glRotatef(-90.0, 1, 0, 0);
pMaze->displayMaze();
glPopMatrix();
}
My update function: I m using glTimer:
void updateScreen(int value)
{
moveAgent();
glutPostRedisplay();
glutTimerFunc(500,updateScreen,0);
}
and this is the move agent function:
void moveAgent()
{
for(int i = 0; i < numRows; i++)
{
//reset the current position of the agent and then move it
int a = pAgents.Position.x;
int b = pAgents.Position.y;
pMaze->m_Maze[a].setCellValue(UNOCCUPIED);
pAgents.positionIndex++;
if(pAgents.positionIndex >= pAgents.PathInMaze.size())
pAgents.positionIndex = 0;
pAgents.Position.x = pAgents.PathInMaze.at(pAgents.positionIndex).getCellX();
pAgents.Position.y = pAgents.PathInMaze.at(pAgents.positionIndex).getCellY();
a = pAgents.Position.x;
b = pAgents.Position.y;
pMaze->m_Maze[a].setCellValue(OCCUPIED);
}
}
Here is the renderAgent function:
void renderAgent()
{
for(int i = 0; i < numRows; i++)
{
float x1,z1;
x1 = -3.75 + ((float)pAgents.Position.x * 2.50f);
z1 = -9.0 + 1.25 + 2.50f + ((float)pAgents.Position.y * 2.50f);
glPushMatrix();
glColor3f(1.0f,0.0f,1.0f);
glTranslatef(-27,-10,-85);
glRotatef(-90.0, 1,0,0);
// glScalef(0.5,0.5,0.5);
glBegin(GL_QUADS);
glVertex3f((x1 + 0.25f) , 0, (z1 - 1.25f));
glVertex3f((x1 - 0.25f) , 0 , (z1 - 1.25f));
glVertex3f((x1 - 0.25f) , 0 , (z1 + 1.25f));
glVertex3f((x1 + 0.25f) , 0 , (z1 + 1.25f));
glEnd();
// Bottom
glBegin(GL_QUADS);
glVertex3f((x1+0.25) ,-0, (z1+1.25)); // Top Right Of The Quad (Bottom)
glVertex3f((x1-0.25) ,-0, (z1+1.25)); // Top Left Of The Quad (Bottom)
glVertex3f((x1-0.25) ,-0, (z1-1.25)); // Bottom Left Of The Quad (Bottom)
glVertex3f((x1+0.25) ,-0, (z1-1.25)); // Bottom Right Of The Quad (Bottom)
glEnd();
// Front
glBegin(GL_QUADS);
glVertex3f((x1+0.25), 0, (z1+1.25)); // Top Right Of The Quad (Front)
glVertex3f((x1-0.25), 0, (z1+1.25)); // Top Left Of The Quad (Front)
glVertex3f((x1-0.25),-0, (z1+1.25)); // Bottom Left Of The Quad (Front)
glVertex3f((x1+0.25),-0, (z1+1.25)); // Bottom Right Of The Quad (Front)
glEnd();
// Back
glBegin(GL_QUADS);
glVertex3f((x1-0.25),0,(z1-1.25)); // Top Right Of The Quad (Back)
glVertex3f((x1+0.25),0,(z1-1.25)); // Top Left Of The Quad (Back)
glVertex3f((x1+0.25),-0,(z1-1.25)); // Bottom Left Of The Quad (Back)
glVertex3f((x1-0.25),-0,(z1-1.25)); // Bottom Right Of The Quad (Back)
glEnd();
// left of cube
glBegin(GL_QUADS);
glVertex3f((x1-0.25), 0,(z1+1.25)); // Top Right Of The Quad (Left)
glVertex3f((x1-0.25), 0,(z1-1.25)); // Top Left Of The Quad (Left)
glVertex3f((x1-0.25),-0,(z1-1.25)); // Bottom Left Of The Quad (Left)
glVertex3f((x1-0.25),-0,(z1+1.25)); // Bottom Right Of The Quad (Left)
glEnd();
// Right of cube
glBegin(GL_QUADS);
glVertex3f((x1+0.25), 0,(z1-1.25)); // Top Right Of The Quad (Right)
glVertex3f((x1+0.25), 0,(z1+1.25)); // Top Left Of The Quad (Right)
glVertex3f((x1+0.25),-0,(z1+1.25)); // Bottom Left Of The Quad (Right)
glVertex3f((x1+0.25),-0,(z1-1.25)); // Bottom Right Of The Quad (Right)
glEnd();
glPopMatrix();
}
}
So I m not sure why they get rendered only 2 at a time. If anyone can point out what is going wrong, I would be appreciate it