三角旋转造成变形
Triangle rotation causes deformation
我想旋转我的三角形,但有一些问题。
其默认形式:
我正在用箭头键旋转它,但如您所见,它在三角形形状上有一些变形:
这是我的代码:
typedef struct {
point_t pos; // position of the triangle
float angle; // view angle
float r;
} weapon_t;
void drawPlayer(weapon_t tw) {
glBegin(GL_TRIANGLES);
glColor3f(0.1, 0.2, 0.3);
glVertex2f(tw.pos.x, tw.pos.y);
glVertex2f(tw.pos.x + 150 * cos(tw.angle * D2R), tw.pos.y + 100 * sin(tw.angle * D2R) + 8);
glVertex2f(tw.pos.x + 150 * cos(tw.angle * D2R), tw.pos.y + 100 * sin(tw.angle * D2R) - 8);
glEnd();
}
void onTimer(int v) {
glutTimerFunc(TIMER_PERIOD, onTimer, 0);
if (right) {
if (weapon.angle != -45)
turnWeapon(&weapon, -3);
}
if (left) {
if (weapon.angle != 45)
turnWeapon(&weapon, 3);
}
伙计们有什么想法吗?
我不知道你的公式是从哪里来的,但它们是错误的。要围绕角度 x 逆时针旋转 2D 矢量,您可以使用旋转矩阵 [cos(x), -sin(x) ; sin(x), cos(x)](你可以用 exp(i*x) = cos(x) + i*sin(x)
轻松证明)。你想旋转向量 [150, 108] 和 [150, 92] 如果你将它们乘以旋转矩阵你得到 [150*cos(x) - 108*sin(x), 150*sin(x) + 108 *cos(x)] 和 [150*cos(x) - 92*sin(x), 150*sin(x) + 92*cos(x)].
翻译成代码如下所示:
float c = cos(tw.angle * D2R);
float s = sin(tw.angle * D2R);
glVertex2f(tw.pos.x + 150*c - 108*s, tw.pos.y + 150*s + 108*c);
glVertex2f(tw.pos.x + 150*c - 92*s, tw.pos.y + 150*s + 92*c);
我想旋转我的三角形,但有一些问题。
其默认形式:
我正在用箭头键旋转它,但如您所见,它在三角形形状上有一些变形:
这是我的代码:
typedef struct {
point_t pos; // position of the triangle
float angle; // view angle
float r;
} weapon_t;
void drawPlayer(weapon_t tw) {
glBegin(GL_TRIANGLES);
glColor3f(0.1, 0.2, 0.3);
glVertex2f(tw.pos.x, tw.pos.y);
glVertex2f(tw.pos.x + 150 * cos(tw.angle * D2R), tw.pos.y + 100 * sin(tw.angle * D2R) + 8);
glVertex2f(tw.pos.x + 150 * cos(tw.angle * D2R), tw.pos.y + 100 * sin(tw.angle * D2R) - 8);
glEnd();
}
void onTimer(int v) {
glutTimerFunc(TIMER_PERIOD, onTimer, 0);
if (right) {
if (weapon.angle != -45)
turnWeapon(&weapon, -3);
}
if (left) {
if (weapon.angle != 45)
turnWeapon(&weapon, 3);
}
伙计们有什么想法吗?
我不知道你的公式是从哪里来的,但它们是错误的。要围绕角度 x 逆时针旋转 2D 矢量,您可以使用旋转矩阵 [cos(x), -sin(x) ; sin(x), cos(x)](你可以用 exp(i*x) = cos(x) + i*sin(x)
轻松证明)。你想旋转向量 [150, 108] 和 [150, 92] 如果你将它们乘以旋转矩阵你得到 [150*cos(x) - 108*sin(x), 150*sin(x) + 108 *cos(x)] 和 [150*cos(x) - 92*sin(x), 150*sin(x) + 92*cos(x)].
翻译成代码如下所示:
float c = cos(tw.angle * D2R);
float s = sin(tw.angle * D2R);
glVertex2f(tw.pos.x + 150*c - 108*s, tw.pos.y + 150*s + 108*c);
glVertex2f(tw.pos.x + 150*c - 92*s, tw.pos.y + 150*s + 92*c);