# Will a math wiz please help with my own polygon rotation function?

## Recommended Posts

xorjesus    100
Hi there, I just up with this method of rotating polygons around a made up origin about about 20 minutes ago. I'm sure its been done a million times, but this just seems a logical way to do it. I find the polygons minimum and maximimum vertexes, and than find the center of the polygon. I than use the standard rotation formula on the vertexes. It almost works perfect except the polygon drifts to the side randomly and than generally returns to the point of origin. It does mess about with the y point luckily. Could anyone take a quick look at my function and look for my mistake?
// this will rotate the polygon based on its precalcaluted center,
void RotatePolygon(Vector verts[], int &num_verts, float &angle)
{
// IF DEBUG
if (num_verts == 0)
return;
// END IF

float cosa = cos(angle),
sina = sin(angle);

Vector min_y = verts[0];
Vector max_y = verts[0];
Vector min_x = verts[0];
Vector max_x = verts[0];

// find the min and max;

for (int j=0; j<num_verts; j++)
{
if (verts[j].x < min_x.x)
min_x = verts[j];

if (verts[j].x > max_x.x)
max_x = verts[j];

if (verts[j].y < min_y.x)
min_y = verts[j];

if (verts[j].x > max_y.y)
min_y = verts[j];
}

Vector local_origin = { (min_x.x+max_x.x)/2, (min_y.y+max_y.y)/2 }; // the center is the mid point between the maxes and mins

for (int i=0; i<num_verts; i++)
{
float x = verts[i].x - local_origin.x;
float y = verts[i].y - local_origin.y;
verts[i].x = (local_origin.x) + (x*cosa - y*sina);
verts[i].y = (local_origin.y) + (x*sina + y*cosa);
}

}



##### Share on other sites
Chris Hare    462
Maybe a twenty would loosen my lips...

##### Share on other sites
Muse    254
Hmm...it may just be that you're not rotating about the polygon's center of mass. Just for grins, replace your existing code to find the rotation point with:

Vector COM;COM.x = COM.y = 0;int i;for(i = 0 ; i < num_verts ; i++){    COM.x += verts[i].x;    COM.y += verts[i].y;}COM.x /= num_verts;COM.y /= num_verts;

And then use COM for your local_origin. If you were planning on applying that function to large meshes (not that you should; it would almost certainly be better to use your graphics API of choice to do that) you could precompute the local origin of each mesh.

##### Share on other sites
xorjesus    100
LOL chris :] Yes muse, it was my centering logic in my code. Thank you very much for pointing that one out. My center was getting refreshed with every iteration of that function. The local center should be member of that structure permenently, and only be recalculated when a transformation is applied that would require it. Ahhh, rotating 2D polygons :] The joy of being a beginner on this journey :]

##### Share on other sites
oliii    2196
It's usually better not to rotate polygons by delta angles and translate them by delta positions. Rather, Use the original polygon and rotate and translate a copy to the current object's position. Using deltas every frames can introduce a drift, and the in extreme cases, the polygon will blow up.

##### Share on other sites
xorjesus    100
Wow! Thanks oliii. I didn't even think of that. That's super easy to do. You roxor. I'm still overjoyed you released those tutorials on collision detection. Unfortunately, because of school, I haven't had the time to read them properly, but that seperating axis method looks like something I could understand. Once I do, I'll be able to make my tank game that shows off motion along a curved math to my physics teacher. Arrrgh! I wish I knew more math! Thx again.

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account