org.joml.Quaternionf Java Examples

The following examples show how to use org.joml.Quaternionf. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SpinningCubeDemo.java    From WraithEngine with Apache License 2.0 6 votes vote down vote up
@Override
public void update(Timer timer)
{
    // Get the rotation component of the game object's transform.
    Quaternionf rot = getGameObject().getTransform()
                                     .getRotation();

    // Get the amount of time, in seconds, that passed since the timer was started.
    // (At the beginning of the game loop.)
    float t = (float) timer.getElapsedTime();

    // Reset the rotation, and apply the new rotation values to it.
    rot.identity();
    rot.rotateLocalX(t * 2.2369f);
    rot.rotateLocalY(t * 1.4562f);
    rot.rotateLocalZ(t * 0.2123f);
}
 
Example #2
Source File: Transform3DTest.java    From WraithEngine with Apache License 2.0 6 votes vote down vote up
@Test
public void equals_diffInstance_diffProperties()
{
    Transform3D t1 = new Transform3D();
    t1.setPosition(1, 2, 4);
    t1.setRotation(new Quaternionf(4, 3, 30, 1));
    t1.setSize(13f);

    Transform3D t2 = new Transform3D();
    t2.setPosition(1, 2, 4);
    t2.setRotation(new Quaternionf(4, 3, 2, 1));
    t2.setSize(13f);

    assertNotEquals(t1, t2);
    assertNotEquals(t1.hashCode(), t2.hashCode());
}
 
Example #3
Source File: OrbitCamera.java    From WraithEngine with Apache License 2.0 6 votes vote down vote up
/**
 * Updates the position of the camera based on the current yaw and pitch. This
 * method will also clamp the yaw and pitch into the acceptable range if
 * required.
 */
public void updatePosition()
{
    yaw %= Math.PI * 2;
    pitch = (float) Math.max(-Math.PI / 2f, Math.min(Math.PI / 2f, pitch));

    Quaternionf rot = camera.getTransform()
                            .getRotation();

    rot.identity()
       .rotateLocalX(pitch)
       .rotateLocalY(yaw);

    camera.getTransform()
          .getPosition()
          .set(0f, 0f, distance)
          .rotate(rot)
          .add(offset);
}
 
Example #4
Source File: CameraTest.java    From WraithEngine with Apache License 2.0 6 votes vote down vote up
@Test
public void defaultProperties()
{
    Camera camera = new Camera(mock(Screen.class));

    assertEquals(0.1f, camera.getNearClip(), 0f);
    assertEquals(1000f, camera.getFarClip(), 0f);
    assertEquals(Math.PI / 2f, camera.getFov(), 0.0001f);

    assertEquals(new Vector3f(), camera.getTransform()
                                       .getPosition());
    assertEquals(new Quaternionf(), camera.getTransform()
                                          .getRotation());
    assertEquals(new Vector3f(1f), camera.getTransform()
                                         .getSize());
}
 
Example #5
Source File: Transform3DTest.java    From WraithEngine with Apache License 2.0 6 votes vote down vote up
@Test
public void equals_diffInstance_diffProperties()
{
    Transform3D t1 = new Transform3D();
    t1.setPosition(1, 2, 4);
    t1.setRotation(new Quaternionf(4, 3, 30, 1));
    t1.setSize(13f);

    Transform3D t2 = new Transform3D();
    t2.setPosition(1, 2, 4);
    t2.setRotation(new Quaternionf(4, 3, 2, 1));
    t2.setSize(13f);

    assertNotEquals(t1, t2);
    assertNotEquals(t1.hashCode(), t2.hashCode());
}
 
