Search in sources :

Example 6 with Matrix4x4

use of com.bergerkiller.bukkit.common.math.Matrix4x4 in project BKCommonLib by bergerhealer.

the class MathUtilTest method testQuaternionRotation.

@Test
public void testQuaternionRotation() {
    // Perform the same rotations with a normal 4x4 transform matrix, and a quaternion
    Matrix4x4 transform = new Matrix4x4();
    Quaternion quaternion = new Quaternion();
    transform.rotateX(20.0);
    quaternion.multiply(Quaternion.fromAxisAngles(1.0, 0.0, 0.0, 20.0));
    transform.rotateY(-33.4);
    quaternion.multiply(Quaternion.fromAxisAngles(0.0, 1.0, 0.0, -33.4));
    transform.rotateZ(12.4);
    quaternion.multiply(Quaternion.fromAxisAngles(0.0, 0.0, 1.0, 12.4));
    Matrix4x4 quaternion_transform = quaternion.toMatrix4x4();
    // Confirm that the quaternion transformation is closely equal to the other one
    double[] a = new double[16];
    double[] b = new double[16];
    transform.toArray(a);
    quaternion_transform.toArray(b);
    for (int i = 0; i < 16; i++) {
        assertEquals(a[i], b[i], 0.0001);
    }
    // Also confirm that the yaw/pitch/roll values are equal
    // There will always be some error involved, because the calculations are different
    Vector ypr_a = transform.getYawPitchRoll();
    Vector ypr_b = quaternion.getYawPitchRoll();
    assertEquals(ypr_a.getX(), ypr_b.getX(), 0.001);
    assertEquals(ypr_a.getY(), ypr_b.getY(), 0.001);
    assertEquals(ypr_a.getZ(), ypr_b.getZ(), 0.001);
    // Verify that the matrix turned into a Quaternion is the same
    transform = new Matrix4x4();
    quaternion = new Quaternion();
    transform.translate(5.0, 23.3, -63.2);
    transform.rotateYawPitchRoll(33.2, -53.2, 12.5);
    quaternion.rotateYawPitchRoll(33.2, -53.2, 12.5);
    Quaternion transform_to_quat = transform.getRotation();
    assertEquals(quaternion.getX(), transform_to_quat.getX(), 0.00001);
    assertEquals(quaternion.getY(), transform_to_quat.getY(), 0.00001);
    assertEquals(quaternion.getZ(), transform_to_quat.getZ(), 0.00001);
    assertEquals(quaternion.getW(), transform_to_quat.getW(), 0.00001);
    // Also test the optimized Quaternion rotateX/Y/Z functions
    quaternion = new Quaternion();
    quaternion.rotateX(20.0);
    quaternion.rotateY(-33.4);
    quaternion.rotateZ(12.4);
    quaternion_transform = quaternion.toMatrix4x4();
    quaternion_transform.toArray(b);
    for (int i = 0; i < 16; i++) {
        assertEquals(a[i], b[i], 0.0001);
    }
    // Also test Matrix multiplication with a quaternion
    // This time, the matrix is in a state that is not the identity matrix
    transform = new Matrix4x4();
    transform.translate(20.0, 32.0, -53.0);
    transform.rotateYawPitchRoll(53.0, 34.0, 90.0);
    transform.translate(100.3, -33.2, 95.3);
    quaternion_transform = transform.clone();
    quaternion = new Quaternion();
    transform.rotateYawPitchRoll(-50.3, 34.0, 12.03);
    quaternion.rotateYawPitchRoll(-50.3, 34.0, 12.03);
    quaternion_transform.rotate(quaternion);
    transform.toArray(a);
    quaternion_transform.toArray(b);
    for (int i = 0; i < 16; i++) {
        assertEquals(a[i], b[i], 0.0001);
    }
    // Verify that the 2D vector based rotateX/Y/Z functions for
    // Quaternion and Matrix4x4 do the same thing.
    transform = new Matrix4x4();
    quaternion = new Quaternion();
    transform.rotateX(2.0, 3.0);
    quaternion.rotateX(2.0, 3.0);
    transform.rotateY(-1.3, 2.5);
    quaternion.rotateY(-1.3, 2.5);
    transform.rotateZ(-0.6, 1.12);
    quaternion.rotateZ(-0.6, 1.12);
    quaternion_transform = quaternion.toMatrix4x4();
    transform.toArray(a);
    quaternion_transform.toArray(b);
    for (int i = 0; i < 16; i++) {
        assertEquals(a[i], b[i], 0.0001);
    }
}
Also used : Quaternion(com.bergerkiller.bukkit.common.math.Quaternion) Vector(org.bukkit.util.Vector) Matrix4x4(com.bergerkiller.bukkit.common.math.Matrix4x4) Test(org.junit.Test)

Example 7 with Matrix4x4

