我正在尝试开发2D游戏。我想知道是否有一种方法可以计算出如图所示旋转框的四个顶点的位置。我现在正在使用此代码进行计算,但结果错误。
// cx, cy - center of square coordinates
// x, y - coordinates of a corner point of the square
// theta is the angle of rotation
// translate point to origin
float tempX = x - cx;
float tempY = y - cy;
// now apply rotation
float rotatedX = tempX*cos(theta) - tempY*sin(theta);
float rotatedY = tempX*sin(theta) + tempY*cos(theta);
// translate back
x = rotatedX + cx;
y = rotatedY + cy;
旋转运动与摆锤相同。我目前正在旋转图片,旋转点位于x = width / 2
和y = 10
。我正在使用SDL2作为图形库。
您拥有的方程式适用于右手坐标系,而您使用的是左手系。
尝试
// now apply rotation
float rotatedX = +tempX*cos(theta) + tempY*sin(theta);
float rotatedY = -tempX*sin(theta) + tempY*cos(theta);