Example #6
Source File: Transform3DTest.java    From WraithEngine with Apache License 2.0 6 votes vote down vote up
@Test
public void getLocalMatrix()
{
    Transform3D t = new Transform3D();

    t.setPosition(10f, 30f, 50f);
    t.getRotation()
     .rotateLocalX((float) Math.toRadians(90f));
    t.setSize(4f);

    Matrix4f mat = new Matrix4f();
    t.getLocalMatrix(mat);

    Matrix4f mat2 = new Matrix4f();
    mat2.translate(10f, 30f, 50f);
    mat2.rotate(new Quaternionf().rotateLocalX((float) Math.toRadians(90f)));
    mat2.scale(4f);

    Assert.assertEquals(mat2, mat);
}
 
Example #7
Source File: SpinningCubeDemo.java    From WraithEngine with Apache License 2.0 6 votes vote down vote up
@Override
public void update(Timer timer)
{
    // Get the rotation component of the game object's transform.
    Quaternionf rot = getGameObject().getTransform()
                                     .getRotation();

    // Get the amount of time, in seconds, that passed since the timer was started.
    // (At the beginning of the game loop.)
    float t = (float) timer.getElapsedTime();

    // Reset the rotation, and apply the new rotation values to it.
    rot.identity();
    rot.rotateLocalX(t * 2.2369f);
    rot.rotateLocalY(t * 1.4562f);
    rot.rotateLocalZ(t * 0.2123f);
}
 
Example #8
Source File: MD5Utils.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
public static Quaternionf calculateQuaternion(float x, float y, float z) {
    Quaternionf orientation = new Quaternionf(x, y, z, 0);
    float temp = 1.0f - (orientation.x * orientation.x) - (orientation.y * orientation.y) - (orientation.z * orientation.z);
    if (temp < 0.0f) {
        orientation.w = 0.0f;
    } else {
        orientation.w = -(float) (Math.sqrt(temp));
    }
    return orientation;
}
 
Example #9
Source File: GameItem.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
public GameItem() {
    selected = false;
    position = new Vector3f();
    scale = 1;
    rotation = new Quaternionf();
    textPos = 0;
    insideFrustum = true;
    disableFrustumCulling = false;
}
 
Example #10
Source File: MD5Utils.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
public static Quaternionf calculateQuaternion(Vector3f vec) {
    Quaternionf orientation = new Quaternionf(vec.x, vec.y, vec.z, 0);
    float temp = 1.0f - (orientation.x * orientation.x) - (orientation.y * orientation.y) - (orientation.z * orientation.z);
    if (temp < 0.0f) {
        orientation.w = 0.0f;
    } else {
        orientation.w = -(float) (Math.sqrt(temp));
    }
    return orientation;
}
 
Example #11
Source File: MD5Utils.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
public static Quaternionf calculateQuaternion(float x, float y, float z) {
    Quaternionf orientation = new Quaternionf(x, y, z, 0);
    float temp = 1.0f - (orientation.x * orientation.x) - (orientation.y * orientation.y) - (orientation.z * orientation.z);
    if (temp < 0.0f) {
        orientation.w = 0.0f;
    } else {
        orientation.w = -(float) (Math.sqrt(temp));
    }
    return orientation;
}
 
Example #12
Source File: Transformation.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
public Matrix4f buildModelMatrix(GameItem gameItem) {
    Quaternionf rotation = gameItem.getRotation();
    return modelMatrix.translationRotateScale(
            gameItem.getPosition().x, gameItem.getPosition().y, gameItem.getPosition().z,
            rotation.x, rotation.y, rotation.z, rotation.w,
            gameItem.getScale(), gameItem.getScale(), gameItem.getScale());
}
 
Example #13
Source File: GameItem.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
public GameItem() {
    selected = false;
    position = new Vector3f();
    scale = 1;
    rotation = new Quaternionf();
    textPos = 0;
    insideFrustum = true;
    disableFrustumCulling = false;
}
 
Example #14
Source File: Transform3DTest.java    From WraithEngine with Apache License 2.0 5 votes vote down vote up
@Test
public void default_rotation()
{
    Transform3D t = new Transform3D();

    Quaternionf rot = t.getRotation();
    Assert.assertEquals(0f, rot.x, 0f);
    Assert.assertEquals(0f, rot.y, 0f);
    Assert.assertEquals(0f, rot.z, 0f);
    Assert.assertEquals(1f, rot.w, 0f);
}
 
