From 38fba56fe0a2502897e17249b22f6df17cff6827 Mon Sep 17 00:00:00 2001 From: Michael Pollind Date: Mon, 14 Sep 2020 18:46:24 -0700 Subject: [PATCH] feat(JOML): partial migrate of LocationComponent (#4051) * feat(JOML) partial migration of LocationComponent * add replacement locationComponent * updated test cases to LocationComponent * update location component * mapped older methods to replacment methods * update docs for LocationComponent * add missing @Deprecated * clean up code * clean up terassert --- .../org/terasology/testUtil/TeraAssert.java | 49 ++--- .../logic/location/LocationComponentTest.java | 65 +++--- .../logic/location/LocationComponent.java | 190 ++++++++++++++++-- .../java/org/terasology/math/Direction.java | 31 +++ 4 files changed, 262 insertions(+), 73 deletions(-) diff --git a/engine-tests/src/main/java/org/terasology/testUtil/TeraAssert.java b/engine-tests/src/main/java/org/terasology/testUtil/TeraAssert.java index b9e0c9adb20..18e1a52c0d0 100644 --- a/engine-tests/src/main/java/org/terasology/testUtil/TeraAssert.java +++ b/engine-tests/src/main/java/org/terasology/testUtil/TeraAssert.java @@ -16,9 +16,11 @@ package org.terasology.testUtil; import com.google.common.collect.Lists; +import org.joml.Quaternionfc; import org.joml.Vector2fc; import org.joml.Vector3fc; import org.joml.Vector4fc; +import org.junit.jupiter.api.Assertions; import org.terasology.math.geom.Quat4f; import org.terasology.math.geom.Vector2f; import org.terasology.math.geom.Vector3f; @@ -59,9 +61,9 @@ public static void assertEquals(Vector3f expected, Vector3f actual, float error) } else { assertNotNull(actual); Supplier errorMessageSupplier = () -> "Expected " + expected + ", actual" + actual; - org.junit.jupiter.api.Assertions.assertEquals(expected.x, actual.x, error, errorMessageSupplier); - org.junit.jupiter.api.Assertions.assertEquals(expected.y, actual.y, error, errorMessageSupplier); - org.junit.jupiter.api.Assertions.assertEquals(expected.z, actual.z, error, errorMessageSupplier); + Assertions.assertEquals(expected.x, actual.x, error, errorMessageSupplier); + Assertions.assertEquals(expected.y, actual.y, error, errorMessageSupplier); + Assertions.assertEquals(expected.z, actual.z, error, errorMessageSupplier); } } @@ -71,8 +73,8 @@ public static void assertEquals(Vector2f expected, Vector2f actual, float error) } else { assertNotNull(actual); Supplier errorMessageSupplier = () -> "Expected " + expected + ", actual" + actual; - org.junit.jupiter.api.Assertions.assertEquals(expected.x, actual.x, error, errorMessageSupplier); - org.junit.jupiter.api.Assertions.assertEquals(expected.y, actual.y, error, errorMessageSupplier); + Assertions.assertEquals(expected.x, actual.x, error, errorMessageSupplier); + Assertions.assertEquals(expected.y, actual.y, error, errorMessageSupplier); } } @@ -82,8 +84,8 @@ public static void assertEquals(Vector2fc expected, Vector2fc actual, float erro } else { assertNotNull(actual); Supplier errorMessageSupplier = () -> "Expected " + expected + ", actual" + actual; - org.junit.jupiter.api.Assertions.assertEquals(expected.x(), actual.x(), error, errorMessageSupplier); - org.junit.jupiter.api.Assertions.assertEquals(expected.y(), actual.y(), error, errorMessageSupplier); + Assertions.assertEquals(expected.x(), actual.x(), error, errorMessageSupplier); + Assertions.assertEquals(expected.y(), actual.y(), error, errorMessageSupplier); } } @@ -94,9 +96,9 @@ public static void assertEquals(Vector3fc expected, Vector3fc actual, float erro } else { assertNotNull(actual); Supplier errorMessageSupplier = () -> "Expected " + expected + ", actual" + actual; - org.junit.jupiter.api.Assertions.assertEquals(expected.x(), actual.x(), error, errorMessageSupplier); - org.junit.jupiter.api.Assertions.assertEquals(expected.y(), actual.y(), error, errorMessageSupplier); - org.junit.jupiter.api.Assertions.assertEquals(expected.z(), actual.z(), error, errorMessageSupplier); + Assertions.assertEquals(expected.x(), actual.x(), error, errorMessageSupplier); + Assertions.assertEquals(expected.y(), actual.y(), error, errorMessageSupplier); + Assertions.assertEquals(expected.z(), actual.z(), error, errorMessageSupplier); } } @@ -106,10 +108,10 @@ public static void assertEquals(Vector4f expected, Vector4f actual, float error) } else { assertNotNull(actual); Supplier errorMessageSupplier = () -> "Expected " + expected + ", actual" + actual; - org.junit.jupiter.api.Assertions.assertEquals(expected.x, actual.x, error, errorMessageSupplier); - org.junit.jupiter.api.Assertions.assertEquals(expected.y, actual.y, error, errorMessageSupplier); - org.junit.jupiter.api.Assertions.assertEquals(expected.z, actual.z, error, errorMessageSupplier); - org.junit.jupiter.api.Assertions.assertEquals(expected.w, actual.w, error, errorMessageSupplier); + Assertions.assertEquals(expected.x, actual.x, error, errorMessageSupplier); + Assertions.assertEquals(expected.y, actual.y, error, errorMessageSupplier); + Assertions.assertEquals(expected.z, actual.z, error, errorMessageSupplier); + Assertions.assertEquals(expected.w, actual.w, error, errorMessageSupplier); } } @@ -119,23 +121,24 @@ public static void assertEquals(Vector4fc expected, Vector4fc actual, float erro } else { assertNotNull(actual); Supplier errorMessageSupplier = () -> "Expected " + expected + ", actual" + actual; - org.junit.jupiter.api.Assertions.assertEquals(expected.x(), actual.x(), error, errorMessageSupplier); - org.junit.jupiter.api.Assertions.assertEquals(expected.y(), actual.y(), error, errorMessageSupplier); - org.junit.jupiter.api.Assertions.assertEquals(expected.z(), actual.z(), error, errorMessageSupplier); - org.junit.jupiter.api.Assertions.assertEquals(expected.w(), actual.w(), error, errorMessageSupplier); + Assertions.assertEquals(expected.x(), actual.x(), error, errorMessageSupplier); + Assertions.assertEquals(expected.y(), actual.y(), error, errorMessageSupplier); + Assertions.assertEquals(expected.z(), actual.z(), error, errorMessageSupplier); + Assertions.assertEquals(expected.w(), actual.w(), error, errorMessageSupplier); } } - public static void assertEquals(Quat4f expected, Quat4f actual, float error) { + + public static void assertEquals(Quaternionfc expected, Quaternionfc actual, float error) { if (expected == null) { assertNull(actual); } else { assertNotNull(actual); Supplier errorMessageSupplier = () -> "Expected " + expected + ", actual" + actual; - org.junit.jupiter.api.Assertions.assertEquals(expected.x, actual.x, error, errorMessageSupplier); - org.junit.jupiter.api.Assertions.assertEquals(expected.y, actual.y, error, errorMessageSupplier); - org.junit.jupiter.api.Assertions.assertEquals(expected.z, actual.z, error, errorMessageSupplier); - org.junit.jupiter.api.Assertions.assertEquals(expected.w, actual.w, error, errorMessageSupplier); + Assertions.assertEquals(expected.x(), actual.x(), error, errorMessageSupplier); + Assertions.assertEquals(expected.y(), actual.y(), error, errorMessageSupplier); + Assertions.assertEquals(expected.z(), actual.z(), error, errorMessageSupplier); + Assertions.assertEquals(expected.w(), actual.w(), error, errorMessageSupplier); } } } diff --git a/engine-tests/src/test/java/org/terasology/logic/location/LocationComponentTest.java b/engine-tests/src/test/java/org/terasology/logic/location/LocationComponentTest.java index ba1ed3300e6..1145c76d86b 100644 --- a/engine-tests/src/test/java/org/terasology/logic/location/LocationComponentTest.java +++ b/engine-tests/src/test/java/org/terasology/logic/location/LocationComponentTest.java @@ -15,14 +15,15 @@ */ package org.terasology.logic.location; +import org.joml.Quaternionf; +import org.joml.Vector3f; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.terasology.TerasologyTestingEnvironment; import org.terasology.entitySystem.entity.EntityRef; import org.terasology.entitySystem.entity.lifecycleEvents.BeforeRemoveComponent; +import org.terasology.math.JomlUtil; import org.terasology.math.TeraMath; -import org.terasology.math.geom.Quat4f; -import org.terasology.math.geom.Vector3f; import org.terasology.testUtil.TeraAssert; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -38,9 +39,9 @@ public class LocationComponentTest extends TerasologyTestingEnvironment { Vector3f pos1 = new Vector3f(1, 2, 3); Vector3f pos2 = new Vector3f(2, 3, 4); Vector3f pos1plus2 = new Vector3f(3, 5, 7); - Quat4f yawRotation; - Quat4f pitchRotation; - Quat4f yawPitch; + Quaternionf yawRotation; + Quaternionf pitchRotation; + Quaternionf yawPitch; long nextFakeEntityId = 1; @BeforeEach @@ -48,9 +49,9 @@ public void setup() { loc = new LocationComponent(); entity = createFakeEntityWith(loc); - yawRotation = new Quat4f(TeraMath.DEG_TO_RAD * 90, 0, 0); - pitchRotation = new Quat4f(0, TeraMath.DEG_TO_RAD * 45, 0); - yawPitch = new Quat4f(TeraMath.DEG_TO_RAD * 90, TeraMath.DEG_TO_RAD * 45, 0); + yawRotation = new Quaternionf().rotationYXZ(TeraMath.DEG_TO_RAD * 90, 0, 0); + pitchRotation = new Quaternionf().rotationYXZ(0, TeraMath.DEG_TO_RAD * 45, 0); + yawPitch = new Quaternionf().rotationYXZ(TeraMath.DEG_TO_RAD * 90, TeraMath.DEG_TO_RAD * 45, 0); } private EntityRef createFakeEntityWith(LocationComponent locationComponent) { @@ -64,19 +65,19 @@ private EntityRef createFakeEntityWith(LocationComponent locationComponent) { @Test public void testSetLocalPosition() { loc.setLocalPosition(pos1); - assertEquals(pos1, loc.getLocalPosition()); + assertEquals(pos1, JomlUtil.from(loc.getLocalPosition())); } @Test public void testSetLocalRotation() { loc.setLocalRotation(yawRotation); - assertEquals(yawRotation, loc.getLocalRotation()); + TeraAssert.assertEquals(yawRotation, JomlUtil.from(loc.getLocalRotation()), 0.0001f); } @Test public void testUnparentedWorldLocationSameAsLocal() { loc.setLocalPosition(pos1); - assertEquals(loc.getLocalPosition(), loc.getWorldPosition()); + assertEquals(JomlUtil.from(loc.getLocalPosition()), loc.getWorldPosition(new Vector3f())); } @Test @@ -84,7 +85,7 @@ public void testOffsetParentAddsToWorldLocation() { LocationComponent parent = giveParent(); loc.setLocalPosition(pos1); parent.setLocalPosition(pos2); - assertEquals(pos1plus2, loc.getWorldPosition()); + assertEquals(pos1plus2, loc.getWorldPosition(new Vector3f())); } @Test @@ -92,7 +93,7 @@ public void testParentRotatesWorldLocation() { LocationComponent parent = giveParent(); loc.setLocalPosition(pos1); parent.setLocalRotation(yawRotation); - TeraAssert.assertEquals(new Vector3f(pos1.z, pos1.y, -pos1.x), loc.getWorldPosition(), 0.00001f); + TeraAssert.assertEquals(new Vector3f(pos1.z, pos1.y, -pos1.x), loc.getWorldPosition(new Vector3f()), 0.00001f); } @Test @@ -101,7 +102,7 @@ public void testParentScalesWorldLocation() { loc.setLocalPosition(pos1); parent.setLocalScale(2.0f); - assertEquals(new Vector3f(2, 4, 6), loc.getWorldPosition()); + assertEquals(new Vector3f(2, 4, 6), loc.getWorldPosition(new Vector3f())); } @Test @@ -112,13 +113,13 @@ public void testScaleRotateAndOffsetCombineCorrectlyForWorldPosition() { parent.setLocalPosition(pos2); parent.setLocalRotation(yawRotation); - TeraAssert.assertEquals(new Vector3f(8, 7, 2), loc.getWorldPosition(), 0.00001f); + TeraAssert.assertEquals(new Vector3f(8, 7, 2), loc.getWorldPosition(new Vector3f()), 0.00001f); } @Test public void testWorldRotationSameAsLocalRotationWhenNoParent() { loc.setLocalRotation(yawRotation); - assertEquals(loc.getLocalRotation(), loc.getWorldRotation()); + assertEquals(JomlUtil.from(loc.getLocalRotation()), loc.getWorldRotation(new Quaternionf())); } @Test @@ -126,7 +127,7 @@ public void testWorldRotationCombinedWithParent() { LocationComponent parent = giveParent(); loc.setLocalRotation(pitchRotation); parent.setLocalRotation(yawRotation); - assertEquals(yawPitch, loc.getWorldRotation()); + TeraAssert.assertEquals(yawPitch, loc.getWorldRotation(new Quaternionf()), 0.0001f); } @Test @@ -146,7 +147,7 @@ public void testWorldScaleStacksWithParent() { @Test public void testSetWorldPositionWorksWithNoParent() { loc.setWorldPosition(pos1); - assertEquals(pos1, loc.getWorldPosition()); + TeraAssert.assertEquals(pos1, loc.getWorldPosition(new Vector3f()), 0.0001f); } @Test @@ -154,8 +155,8 @@ public void testSetWorldPositionWorksWithOffsetParent() { LocationComponent parent = giveParent(); parent.setLocalPosition(pos1); loc.setWorldPosition(pos1plus2); - assertEquals(pos2, loc.getLocalPosition()); - assertEquals(pos1plus2, loc.getWorldPosition()); + TeraAssert.assertEquals(pos2, JomlUtil.from(loc.getLocalPosition()), 0.0001f); + assertEquals(pos1plus2, loc.getWorldPosition(new Vector3f())); } @Test @@ -163,7 +164,7 @@ public void testSetWorldPositionWorksWithScaledParent() { LocationComponent parent = giveParent(); parent.setLocalScale(2.0f); loc.setWorldPosition(pos1); - assertEquals(pos1, loc.getWorldPosition()); + TeraAssert.assertEquals(pos1, loc.getWorldPosition(new Vector3f()), 0.0001f); } @Test @@ -171,7 +172,7 @@ public void testSetWorldPositionWorksWithRotatedParent() { LocationComponent parent = giveParent(); parent.setLocalRotation(yawRotation); loc.setWorldPosition(pos1); - TeraAssert.assertEquals(pos1, loc.getWorldPosition(), 0.000001f); + TeraAssert.assertEquals(pos1, loc.getWorldPosition(new Vector3f()), 0.000001f); } @Test @@ -188,13 +189,13 @@ public void testSetWorldPositionWorksWithNestedRotatedParent() { Location.attachChild(firstEntity, secondEntity); second.setLocalPosition(new Vector3f(1, 0, 0)); first.setLocalRotation(yawRotation); - TeraAssert.assertEquals(new Vector3f(0, 0, -1), second.getWorldPosition(), 0.000001f); + TeraAssert.assertEquals(new Vector3f(0, 0, -1), second.getWorldPosition(new Vector3f()), 0.000001f); Location.attachChild(secondEntity, thirdEntity); second.setLocalRotation(pitchRotation); third.setLocalPosition(new Vector3f(0, 0, 0)); - TeraAssert.assertEquals(new Vector3f(0, 0, -1), third.getWorldPosition(), 0.000001f); + TeraAssert.assertEquals(new Vector3f(0, 0, -1), third.getWorldPosition(new Vector3f()), 0.000001f); third.setLocalPosition(new Vector3f(0, 0, 1)); - TeraAssert.assertEquals(new Vector3f(0.5f * (float) Math.sqrt(2), -0.5f * (float) Math.sqrt(2), -1), third.getWorldPosition(), 0.000001f); + TeraAssert.assertEquals(new Vector3f(0.5f * (float) Math.sqrt(2), -0.5f * (float) Math.sqrt(2), -1), third.getWorldPosition(new Vector3f()), 0.000001f); } @@ -205,7 +206,7 @@ public void testSetWorldPositionWorksWithComplexParent() { parent.setLocalScale(2.0f); parent.setLocalPosition(pos2); loc.setWorldPosition(pos1); - TeraAssert.assertEquals(pos1, loc.getWorldPosition(), 0.000001f); + TeraAssert.assertEquals(pos1, loc.getWorldPosition(new Vector3f()), 0.000001f); } @Test @@ -226,8 +227,8 @@ public void testSetWorldScaleWorksWithScaledParent() { @Test public void testSetWorldRotationWorksWithNoParent() { loc.setWorldRotation(yawRotation); - assertEquals(yawRotation, loc.getWorldRotation()); - assertEquals(yawRotation, loc.getLocalRotation()); + TeraAssert.assertEquals(yawRotation, loc.getWorldRotation(new Quaternionf()), 0.0001f); + TeraAssert.assertEquals(yawRotation, JomlUtil.from(loc.getLocalRotation()), 0.0001f); } @Test @@ -235,7 +236,7 @@ public void testSetWorldRotationWithRotatedParent() { LocationComponent parent = giveParent(); parent.setLocalRotation(yawRotation); loc.setWorldRotation(yawPitch); - TeraAssert.assertEquals(yawPitch, loc.getWorldRotation(), 0.0000001f); + TeraAssert.assertEquals(yawPitch, loc.getWorldRotation(new Quaternionf()), 0.0001f); } @Test @@ -247,7 +248,7 @@ public void testPositionMaintainedWhenAttachedToParent() { loc.setWorldPosition(new Vector3f(2, 0, 0)); Location.attachChild(parentEntity, entity); - TeraAssert.assertEquals(new Vector3f(2, 0, 0), loc.getWorldPosition(), 0.000001f); + TeraAssert.assertEquals(new Vector3f(2, 0, 0), loc.getWorldPosition(new Vector3f()), 0.000001f); } @Test @@ -260,7 +261,7 @@ public void testPositionMaintainedWhenRemovedFromParent() { Location.attachChild(parentEntity, entity); Location.removeChild(parentEntity, entity); - TeraAssert.assertEquals(new Vector3f(2, 0, 0), loc.getWorldPosition(), 0.000001f); + TeraAssert.assertEquals(new Vector3f(2, 0, 0), loc.getWorldPosition(new Vector3f()), 0.000001f); } @Test @@ -276,7 +277,7 @@ public void testPositionMaintainedWhenParentDestroyed() { when(parentEntity.getComponent(LocationComponent.class)).thenReturn(null); when(parentEntity.exists()).thenReturn(false); - TeraAssert.assertEquals(new Vector3f(2, 0, 0), loc.getWorldPosition(), 0.000001f); + TeraAssert.assertEquals(new Vector3f(2, 0, 0), loc.getWorldPosition(new Vector3f()), 0.000001f); } diff --git a/engine/src/main/java/org/terasology/logic/location/LocationComponent.java b/engine/src/main/java/org/terasology/logic/location/LocationComponent.java index 8e292133e92..ec41ad08f34 100644 --- a/engine/src/main/java/org/terasology/logic/location/LocationComponent.java +++ b/engine/src/main/java/org/terasology/logic/location/LocationComponent.java @@ -16,9 +16,13 @@ package org.terasology.logic.location; import com.google.common.collect.Lists; +import org.joml.Quaternionf; +import org.joml.Quaternionfc; +import org.joml.Vector3fc; import org.terasology.entitySystem.Component; import org.terasology.entitySystem.entity.EntityRef; import org.terasology.math.Direction; +import org.terasology.math.JomlUtil; import org.terasology.math.geom.Quat4f; import org.terasology.math.geom.Vector3f; import org.terasology.network.Replicate; @@ -56,7 +60,7 @@ public final class LocationComponent implements Component, ReplicationCheck { @Replicate Vector3f lastPosition = new Vector3f(); @Replicate - Quat4f lastRotation = new Quat4f(0,0,0,1); + Quat4f lastRotation = new Quat4f(0, 0, 0, 1); public LocationComponent() { } @@ -65,78 +69,196 @@ public LocationComponent(Vector3f position) { setLocalPosition(position); } + /** + * @return local rotation of location component + * + * TODO: make this readonly Quaternionfc -- Michael Pollind + */ + public Quat4f getLocalRotation() { + return rotation; + } + + + /** + * @param newQuat + * @deprecated This is scheduled for removal in an upcoming version method will be replaced with JOML implementation + * {@link #setLocalRotation(Quaternionfc)}. + */ + @Deprecated + public void setLocalRotation(Quat4f newQuat) { + lastRotation.set(rotation); + rotation.set(newQuat); + } + + /** + * set the current local rotation of the component + * + * @param rot local rotation + */ + public void setLocalRotation(Quaternionfc rot) { + lastRotation.set(rotation); + rotation.set(JomlUtil.from(rot)); + } + + /** * @return The position of this component relative to any parent. Can be directly modified to update the component + * TODO: make this readonly Vector3fc -- Michael Pollind */ public Vector3f getLocalPosition() { return position; } - public void setLocalPosition(Vector3f newPos) { + /** + * @param pos + * @deprecated This is scheduled for removal in an upcoming version method will be replaced with JOML implementation + * {@link #setLocalPosition(Vector3fc)}. + */ + @Deprecated + public void setLocalPosition(Vector3f pos) { + lastPosition.set(position); + position.set(pos); + } + + + /** + * the local position of this location component + * + * @param pos position to set + */ + public void setLocalPosition(Vector3fc pos) { lastPosition.set(position); - position.set(newPos); + position.set(JomlUtil.from(pos)); } + /** + * @return + * @deprecated This is scheduled for removal in an upcoming version method will be replaced with JOML implementation + * {@link #getLocalDirection(org.joml.Vector3f)}. + */ + @Deprecated public Vector3f getLocalDirection() { Vector3f result = Direction.FORWARD.getVector3f(); getLocalRotation().rotate(result, result); return result; } - public Quat4f getLocalRotation() { - return rotation; + /** + * gets the local direction of the given entity in + * + * @param dest will hold the result + * @return dest + */ + public org.joml.Vector3f getLocalDirection(org.joml.Vector3f dest) { + return dest.set(Direction.FORWARD.asVector3i()).rotate(JomlUtil.from(getLocalRotation())); } - public void setLocalRotation(Quat4f newQuat) { - lastRotation.set(rotation); - rotation.set(newQuat); - } + /** + * set the local scale + * @param value the scale + */ public void setLocalScale(float value) { this.scale = value; } + /** + * local scale + * @return the scale + */ public float getLocalScale() { return scale; } /** * @return A new vector containing the world location. + * @deprecated This is scheduled for removal in an upcoming version method will be replaced with JOML implementation + * {@link #getWorldPosition(org.joml.Vector3f)}. */ + @Deprecated public Vector3f getWorldPosition() { return getWorldPosition(new Vector3f()); } + /** + * @param output + * @return + * @deprecated This is scheduled for removal in an upcoming version method will be replaced with JOML implementation + * {@link #getWorldPosition(org.joml.Vector3f)}. + */ + @Deprecated public Vector3f getWorldPosition(Vector3f output) { - output.set(position); + output.set(JomlUtil.from(getWorldPosition(new org.joml.Vector3f()))); + return output; + } + + /** + * get the world position + * + * @param dest will hold the result + * @return dest + */ + public org.joml.Vector3f getWorldPosition(org.joml.Vector3f dest) { + dest.set(JomlUtil.from(position)); LocationComponent parentLoc = parent.getComponent(LocationComponent.class); while (parentLoc != null) { - output.scale(parentLoc.scale); - parentLoc.getLocalRotation().rotate(output, output); - output.add(parentLoc.position); + dest.mul(parentLoc.scale); + dest.rotate(JomlUtil.from(parentLoc.getLocalRotation())); + dest.add(JomlUtil.from(parentLoc.position)); parentLoc = parentLoc.parent.getComponent(LocationComponent.class); } - return output; + return dest; } + /** + * @deprecated This is scheduled for removal in an upcoming version method will be replaced with JOML implementation + * {@link #getWorldDirection(org.joml.Vector3f)}. + */ + @Deprecated public Vector3f getWorldDirection() { Vector3f result = Direction.FORWARD.getVector3f(); getWorldRotation().rotate(result, result); return result; } + + public org.joml.Vector3f getWorldDirection(org.joml.Vector3f dest) { + return dest.set(Direction.FORWARD.asVector3f()).rotate(JomlUtil.from(getWorldRotation())); + } + + /** + * @deprecated This is scheduled for removal in an upcoming version method will be replaced with JOML implementation + * {@link #getWorldRotation(Quaternionf)}. + */ + @Deprecated public Quat4f getWorldRotation() { return getWorldRotation(new Quat4f(0, 0, 0, 1)); } + /** + * @deprecated This is scheduled for removal in an upcoming version method will be replaced with JOML implementation + * {@link #getWorldRotation(Quaternionf)}. + */ + @Deprecated public Quat4f getWorldRotation(Quat4f output) { - output.set(rotation); + output.set(JomlUtil.from(getWorldRotation(new Quaternionf()))); + return output; + } + + /** + * get the current world rotation of the location component + * + * @param dest will hold the result + * @return dest + */ + public Quaternionf getWorldRotation(Quaternionf dest) { + dest.set(JomlUtil.from(rotation)); LocationComponent parentLoc = parent.getComponent(LocationComponent.class); while (parentLoc != null) { - output.mul(parentLoc.rotation, output); + dest.premul(JomlUtil.from(parentLoc.rotation)); parentLoc = parentLoc.parent.getComponent(LocationComponent.class); } - return output; + return dest; } public float getWorldScale() { @@ -149,8 +271,23 @@ public float getWorldScale() { return result; } + /** + * @param value + * @deprecated This is scheduled for removal in an upcoming version method will be replaced with JOML implementation + * {@link #setWorldPosition(Vector3fc)}. + */ + @Deprecated public void setWorldPosition(Vector3f value) { - setLocalPosition(value); + this.setWorldPosition(JomlUtil.from(value)); + } + + /** + * set the world position of the {@link LocationComponent} + * + * @param pos position to set + */ + public void setWorldPosition(Vector3fc pos) { + setLocalPosition(pos); LocationComponent parentLoc = parent.getComponent(LocationComponent.class); if (parentLoc != null) { this.position.sub(parentLoc.getWorldPosition()); @@ -161,7 +298,24 @@ public void setWorldPosition(Vector3f value) { } } + /** + * set the world rotation of the {@link LocationComponent} + * + * @param value + * @deprecated This is scheduled for removal in an upcoming version method will be replaced with JOML implementation + * {@link #setWorldRotation(Quaternionfc)}. + */ + @Deprecated public void setWorldRotation(Quat4f value) { + this.setWorldRotation(JomlUtil.from(value)); + } + + /** + * set the world rotation of the {@link LocationComponent} + * + * @param value position to set + */ + public void setWorldRotation(Quaternionfc value) { setLocalRotation(value); LocationComponent parentLoc = parent.getComponent(LocationComponent.class); if (parentLoc != null) { diff --git a/engine/src/main/java/org/terasology/math/Direction.java b/engine/src/main/java/org/terasology/math/Direction.java index ea11ba3ee27..19b359725cb 100644 --- a/engine/src/main/java/org/terasology/math/Direction.java +++ b/engine/src/main/java/org/terasology/math/Direction.java @@ -17,6 +17,10 @@ package org.terasology.math; import com.google.common.collect.Maps; +import org.joml.Quaternionfc; +import org.joml.Vector3fc; +import org.joml.Vector3ic; +import org.terasology.entitySystem.entity.EntityRef; import org.terasology.math.geom.Vector3f; import org.terasology.math.geom.Vector3i; @@ -114,13 +118,40 @@ public static Direction inHorizontalDirection(float x, float z) { return (z > 0) ? FORWARD : BACKWARD; } + + /** + * readonly normalized {@link Vector3ic} in the given {@link Direction} + * + * @return vector pointing in the direction + */ + public Vector3fc asVector3i() { + return JomlUtil.from(vector3fDir); + } + + + /** + * readonly normalized {@link Vector3fc} in the given {@link Direction} + * + * @return vector pointing in the direction + */ + public Vector3ic asVector3f() { + return JomlUtil.from(vector3iDir); + } + /** * @return The vector3i in the direction of the side. Do not modify. + * @deprecated This is scheduled for removal in an upcoming version method will be replaced with JOML implementation + * {@link #asVector3i()}. */ public Vector3i getVector3i() { return new Vector3i(vector3iDir); } + /** + * @return + * @deprecated This is scheduled for removal in an upcoming version method will be replaced with JOML implementation + * {@link #asVector3f()} + */ public Vector3f getVector3f() { return new Vector3f(vector3fDir); }