c++ - 使用旋转矩阵旋转三角形 - 从窗口中消失

标签 c++ xcode rotation openframeworks

我尝试使用旋转矩阵围绕其中心顺时针旋转三角形。但是,三角形会移出屏幕并移向左下角。 这是我的代码

class Triangle {
    public:
    vector<glm::vec3> verts; // positions of the triangles vertices.
    glm::vec3 pos; // position of the triangle's origin.

    void rotate(float rotationMat[4]) {
        // Transform vert 1
        glm::vec3 vert1 = verts[0]; // Get the vertices (centred about the triangle origin)
        vert1 = vert1 - pos; // Get the vertices centered about the origin (0, 0)
        // Rotate the vertices
        vert1.x = (vert1.x * rotationMat[0]) + (vert1.y * rotationMat[1]);
        vert1.y = (vert1.x * rotationMat[2]) + (vert1.x * rotationMat[3]);
        // Get the vertices centered about the triangle origin
        vert1 = vert1 + pos;

        // Set the new vertices value
        verts[0] = vert1;



        // Transform vert2
        glm::vec3 vert2 = verts[1]; // Get the vertices (centered about the triangle origin)
        vert2 = vert2 - pos; // Get the vertices centered about the origin (0, 0)
        // Rotate the vertices
        vert2.x = (vert2.x * rotationMat[0]) + (vert2.y * rotationMat[1]);
        vert2.y = (vert2.x * rotationMat[2]) + (vert2.x * rotationMat[3]);
        // Get the vertices centered about the triangle origin
        vert2 = vert2 + pos;

        // Set the new vertices value
        verts[1] = vert2;



        // Transform vert3
        glm::vec3 vert3 = verts[2]; // Get the vertices (centered about the triangle origin)
        vert3 = vert3 - pos; // Get the vertices centered about the origin (0, 0)
        // Rotate the vertices
        vert3.x = (vert3.x * rotationMat[0]) + (vert3.y * rotationMat[1]);
        vert3.y = (vert3.x * rotationMat[2]) + (vert3.x * rotationMat[3]);
        // Get the vertices centered about the triangle origin
        vert3 = vert3 + pos;

        // Set the new vertices value
        verts[2] = vert3;

    }
};

我这样调用方法:

float rotationMat[4] = { (float) cos(1), (float) -sin(1), (float) sin(1), (float) cos(1)};
triangle.rotate(rotationMat);

作为测试,我打印出旋转前后的顶点,结果如下:

BEFORE: 
(100,-100)
(0,100)
(-100,-100)

AFTER: 
(788.762,899.003)
(566.437,591.801)
(680.701,749.688)

我哪里错了?为什么三角形会飞出屏幕?

最佳答案

你不能那样做:

vert1.x = (vert1.x * rotationMat[0]) + (vert1.y * rotationMat[1]);
vert1.y = (vert1.x * rotationMat[2]) + (vert1.x * rotationMat[3]);

在第一行之后,vert1.x 将发生变化,您在第二行所做的计算将无效。做:

auto newx = (vert1.x * rotationMat[0]) + (vert1.y * rotationMat[1]);
auto newy = (vert1.x * rotationMat[2]) + (vert1.x * rotationMat[3]);
vert1.x = newx;
vert1.y = newy;

(可能还有其他问题)

关于c++ - 使用旋转矩阵旋转三角形 - 从窗口中消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57828682/

相关文章:

c++ - 序列化部分工作 - boost::asio

Javascript 在鼠标向下拖动时旋转

c++ - 创建一个围绕对象旋转的相机 OpenGL C++

ios - iTunes 连接,Xcode 在管理器中验证

ios - 在 xcode "\\\\\\\"$(SRCROOT)/myprojectname\\\\\\\"中的库搜索路径中,所有这些反斜杠是什么意思?

ios - Xcode 调试器崩溃 (Xcode 6.1/LLDB/Yosemite)

iphone - 将加速度计输出旋转到新的 "neutral",适用于 iOS(和其他)。

c++ - 我应该在这里使用什么类型的转换表?

使用 std::unique_ptr 的 C++ Pimpl Idiom 不完整类型

c++ - extern const 数组不链接