Transformation { public Matrix4 asMatrix() { double[][] matrix = rotation.getMatrix(); double scaleX = scale.getX(); double scaleY = scale.getY(); double scaleZ = scale.getZ(); return new Matrix4( matrix[0][0] * scaleX, matrix[0][1] * scaleY, matrix[0][2] * scaleZ, position.getX(), matrix[1][0] * scaleX, matrix[1][1] * scaleY, matrix[1][2] * scaleZ, position.getY(), matrix[2][0] * scaleX, matrix[2][1] * scaleY, matrix[2][2] * scaleZ, position.getZ(), 0, 0, 0, 1); } Transformation(Vector3 position, Quaternion rotation, Vector3 scale); Transformation transformTo(Vector3 position, Quaternion rotation, Vector3 scale); Matrix4 asMatrix(); Vector3 transform(Vector3 vector3);  }
@Test public void identity() throws Exception { Transformation transformation = new Transformation(Vector3.ZERO, Quaternion.IDENTITY, Vector3.ONE); Matrix4 identity = transformation.asMatrix(); assertEquals(Matrix4.IDENTITY, identity); }
@Test public void translate() throws Exception { Transformation transformation = new Transformation(new Vector3(30, 20, 10), Quaternion.IDENTITY, Vector3.ONE); Matrix4 matrix4 = transformation.asMatrix(); assertEquals(1, matrix4.getValue(0, 0), EPSILON); assertEquals(0, matrix4.getValue(0, 1), EPSILON); assertEquals(0, matrix4.getValue(0, 2), EPSILON); assertEquals(30, matrix4.getValue(0, 3), EPSILON); assertEquals(0, matrix4.getValue(1, 0), EPSILON); assertEquals(1, matrix4.getValue(1, 1), EPSILON); assertEquals(0, matrix4.getValue(1, 2), EPSILON); assertEquals(20, matrix4.getValue(1, 3), EPSILON); assertEquals(0, matrix4.getValue(2, 0), EPSILON); assertEquals(0, matrix4.getValue(2, 1), EPSILON); assertEquals(1, matrix4.getValue(2, 2), EPSILON); assertEquals(10, matrix4.getValue(2, 3), EPSILON); assertEquals(0, matrix4.getValue(3, 0), EPSILON); assertEquals(0, matrix4.getValue(3, 1), EPSILON); assertEquals(0, matrix4.getValue(3, 2), EPSILON); assertEquals(1, matrix4.getValue(3, 3), EPSILON); }
@Test public void rotateAroundZ() throws Exception { Transformation transformation = new Transformation(new Vector3(0, 0, 0), Quaternion.fromEulerAngles(0, Math.PI / 2, 0), Vector3.ONE); Matrix4 matrix4 = transformation.asMatrix(); assertEquals(0, matrix4.getValue(2, 0), EPSILON); assertEquals(0, matrix4.getValue(2, 1), EPSILON); assertEquals(1, matrix4.getValue(2, 2), EPSILON); assertEquals(0, matrix4.getValue(2, 3), EPSILON); assertEquals(0, matrix4.getValue(3, 0), EPSILON); assertEquals(0, matrix4.getValue(3, 1), EPSILON); assertEquals(0, matrix4.getValue(3, 2), EPSILON); assertEquals(1, matrix4.getValue(3, 3), EPSILON); assertEquals(0, matrix4.getValue(0, 0), EPSILON); assertEquals(1, matrix4.getValue(0, 1), EPSILON); assertEquals(0, matrix4.getValue(0, 2), EPSILON); assertEquals(0, matrix4.getValue(0, 3), EPSILON); assertEquals(-1, matrix4.getValue(1, 0), EPSILON); assertEquals(0, matrix4.getValue(1, 1), EPSILON); assertEquals(0, matrix4.getValue(1, 2), EPSILON); assertEquals(0, matrix4.getValue(1, 3), EPSILON); }