Reputation: 763
I am currently trying some simple line rotation in sdl and I have found a function that rotates a point around another point. Here it is ( changed to work with sdl ).
SDL_Point rotate_point(double cx, double cy, double angle, SDL_Point p)
{
double pi = acos(-1);
double rotation_angle = (double)angle / 180.0 * pi;
double s = sin(rotation_angle);
double c = cos(rotation_angle);
// translate point back to origin:
p.x -= cx;
p.y -= cy;
// rotate point
double xnew = p.x * c - p.y * s;
double ynew = p.x * s + p.y * c;
// translate point back:
p.x = xnew + cx;
p.y = ynew + cy;
return p;
}
Now im having a problem where the line gets shorter and shorter until it stops at the center point im rotating it around. I dont know why.
Here is the way im using the function:
double angle1=0.1, angle2=0,oldangle1=0,oldangle2=0;
SDL_Point line11 = { 10,0 }, line12 = { 110,0 }, line21 = { 110,0 }, line22 = { 210,0 };
SDL_Event event;
int mousex = 0, mousey = 0;
while (SDL_PollEvent(NULL)) {
SDL_GetMouseState(&mousex, &mousey);
if (GetAsyncKeyState('W'))
angle1 += 0.1;
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
if (angle1 < oldangle1-0.1||angle1 > oldangle1 + 0.1) {
line12 = rotate_point(0, 0, (angle1-oldangle1), line12);
line21 = rotate_point(0, 0, (angle1 - oldangle1), line21);
oldangle1 = angle1;
}
if (angle2 < oldangle2 - 0.1 || angle2 > oldangle2 + 0.1) {
line22 = rotate_point(line21.x,line21.y, (angle2 - oldangle2), line22);
oldangle2 = angle2;
}
if (angle2 == 0)
angle2 = angle1;
printf("%f ", angle2);
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 0);
SDL_RenderDrawLine(renderer, line11.x, line11.y, line12.x, line12.y);
SDL_RenderDrawLine(renderer, line21.x, line21.y, line22.x, line22.y);
//solveIK(&angle1, &angle2, false, 100, 100, mousex, mousey);
//angle1 = get_degrees(angle1);
//angle2 = get_degrees(angle2);
SDL_RenderPresent(renderer);
SDL_Delay(16);
}
I know that it is incredibly messy but ive just started with sdl and c++. i will try to fix it up after ive gotten it to work.
What ive already tried:
Different functions for rotating. They all seem to be worse in terms of the line shortening. Its most likely not a problem with the rotation function.
Rewriting the code. This is version 3 and it works the best but is also the worst looking.
Upvotes: 1
Views: 830
Reputation: 275760
Do not rerotate.
Start with the initial points. Now apply a rotation.
If you have a new rotation, start with the initial points and apply a new rotation. Don't rotate the result of the first rotation.
In general, floating point operations induce error away from the theoretical result. Rounding to integers even more error (not sure if your code does that). Cascading large numbers of floating point ops will result in garbage sometimes.
Now, adding angles will also have rounding error, but not in a noticable way: it will have rotated more/less than it should have, not stretch/shrink the line. Again here you can replace repeated addition with a fixed number of multiplications to reduce this, if you care.
Upvotes: 3