I'm making my own polygon class for a game I'm making. However, whenever I try to apply rotation to 'em, it shrinks the entire polygon. After around 1000 rotations the shrinking becomes apparent, and visually you can notice them shrinking rapidly. Is there any way to minimize this? I'm assuming it's some precision errors? Here's my code below, I'm hoping it's enough to see my problem.
(X and Y are macros btw)
void Poly2D::rotateBy(const float angle){
const float sinA = sinf(angle), cosA = cosf(angle);
for(ushort i = 0; i < vertLength; i++){
float &x = vertices[X], &y = vertices[Y];
//move to origin for rotating
x -= center -> x;
y -= center -> y;
x = (x * cosA) - (y * sinA);
y = (x * sinA) + (y * cosA);
This is common in graphics due to compounding rounding issues. I would suggest keeping the original polygon co-ordinates untouched, and do rendering based on a copy which is the original polygon rotated by the accumulated rotations. Also consider letting the GPU do the work, as it's pretty good at applying a transformation to a sequence of primitives.
This is common in graphics due to compounding rounding issues. I would suggest keeping the original polygon co-ordinates untouched, and do rendering based on a copy which is the original polygon rotated by the accumulated rotations. Also consider letting the GPU do the work, as it's pretty good at applying a transformation to a sequence of primitives.
Thanks, I figured that was the issue, I'll resolve it quite easily now. Also I'm not very good at things like GLSL, lol.
Strange... I took your advice, and I created another array to hold a copy of the vertex data to apply the rotations to... now it's wanting to rotate on like every axis?
void Poly2D::rotateBy(const float angle){
//accumulated rotation... what's the efficient way to clamp this?
rotation += angle;
const float sinA = sinf(rotation), cosA = cosf(rotation);
for(ushort i = 0; i < vertLength; i++){
//this is just a copy of the vertex data
rotVertices[X] = vertices[X];
rotVertices[Y] = vertices[Y];
float &x = rotVertices[X], &y = rotVertices[Y];
x -= center -> x;
y -= center -> y;
x = (x * cosA) - (y * sinA);
y = (x * sinA) + (y * cosA);
//accumulated rotation... what's the efficient way to clamp this?
rotation += angle;[/quote]
You don't. Unless your polygon is rotating at an insane rate you will not run out of accuracy any time soon. Don't worry about it (if you really want to, and with small increments (i.e. less than a full rotation) you can simply decrement it by 2pi if it gets greater than 2pi, sparing the expensive general-purpose real remainder operation)
What do you mean by rotating on every axis? There's only one possible rotation axis in your code which is the virtual depth axis through the object's center. Do you mean the vertices start rotating with incorrect centers or something? Could you elaborate and possibly post all of the rotation-related code, my guess is you forgot to change vertices to rotVertices somewhere.
“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”
Guys, please help... I'm really desperate to get this done... please, can anyone explain this unfixable, sudden problem? It ONLY does this when I reset the rotated polygon and try to rotate it from there...
x = (x * cosA) - (y * sinA);
y = (x * sinA) + (y * cosA);
These equations work on the original coordinates. But in the first equation, you overwrite the x, and you use the overwritten x in the second equation. you have to use temporary variables.
This was the mistake all along, but the angle was small first, so the mistake only resulted in shrinking.
//move back
x = x_temp + center -> x;
y = y_temp + center -> y;
There are precision errors, but these shouldn't be apparent under so few rotations. I use this kind of accumulated rotation in a few places, and I never needed to use correction on the vertices.