Manual Rotation of vertices about center of shape

Started by
11 comments, last by mameman 17 years, 1 month ago
I think you misunderstood me, either that or I'm just being dumb (most likely), all I want to do is scrap the rotation and just have the set angle, but the code I've got at the moment doesn't just set an angle, it constantly rotates the square .

I know these two lines need altering:

vertices[0] = fx * cosf(degInRad) - fy * sinf(degInRad);
vertices[1] = fx * sinf(degInRad) + fy * cosf(degInRad);

but again I'm stuck with the math.

So, to re-iterate, the shape starts of with no rotation. I then call my rotate method and the shape is redisplayed, rotated to whatever angle I wanted. This would be the same as calling glRotatef on the shape once.
Advertisement
Yep I guessed right. I think I am being dumb. Basically all I need to do is account for the fact that the angle is added onto the current angle of the shape.
The problem with my solution is that the vertex data is constantly being updated (correct?). So what you will need to do is store the initial WORLD co-ordinates of your object and work with those. Looking at you code you may want to do it this way.

void orbit(float angle)
{
const float DEG2RAD = (float) 3.14159/180;

if (angle > 360) angle -= 360;
if (angle < 0) angle += 360;

float degInRad = angle*DEG2RAD;

for (int i = 0; i < 4; i++)
{
vertices[0] = initial_vertex[0];
vertices[1] = initial_vertex[1];

vertices[0] -= x; // move into local space
vertices[1] -= y;
}

for (int i = 0; i < 4; i++)
{

float fx = vertices[0];
float fy = vertices[1];

vertices[0] = fx * cosf(degInRad) - fy * sinf(degInRad); // rotate point
vertices[1] = fx * sinf(degInRad) + fy * cosf(degInRad);
}

for (int i = 0; i < 4; i++)
{
vertices[0] += x; // move back to world space
vertices[1] += y;
}
}

BEWARE: If you perform a translation operation then you will need to update initial_vertex as well as vertices. Hope this solves it.

This topic is closed to new replies.

Advertisement