我的2D旋转矩阵有什么问题?

时间:2018-04-16 08:49:22

标签: opengl graphics 2d linear-algebra

我有这个旋转矩阵围绕Z旋转:

function rotation4x4(angle: number, output: number[], origin_x: number = 0.0, origin_y: number = 0.0)
{
    let x = origin_x;
    let y = origin_y;

    let r00 = cos(angle);
    let r01 = -sin(angle);
    let r10 = sin(angle);
    let r11 = cos(angle);

    mat4x4_set(output, 0, 0, r00);
    mat4x4_set(output, 0, 1, r01);
    mat4x4_set(output, 0, 2, x-r00*x-r01*y);
    mat4x4_set(output, 0, 3, y-r10*x-r11*y);

    mat4x4_set(output, 1, 0, r10);
    mat4x4_set(output, 1, 1, r11);
    mat4x4_set(output, 1, 2, 0);
    mat4x4_set(output, 1, 3, 0);

    mat4x4_set(output, 2, 0, 0);
    mat4x4_set(output, 2, 1, 0);
    mat4x4_set(output, 2, 2, 1);
    mat4x4_set(output, 2, 3, 0);

    mat4x4_set(output, 3, 0, 0);
    mat4x4_set(output, 3, 1, 0);
    mat4x4_set(output, 3, 2, 0);
    mat4x4_set(output, 3, 3, 1);
}
然后,我将我渲染的所有顶点与此矩阵相乘:

x = mat4x4_get(m, 0, 0) * x + mat4x4_get(m, 0, 1) * y + mat4x4_get(m, 0, 2) * z + mat4x4_get(m, 0, 3) * w;
y = mat4x4_get(m, 1, 0) * x + mat4x4_get(m, 1, 1) * y + mat4x4_get(m, 1, 2) * z + mat4x4_get(m, 1, 3) * w;
z = mat4x4_get(m, 2, 0) * x + mat4x4_get(m, 2, 1) * y + mat4x4_get(m, 2, 2) * z + mat4x4_get(m, 2, 3) * w;
w = mat4x4_get(m, 3, 0) * x + mat4x4_get(m, 3, 1) * y + mat4x4_get(m, 3, 2) * z + mat4x4_get(m, 3, 3) * w;

但是当我尝试渲染一个围绕一个点旋转的圆圈时,我得到了这个: enter image description here

我想要作为圆心的点在这个形状的中间。

预期结果(但更顺畅): enter image description here

这里我给每个象限一个颜色:

似乎只有y不正确?

1 个答案:

答案 0 :(得分:1)

我很抱歉。我真笨。看看这段代码:

x = mat4x4_get(m, 0, 0) * x + mat4x4_get(m, 0, 1) * y + mat4x4_get(m, 0, 2) * z + mat4x4_get(m, 0, 3) * w;
y = mat4x4_get(m, 1, 0) * x + mat4x4_get(m, 1, 1) * y + mat4x4_get(m, 1, 2) * z + mat4x4_get(m, 1, 3) * w;
z = mat4x4_get(m, 2, 0) * x + mat4x4_get(m, 2, 1) * y + mat4x4_get(m, 2, 2) * z + mat4x4_get(m, 2, 3) * w;
w = mat4x4_get(m, 3, 0) * x + mat4x4_get(m, 3, 1) * y + mat4x4_get(m, 3, 2) * z + mat4x4_get(m, 3, 3) * w;

我使用新的x来计算y坐标。现在它起作用了:

enter image description here

相关问题