Here's some old fat line drawing function of mine. You could use it to calculate less shapes. I think the beveled left and right turns are actually convex polys too, so you should be able to get your calculations down to 1 shape per segment. I think I found that edge beveling algo originally from libart. The reason the loop goes from 0 to n+1 is that in order to create polygons you need the previous left and right point too. Also, precalculating the edge directions may be faster as each segment is normalize twice now.
On the other hand, this is really a case where you should use capsules. They should not be too hard to implement and should be way faster for collision detection than polygons.
Code:
static float Normalize(float& x, float& y)
{
float d = sqrtf(x*x + y*y);
if (d > 0.000001f)
{
x /= d;
y /= d;
}
return d;
}
inline unsigned prev(unsigned i, unsigned n) { return (i+n-1) % n; }
inline unsigned next(unsigned i, unsigned n) { return (i+1) % n; }
static void DrawFatLine(const float* pts, unsigned npts, float width)
{
const float MITER_LIMIT = 1.20f;
const float w = width/2;
float plx, ply, prx, pry, d;
glBegin(GL_LINES);
for (unsigned i = 0; i < npts+1; ++i)
{
const float* v0 = pts + prev(i,npts)*2;
const float* v1 = pts + (i%npts)*2;
const float* v2 = pts + next(i,npts)*2;
// Calculate direction and normal of both segments.
float dx0 = v1[0] - v0[0]; float dy0 = v1[1] - v0[1];
float dx1 = v2[0] - v1[0]; float dy1 = v2[1] - v1[1];
// Normalize directions
d = sqrtf(dx0*dx0 + dy0*dy0);
dx0 /= d; dy0 /= d;
d = sqrtf(dx1*dx1 + dy1*dy1);
dx1 /= d; dy1 /= d;
// Normals
float nx0 = -dy0; float ny0 = dx0;
float nx1 = -dy1; float ny1 = dx1;
// Blended middle normal.
float nmx = (nx0 + nx1) * 0.5f; float nmy = (ny0 + ny1) * 0.5f;
float nmr2 = nmx*nmx + nmy*nmy;
bool bevel = false;
if (nmr2 * MITER_LIMIT*MITER_LIMIT < 1.0f)
bevel = true;
if (nmr2 > 0.000001f)
{
float scale = 1.0f / nmr2;
// Limit the scale on really acute angles so
// that the inner vertex does not explode.
if (scale > 20.0f) scale = 20.0f;
nmx *= scale; nmy *= scale;
}
if (bevel)
{
float cross = dx1 * dy0 - dx0 * dy1;
if (cross < 0)
{
// Turn left
float lx = v1[0] + nmx * w; float ly = v1[1] + nmy * w;
float rx0 = v1[0] - nx0 * w; float ry0 = v1[1] - ny0 * w;
float rx1 = v1[0] - nx1 * w; float ry1 = v1[1] - ny1 * w;
if (i > 0)
{
glVertex2f(plx, ply);
glVertex2f(lx, ly);
glVertex2f(prx, pry);
glVertex2f(rx0, ry0);
glVertex2f(rx0, ry0);
glVertex2f(rx1, ry1);
}
plx = lx; ply = ly;
prx = rx1; pry = ry1;
}
else
{
// Turn right
float lx0 = v1[0] + nx0 * w; float ly0 = v1[1] + ny0 * w;
float lx1 = v1[0] + nx1 * w; float ly1 = v1[1] + ny1 * w;
float rx = v1[0] - nmx * w; float ry = v1[1] - nmy * w;
if (i > 0)
{
glVertex2f(plx, ply);
glVertex2f(lx0, ly0);
glVertex2f(lx0, ly0);
glVertex2f(lx1, ly1);
glVertex2f(prx, pry);
glVertex2f(rx, ry);
}
plx = lx1; ply = ly1;
prx = rx; pry = ry;
}
}
else
{
float lx = v1[0] + nmx * w; float ly = v1[1] + nmy * w;
float rx = v1[0] - nmx * w; float ry = v1[1] - nmx * w;
glVertex2f(plx, ply);
glVertex2f(lx, ly);
glVertex2f(prx, pry);
glVertex2f(rx, ry);
plx = lx; ply = ly;
prx = rx; pry = ry;
}
}
glEnd();
}