[solved] [LWJGL] Changing the pivot point (origin) of a model for rotations.

Hello!

I am developing a first person shooter using LWJGL. Currently, for my “view gun” I am doing the following:

public ModelMatrix getViewModelMatrix(Camera camera) {
	//Rotate hand/gun to cameras rotation, and draw
	float rx = 0;
	float ry = (float)Math.toDegrees(camera.getPitch()) + 90f;
	float rz = (float)-Math.toDegrees(camera.getYaw()) + 90f;
	
	ModelMatrix matrix = new ModelMatrix();
	matrix.matrix_add_translation(camera.getX(), camera.getY(), camera.getZ());
	matrix.matrix_add_rotation_z(rz);
	matrix.matrix_add_rotation_y(ry);
	matrix.matrix_add_rotation_x(rx);
	
	return matrix;
}

And this works! With this matrix setup I can draw my gun exactly where the camera is.

However, I want to add a rotate effect: I want to rotate the gun by the cameras delta yaw. This is quite easy to do, I created a “yawSpeed” variable, and when the mouse moves in the X direction, I add to the speed; I also have a friction to return it to 0.
This works, however the origin of the model is at 0, 0, 0. If you look at the picture above, it pivots the gun at the axis origin around the players head.

I want to change the origin axis to about where the gun is, so when the gun rotates it isn’t moving around the cameras position, but instead rotating around the guns position.

TL:DR
I want to draw my gun at the same position visually, but change the point where it rotates.

Any ideas?

I’ve been wondering the same thing.

Tip: Use a quaternion based camera. Instead of Euler angles

I’ll most likely end up using quaternions for the gun model. The camera doesn’t need them, as it will only ever rotate with pitch and yaw (I don’t like roll in FPS’s).

Translate the model to the desired origin, rotate, and then translate it back to the original origin.

EDIT: In this case I think you would add the offset to your initial translation to the camera position, rotate, and then translate again by -'ve offset.

if I translate to (1, 0, 1), rotate by (0, 45 degrees, 0), then translate (-1, 0, -1), the gun will be in a different position.

Turns out you were right; I was overthinking it :slight_smile:

Your solution works perfectly!

Good to hear. :slight_smile: