Allegro.cc Forums » Programming Questions » Rotating polygon coordinates (linear algebra)

Credits go to Sirocco and X-G for helping out!
 Rotating polygon coordinates (linear algebra)
Johan Peitz
Member #9
April 2000

Hello Allegators!

I'm in the midst of porting Olivier Renaults 2D polygon collision thingy to use Allegro instead of OpenGL so that I can use it in my game engine.

I've got mostly everything working but since my linear algebra is shoddy at best I've naturally run into some problems and am currently stuck at some rotation issues. Actually, the logic part runs as it should, it is the drawing that I'm stumped at.

Since the original code uses OpenGL it simply applies some rotation matrix before drawing each object making it quick, elegant and easy. This is something that I'm pretty sure isn't as easily done in Allegro so I rush to the source of all knowledge.

Here's the original polygon drawing code (some non relevant stuff removed):

 1 void PolyColl::Render(const Vector& xOffset, float angle, u_int ARGBfill, u_int ARGBline, const Vector* axVertices, int iNumVertices) 2 { 3 if (!axVertices) return; 4 glMatrixMode(GL_TEXTURE); 5 glLoadIdentity(); 6 float scale = 0.06f; 7 glScalef(scale, scale, scale); 8 9 glEnable(GL_TEXTURE_2D); 10 glBindTexture(GL_TEXTURE_2D, BindTexture()); 11 12 glMatrixMode(GL_MODELVIEW); 13 glPushMatrix(); 14 15 glTranslatef(xOffset.x, xOffset.y, 0.0f); 16 17 glRotatef(RadiansToDegrees(angle), 0, 0, -1); 18 19 glColor4ub(ARGB_R(ARGBfill), ARGB_G(ARGBfill), ARGB_B(ARGBfill), ARGB_A(ARGBfill)); 20 21 glBegin(GL_TRIANGLE_FAN); 22 23 for(int i = 0; i < iNumVertices; i ++) 24 { 25 glTexCoord2f(axVertices.x, axVertices.y); 26 glVertex2f(axVertices.x, axVertices.y); 27 } 28 glTexCoord2f(axVertices[0].x, axVertices[0].y); 29 glVertex2f(axVertices[0].x, axVertices[0].y); 30 glEnd(); 31 glDisable(GL_TEXTURE_2D); 32 33 if (ARGBline != 0) 34 { 35 glColor4ub(ARGB_R(ARGBline), ARGB_G(ARGBline), ARGB_B(ARGBline), ARGB_A(ARGBline)); 36 37 glBegin(GL_LINE_LOOP); 38 39 for(int i = 0; i < iNumVertices; i ++) 40 { 41 glVertex2f(axVertices.x, axVertices.y); 42 } 43 glVertex2f(axVertices[0].x, axVertices[0].y); 44 glEnd(); 45 } 46 glPopMatrix(); 47 }

I can easily draw the polygon at the right place using the following code:

```  for(int i = 0; i < iNumVertices - 1; i ++) {
line(p_buffer, (int)(xOffset.x + axVertices<i>.x), (int)(xOffset.y + axVertices<i>.y),
(int)(xOffset.x + axVertices[i+1].x), (int)(xOffset.y + axVertices[i+1].y), ARGBline);
}
line(p_buffer, (int)(xOffset.x + axVertices[0].x), (int)(xOffset.y + axVertices[0].y),
(int)(xOffset.x + axVertices[iNumVertices - 1].x), (int)(xOffset.y + axVertices[iNumVertices - 1].y),ARGBline);
```

but then it will not be rotated.

So what I have is a set of coordinates and an offset and I would like to know how to rotate them. I guess I could calculate the distance from origo to all coordninates and then rotate that with some angle and calculate the x and y angain for wacy node, but that seems tedious. Is there some quick and dirty matrix or vector operation I can use?