计算旋转框的顶点的位置

问题描述 投票:-1回答:1

enter image description here

我正在尝试开发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 / 2y = 10。我正在使用SDL2作为图形库。

c++ game-physics 2d-games
1个回答
0
投票

您拥有的方程式适用于右手坐标系,而您使用的是左手系。

尝试

// now apply rotation
float rotatedX = +tempX*cos(theta) + tempY*sin(theta);
float rotatedY = -tempX*sin(theta) + tempY*cos(theta);
© www.soinside.com 2019 - 2024. All rights reserved.