0
votes

apologies if this has been asked before, but I couldn't find any suitable answers for my case. I'm trying to rotate an object (collection of faces) but it rotates around the world's origin rather than the object's origin. This is my view() method for the calculation:

public static Matrix view(Vector position, Vector rotator) {
        Matrix translation = Matrix.translate(new Vector(-position.getX(), -position.getY(), -position.getZ()));
        Matrix rotX = Matrix.rotate(rotator.getX(), new Vector(1, 0, 0));
        Matrix rotY = Matrix.rotate(rotator.getY(), new Vector(0, 1, 0));
        Matrix rotZ = Matrix.rotate(rotator.getZ(), new Vector(0, 0, 1));

        Matrix rotation = Matrix.multiply(rotZ, Matrix.multiply(rotY, rotX));

        return Matrix.multiply(translation, rotation);
    }

I have already tried swapping the translation and rotating parameters in the multiply() method but things got a bit weird upon doing so.

Also, this is my vertex shader is case anyone needs it: gl_Position = projection * view * model * vec4(position.x , position.y, position.z, 1.0);

Anyone know the fix for this?

1
Yep, that's exactly what I needed. Thanks!Hydronic Dev

1 Answers

0
votes

I just wanted to document the answer here that I provided in the comments. If you want to rotate an object around a different origin other than (0,0), you will need to offset the rotational matrix by your new origin.

The following post on Math Stack Exchange details how to do this precisely!

Another way to think about this is to move your new origin back to (0, 0) and move your point by the same amount. You can then apply the rotation matrix and then move your rotated point back by the same amount. Take a look at this explanation here to get an idea of this.