Java Matrix transformation

345 views Asked by At

I'm trying to render a quad and then use a transformation matrix to move it around the display. I've tracked the issue back to my createTransformationMatrix method. I know that the issue is most likely in the way I'm calling functions from my matrix class. The code in question is the following:

public static Matrix4f createTransformationMatrix(Vector3f translation, float rx, float ry, float rz, float scale) {
        Matrix4f matrix = new Matrix4f();
        Matrix4f.translate(translation.x, translation.y, translation.z);
        Matrix4f.rotate((float) Math.toDegrees(rx), 1, 0, 0);
        Matrix4f.rotate((float) Math.toDegrees(ry), 0, 1, 0);
        Matrix4f.rotate((float) Math.toDegrees(rz), 0, 0, 1);
        Matrix4f.scale(scale, scale, scale);

        return matrix;
}

I think that the issue is with calling Matrix4f for the transformations, but they are static, so I'm not sure how to fix this issue. If needed the transformation functions are the following.

public static Matrix4f translate(float x, float y, float z) {
        Matrix4f translation = new Matrix4f();

        translation.m03 = x;
        translation.m13 = y;
        translation.m23 = z;

        return translation;
}

public static Matrix4f rotate(float angle, float x, float y, float z) {
        Matrix4f rotation = new Matrix4f();

        float c = (float) Math.cos(Math.toRadians(angle));
        float s = (float) Math.sin(Math.toRadians(angle));
        Vector3f vec = new Vector3f(x, y, z);
        if (vec.length() != 1f) {
            vec = vec.normalize();
            x = vec.x;
            y = vec.y;
            z = vec.z;
        }

        rotation.m00 = x * x * (1f - c) + c;
        rotation.m10 = y * x * (1f - c) + z * s;
        rotation.m20 = x * z * (1f - c) - y * s;
        rotation.m01 = x * y * (1f - c) - z * s;
        rotation.m11 = y * y * (1f - c) + c;
        rotation.m21 = y * z * (1f - c) + x * s;
        rotation.m02 = x * z * (1f - c) + y * s;
        rotation.m12 = y * z * (1f - c) - x * s;
        rotation.m22 = z * z * (1f - c) + c;

        return rotation;
}

public static Matrix4f scale(float x, float y, float z) {
        Matrix4f scaling = new Matrix4f();

        scaling.m00 = x;
        scaling.m11 = y;
        scaling.m22 = z;

        return scaling;
}

It is worth noting that my matrix class was made by SilverTiger, as I am not familiar enough with matrix math and linear algebra to write such classes.

Help is much appreciated.

1

There are 1 answers

0
Nick Clark On

You never write to matrix in the createTransformationMatrix method. You need to hold a reference to the translation, rotation, and scaling matrices, then multiply them together. scale * rotation * translation should give you the correct transformation matrix. If the transformation order feels "flipped", just transpose the order of the multiplication.