Example #15
Source File: MD5Utils.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
public static Quaternionf calculateQuaternion(float x, float y, float z) {
    Quaternionf orientation = new Quaternionf(x, y, z, 0);
    float temp = 1.0f - (orientation.x * orientation.x) - (orientation.y * orientation.y) - (orientation.z * orientation.z);
    if (temp < 0.0f) {
        orientation.w = 0.0f;
    } else {
        orientation.w = -(float) (Math.sqrt(temp));
    }
    return orientation;
}
 
Example #16
Source File: CameraTranslateControl.java    From sciview with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override public void drag( int x, int y ) {

        if(!sciView.getCamera().getLock().tryLock()) {
            return;
        }

        Vector3f translationVector = new Vector3f((x - lastX) * getDragSpeed(), (y - lastY) * getDragSpeed(), 0);

        ( new Quaternionf( sciView.getCamera().getRotation() ) ).conjugate().transform( translationVector );
        translationVector.y *= -1;
        sciView.getCamera().setPosition( sciView.getCamera().getPosition().add( new Vector3f( translationVector.x(), translationVector.y(), translationVector.z() ) ) );

        sciView.getCamera().getLock().unlock();
    }
 
Example #17
Source File: MD5Utils.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
public static Quaternionf calculateQuaternion(float x, float y, float z) {
    Quaternionf orientation = new Quaternionf(x, y, z, 0);
    float temp = 1.0f - (orientation.x * orientation.x) - (orientation.y * orientation.y) - (orientation.z * orientation.z);
    if (temp < 0.0f) {
        orientation.w = 0.0f;
    } else {
        orientation.w = -(float) (Math.sqrt(temp));
    }
    return orientation;
}
 
Example #18
Source File: Transformation.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
public Matrix4f buildModelMatrix(GameItem gameItem) {
    Quaternionf rotation = gameItem.getRotation();
    return modelMatrix.translationRotateScale(
            gameItem.getPosition().x, gameItem.getPosition().y, gameItem.getPosition().z,
            rotation.x, rotation.y, rotation.z, rotation.w,
            gameItem.getScale(), gameItem.getScale(), gameItem.getScale());
}
 
Example #19
Source File: Transform3DTest.java    From WraithEngine with Apache License 2.0 5 votes vote down vote up
@Test
public void setRotation_FromQuaternion()
{
    Transform3D t = new Transform3D();
    t.setRotation(new Quaternionf(1, 2, 3, 4));

    Quaternionf rot = t.getRotation();
    Assert.assertEquals(1f, rot.x, 0f);
    Assert.assertEquals(2f, rot.y, 0f);
    Assert.assertEquals(3f, rot.z, 0f);
    Assert.assertEquals(4f, rot.w, 0f);
}
 
Example #20
Source File: MD5Utils.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
public static Quaternionf calculateQuaternion(float x, float y, float z) {
    Quaternionf orientation = new Quaternionf(x, y, z, 0);
    float temp = 1.0f - (orientation.x * orientation.x) - (orientation.y * orientation.y) - (orientation.z * orientation.z);
    if (temp < 0.0f) {
        orientation.w = 0.0f;
    } else {
        orientation.w = -(float) (Math.sqrt(temp));
    }
    return orientation;
}
 
Example #21
Source File: Rotate3fTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Test
public void testMutate() {
	final Vector3f xAxis = new Vector3f(1, 0, 0);
	final Vector3f in = new Vector3f(xAxis);
	final Quaternionf q = new Quaternionf(new AxisAngle4f((float) (Math.PI /
		2.0), 0, 0, 1));
	final Vector3f expected = xAxis.rotate(q);

	final Vector3f result = ops.linalg().rotate1(in, q);

	assertSame("Mutate should operate on the input object", in, result);
	assertEquals("Rotation is incorrect", expected, result);
}
 
