Allegro.cc - Online Community

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

Credits go to Sirocco and X-G for helping out!
This thread is locked; no one can reply to it. rss feed Print
Rotating polygon coordinates (linear algebra)
Johan Peitz
Member #9
April 2000
avatar

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):

1void 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<i>.x, axVertices<i>.y);
26 glVertex2f(axVertices<i>.x, axVertices<i>.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<i>.x, axVertices<i>.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?

Thank you in advance,
Johan, the linear noob

--
johan peitz :: things I made

Sirocco
Member #88
April 2000
avatar

IIRC you can use a standard 3D rotation matrix and ignore the Y or Z component depending on your setup.

There's a handy 2D matrix as well.

-->
Graphic file formats used to fascinate me, but now I find them rather satanic.

Johan Peitz
Member #9
April 2000
avatar

That's what X-G said too and I kinda got too it myself while I was writing the post. :-[ I'll be back with the results...

--
johan peitz :: things I made

X-G
Member #856
December 2000
avatar

COOKIE!

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Johan Peitz
Member #9
April 2000
avatar

Very nice. 2D physics for the win. (It works now.)

--
johan peitz :: things I made

Go to: