I am stuck at Drawing Polygons as Box2D objects. With opengl, the drawing of a polygon is pretty simple i.e
glBegin(GL_POLYGON);
glColor3f(0,0,0);
glVertex2f(2.0,6.0);
glVertex2f(2.0,8.0);
glVertex2f(2.5,8.0);
glVertex2f(2.5,6.0);
glEnd();
And what I have seen in the tutorials, with box2D we do it like this:
void drawSquare(b2Vec2* points,b2Vec2 center,float angle)
{
glColor3f(1,1,1);
glPushMatrix();
glTranslatef(center.x*M2P,center.y*M2P,0);
glRotatef(angle*180.0/3.141,0,0,1);
glBegin(GL_QUADS);
for(int i=0;i<4;i++)
{
glVertex2f(points[i].x*M2P,points[i].y*M2P);
}
glEnd();
glPopMatrix();
}
And the display() code is as follows:
void display()
{
glClear(GL_COLOR_BUFFER_BIT)
glEnable (GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,11);
//pix[0].mDraw();
glLoadIdentity();
b2Body* tmp=world->GetBodyList(); // Get no.of bodies
b2Vec2 points[4];
while(tmp)
{
for(int i=0;i<4;i++)
points[i]=((b2PolygonShape*)tmp->GetFixtureList()->GetShape())->GetVertex(i);
drawSquare(points,tmp->GetWorldCenter(),tmp->GetAngle());
tmp=tmp->GetNext();
}
glFlush();
}
I am stuck with this following code:
/*for(int i=0;i<4;i++)
points[i]=((b2PolygonShape*)tmp->GetFixtureList()->GetShape())->GetVertex(i);
drawSquare(points,tmp->GetWorldCenter(),tmp->GetAngle());
tmp=tmp->GetNext();*/
I dont really get what gets assigned into the points' array??
points
is accumulating the vertices of the collision polygon, which are assumed to be squares. Those vertices are then passed to the drawing function to be handled to OpenGL for rendering. - Trillian