Rotation matrix scales the object

Started by Mihai_Ionut_Floares, January 08, 2021, 01:39:19

Previous topic - Next topic

Mihai_Ionut_Floares

Roation matrix code:
public static mat4 rotateMat(mat4 mat, float angle, vec3 axis) {
		mat4 result = new mat4();
		float radians = (float) Math.toRadians(angle); 
		float cos = (float) Math.cos(radians);
		float sin = (float) Math.sin(radians);
		float x2 = (float) Math.pow(axis.x, 2);
		float y2 = (float) Math.pow(axis.y, 2);
		float z2 = (float) Math.pow(axis.z, 2);
		result.mat[0][0]+=cos+x2*(1-cos);
		result.mat[0][1]+=axis.x*axis.y*(1-cos)-axis.z*sin;
		result.mat[0][2]+=axis.x*axis.z*(1-cos)+axis.z*sin;
		
		result.mat[1][0]+=axis.y*axis.x*(1-cos)+axis.z*sin;
		result.mat[1][1]+=cos+z2*(1-cos);
		result.mat[1][2]+=axis.y*axis.z*(1-cos)+axis.x*sin;
		
		result.mat[2][0]+=axis.z*axis.x*(1-cos)-axis.y*sin;
		result.mat[2][1]+=axis.z*axis.y*(1-cos)-axis.x*sin;
		result.mat[2][2]+=cos+z2*(1-cos);
		
		
		
		
		
		return result;
	}

Source:


It rotates, but also scales the object, why?
And it seems logic, because the scaling is set by the diagonal of the matrix, but the rotation matrix overwrites that diagonal