2D 旋转打开

2d rotation opengl

本文关键字:旋转 2D      更新时间:2023-10-16

这是我正在使用的代码。

#define ANGLETORADIANS 0.017453292519943295769236907684886f // PI / 180
#define RADIANSTOANGLE 57.295779513082320876798154814105f   // 180 / PI
rotation = rotation *ANGLETORADIANS;
cosRotation = cos(rotation);
sinRotation = sin(rotation);
for(int i = 0; i < 3; i++)
{
    px[i] = (vec[i].x + centerX) * (cosRotation - (vec[i].y + centerY)) * sinRotation;
    py[i] = (vec[i].x + centerX) * (sinRotation + (vec[i].y + centerY)) * cosRotation;
    printf("num: %i, px: %f, py: %fn", i, px[i], py[i]);
}

到目前为止,我的 Y 值正在翻转。. 假设我输入 X = 1 和 Y = 1 的值,旋转 45 次,您应该看到大约 x = 0 和 y = 1.25 ish,但我得到 x = 0 y = -1.25。

此外,我的 90 度旋转总是返回 x = 0 和 y = 0。

p.s 我知道我只是以我的价值观为中心,而不是把它们放回原来的地方。不需要把它们放回去,因为我只需要知道我现在得到的价值。

你的括号位置对我来说看起来不对。我期望:

px[i] = (vec[i].x + centerX) * cosRotation - (vec[i].y + centerY) * sinRotation;
py[i] = (vec[i].x + centerX) * sinRotation + (vec[i].y + centerY) * cosRotation;

你的括号是错误的。它应该是

px[i] = ((vec[i].x + centerX) * cosRotation) - ((vec[i].y + centerY) * sinRotation);
py[i] = ((vec[i].x + centerX) * sinRotation) + ((vec[i].y + centerY) * cosRotation);

相反