旋转矩形 - OpenGL ES

时间:2015-01-30 01:03:25

标签: java android opengl-es rotation

在开发游戏的过程中,我创建了一个旋转2d三角形的功能。 但问题是,当我旋转一个物体时,旋转是非常奇怪的。 如果物体的中心位于轴的原点((0,0)点),那么它会旋转很好,但是如果我移动物体然后它以非常奇怪的方式旋转它不应该旋转转身。 正如你在这里看到的那样是函数的代码。

public void rotate(float angle){
        float x1 = x, y1 = y;
        float x2 = x + width, y2 = y;
        float x3 = x + width, y3 = y + height;
        float x4 = x, y4 = y + height;

        // Vertices in object space:
        float[] vector1 = {x1, y1, 0f, 1f};
        float[] vector2 = {x2, y2, 0f, 1f};
        float[] vector3 = {x3, y3, 0f, 1f};
        float[] vector4 = {x4, y4, 0f, 1f};

        // Vertices in world space:
        float[] vector1r = new float[4];
        float[] vector2r = new float[4];
        float[] vector3r = new float[4];
        float[] vector4r = new float[4];

        // Calculate the vertices in world space:
        Matrix.multiplyMV(vector1r, 0, modelMatrix, 0, vector1, 0);
        Matrix.multiplyMV(vector2r, 0, modelMatrix, 0, vector2, 0);
        Matrix.multiplyMV(vector3r, 0, modelMatrix, 0, vector3, 0);
        Matrix.multiplyMV(vector4r, 0, modelMatrix, 0, vector4, 0);

        // Get the middle of the rectangle:
        float[] vecMid = {(vector1r[0] + vector3r[0]) / 2,
                          (vector1r[1] + vector3r[1]) / 2, 0f, 1f};

        move(vecMid[0], vecMid[1]); // Move to the origin
        Matrix.rotateM(modelMatrix, 0, angle, 0, 0, 1f); // Rotate the object
        move(-vecMid[0], -vecMid[1]); // Move back to place

        if(LoggerConfig.ON) 
            Log.d("Middle Vertex", vecMid[0] + ", " + vecMid[1]);
}

移动方法:

    public void move(float x, float y){
        Matrix.translateM(modelMatrix, 0, x, y, 0);
    }

有人可以告诉我这是什么问题吗?

编辑:我对代码做了一些修改并添加了移动方法。

0 个答案:

没有答案
相关问题