use of com.bergerkiller.bukkit.common.math.Matrix4x4 in project BKCommonLib by bergerhealer.

the class MapResourcePack method getItemTexture.

/**
 * Renders the item gui slot texture of an item
 *
 * @param item to render
 * @param width of the produced icon image
 * @param height of the produced icon image
 * @return rendered item slot image
 */
public MapTexture getItemTexture(ItemStack item, int width, int height) {
    Model model = this.getItemModel(item);
    if (model == null || model.placeholder) {
        return createPlaceholderTexture(width, height);
    }
    MapTexture texture = MapTexture.createEmpty(width, height);
    Matrix4x4 transform = new Matrix4x4();
    if (width != 16 || height != 16) {
        transform.scale((double) width / 16.0, 1.0, (double) height / 16.0);
    }
    Model.Display display = model.display.get("gui");
    if (display != null) {
        display.apply(transform);
        texture.setLightOptions(0.0f, 1.0f, new Vector3(-1, 1, -1));
    } else {
    // System.out.println("GUI DISPLAY ELEMENT NOT FOUND");
    }
    texture.drawModel(model, transform);
    return texture;
}
Also used : Model(com.bergerkiller.bukkit.common.map.util.Model) GeneratedModel(com.bergerkiller.bukkit.common.internal.resources.builtin.GeneratedModel) Vector3(com.bergerkiller.bukkit.common.math.Vector3) Matrix4x4(com.bergerkiller.bukkit.common.math.Matrix4x4)

Example 8 with Matrix4x4

use of com.bergerkiller.bukkit.common.math.Matrix4x4 in project BKCommonLib by bergerhealer.

the class MathUtilTest method testMatrixRotation.

@Test
public void testMatrixRotation() {
    Matrix4x4 transform = new Matrix4x4();
    transform.translateRotate(2.0, 3.0, 4.0, -12.0f, 34.0f);
    Vector3 vec = new Vector3();
    transform.transformPoint(vec);
    assertEquals(2.0, vec.x, 0.00001);
    assertEquals(3.0, vec.y, 0.00001);
    assertEquals(4.0, vec.z, 0.00001);
    Vector yawPitchRoll = transform.getYawPitchRoll();
    assertEquals(-12.0, yawPitchRoll.getX(), 0.001);
    assertEquals(34.0, yawPitchRoll.getY(), 0.001);
    assertEquals(0.0, yawPitchRoll.getZ(), 0.001);
    transform.rotateZ(30.0);
    yawPitchRoll = transform.getYawPitchRoll();
    assertEquals(-12.0, yawPitchRoll.getX(), 0.001);
    assertEquals(34.0, yawPitchRoll.getY(), 0.001);
    assertEquals(30.0, yawPitchRoll.getZ(), 0.001);
    transform.scale(1.5, 2.5, 3.5);
    yawPitchRoll = transform.getYawPitchRoll();
    assertEquals(-16.729, yawPitchRoll.getX(), 0.001);
    assertEquals(43.496, yawPitchRoll.getY(), 0.001);
    assertEquals(30.787, yawPitchRoll.getZ(), 0.001);
    // Try a bunch of random yaw/pitch/roll values and see that they all work
    Random rand = new Random();
    for (int i = 0; i < 1000; i++) {
        Vector result;
        Vector rotation = new Vector(180.0 * rand.nextDouble() - 90.0, 180.0 * rand.nextDouble() - 90.0, 180.0 * rand.nextDouble() - 90.0);
        transform = new Matrix4x4();
        transform.rotateYawPitchRoll(rotation);
        result = transform.getYawPitchRoll();
        assertEquals(rotation.getX(), result.getX(), 0.001);
        assertEquals(rotation.getY(), result.getY(), 0.001);
        assertEquals(rotation.getZ(), result.getZ(), 0.001);
    }
}
Also used : Random(java.util.Random) Vector3(com.bergerkiller.bukkit.common.math.Vector3) Vector(org.bukkit.util.Vector) Matrix4x4(com.bergerkiller.bukkit.common.math.Matrix4x4) Test(org.junit.Test)

Aggregations

Matrix4x4 (com.bergerkiller.bukkit.common.math.Matrix4x4)8 Vector3 (com.bergerkiller.bukkit.common.math.Vector3)6 MapTexture (com.bergerkiller.bukkit.common.map.MapTexture)2 Model (com.bergerkiller.bukkit.common.map.util.Model)2 Vector (org.bukkit.util.Vector)2 Test (org.junit.Test)2 GeneratedModel (com.bergerkiller.bukkit.common.internal.resources.builtin.GeneratedModel)1 Quaternion (com.bergerkiller.bukkit.common.math.Quaternion)1 BlockRenderOptions (com.bergerkiller.bukkit.common.wrappers.BlockRenderOptions)1 Point (java.awt.Point)1 Random (java.util.Random)1 ItemStack (org.bukkit.inventory.ItemStack)1