Example #22
Source File: Transformation.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
public Matrix4f buildModelMatrix(GameItem gameItem) {
    Quaternionf rotation = gameItem.getRotation();
    return modelMatrix.translationRotateScale(
            gameItem.getPosition().x, gameItem.getPosition().y, gameItem.getPosition().z,
            rotation.x, rotation.y, rotation.z, rotation.w,
            gameItem.getScale(), gameItem.getScale(), gameItem.getScale());
}
 
Example #23
Source File: MD5Utils.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
public static Quaternionf calculateQuaternion(float x, float y, float z) {
    Quaternionf orientation = new Quaternionf(x, y, z, 0);
    float temp = 1.0f - (orientation.x * orientation.x) - (orientation.y * orientation.y) - (orientation.z * orientation.z);
    if (temp < 0.0f) {
        orientation.w = 0.0f;
    } else {
        orientation.w = -(float) (Math.sqrt(temp));
    }
    return orientation;
}
 
Example #24
Source File: Transformation.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
public Matrix4f buildModelMatrix(GameItem gameItem) {
    Quaternionf rotation = gameItem.getRotation();
    return modelMatrix.translationRotateScale(
            gameItem.getPosition().x, gameItem.getPosition().y, gameItem.getPosition().z,
            rotation.x, rotation.y, rotation.z, rotation.w,
            gameItem.getScale(), gameItem.getScale(), gameItem.getScale());
}
 
Example #25
Source File: GameItem.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
public GameItem() {
    selected = false;
    position = new Vector3f();
    scale = 1;
    rotation = new Quaternionf();
    textPos = 0;
    insideFrustum = true;
}
 
Example #26
Source File: MD5Utils.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
public static Quaternionf calculateQuaternion(float x, float y, float z) {
    Quaternionf orientation = new Quaternionf(x, y, z, 0);
    float temp = 1.0f - (orientation.x * orientation.x) - (orientation.y * orientation.y) - (orientation.z * orientation.z);
    if (temp < 0.0f) {
        orientation.w = 0.0f;
    } else {
        orientation.w = -(float) (Math.sqrt(temp));
    }
    return orientation;
}
 
Example #27
Source File: MD5Utils.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
public static Quaternionf calculateQuaternion(float x, float y, float z) {
    Quaternionf orientation = new Quaternionf(x, y, z, 0);
    float temp = 1.0f - (orientation.x * orientation.x) - (orientation.y * orientation.y) - (orientation.z * orientation.z);
    if (temp < 0.0f) {
        orientation.w = 0.0f;
    } else {
        orientation.w = -(float) (Math.sqrt(temp));
    }
    return orientation;
}
 
Example #28
Source File: GameItem.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
public GameItem() {
    selected = false;
    position = new Vector3f();
    scale = 1;
    rotation = new Quaternionf();
    textPos = 0;
}
 
Example #29
Source File: MD5Utils.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
public static Quaternionf calculateQuaternion(float x, float y, float z) {
    Quaternionf orientation = new Quaternionf(x, y, z, 0);
    float temp = 1.0f - (orientation.x * orientation.x) - (orientation.y * orientation.y) - (orientation.z * orientation.z);
    if (temp < 0.0f) {
        orientation.w = 0.0f;
    } else {
        orientation.w = -(float) (Math.sqrt(temp));
    }
    return orientation;
}
 
Example #30
Source File: Transformation.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
public Matrix4f buildModelMatrix(GameItem gameItem) {
    Quaternionf rotation = gameItem.getRotation();
    return modelMatrix.translationRotateScale(
            gameItem.getPosition().x, gameItem.getPosition().y, gameItem.getPosition().z,
            rotation.x, rotation.y, rotation.z, rotation.w,
            gameItem.getScale(), gameItem.getScale(), gameItem.getScale());
}