Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into feature/gestalt-v7
Browse files Browse the repository at this point in the history
# Conflicts:
#	engine-tests/src/test/java/org/terasology/logic/location/LocationComponentTest.java
#	engine/src/main/java/org/terasology/logic/location/LocationComponent.java
#	engine/src/main/java/org/terasology/math/Direction.java
  • Loading branch information
DarkWeird committed Sep 16, 2020
2 parents 66deb05 + 38fba56 commit f987e3b
Show file tree
Hide file tree
Showing 4 changed files with 290 additions and 101 deletions.
49 changes: 26 additions & 23 deletions engine-tests/src/main/java/org/terasology/testUtil/TeraAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -59,9 +61,9 @@ public static void assertEquals(Vector3f expected, Vector3f actual, float error)
} else {
assertNotNull(actual);
Supplier<String> 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);
}
}

Expand All @@ -71,8 +73,8 @@ public static void assertEquals(Vector2f expected, Vector2f actual, float error)
} else {
assertNotNull(actual);
Supplier<String> 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);
}
}

Expand All @@ -82,8 +84,8 @@ public static void assertEquals(Vector2fc expected, Vector2fc actual, float erro
} else {
assertNotNull(actual);
Supplier<String> 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);
}
}

Expand All @@ -94,9 +96,9 @@ public static void assertEquals(Vector3fc expected, Vector3fc actual, float erro
} else {
assertNotNull(actual);
Supplier<String> 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);
}
}

Expand All @@ -106,10 +108,10 @@ public static void assertEquals(Vector4f expected, Vector4f actual, float error)
} else {
assertNotNull(actual);
Supplier<String> 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);
}
}

Expand All @@ -119,23 +121,24 @@ public static void assertEquals(Vector4fc expected, Vector4fc actual, float erro
} else {
assertNotNull(actual);
Supplier<String> 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<String> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
// SPDX-License-Identifier: Apache-2.0
package org.terasology.engine.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.engine.entitySystem.entity.EntityRef;
import org.terasology.engine.entitySystem.entity.lifecycleEvents.BeforeRemoveComponent;
import org.terasology.engine.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;
Expand All @@ -26,19 +27,19 @@ 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
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) {
Expand All @@ -52,35 +53,35 @@ 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
public void testOffsetParentAddsToWorldLocation() {
LocationComponent parent = giveParent();
loc.setLocalPosition(pos1);
parent.setLocalPosition(pos2);
assertEquals(pos1plus2, loc.getWorldPosition());
assertEquals(pos1plus2, loc.getWorldPosition(new Vector3f()));
}

@Test
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
Expand All @@ -89,7 +90,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
Expand All @@ -100,21 +101,21 @@ 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
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
Expand All @@ -134,32 +135,32 @@ public void testWorldScaleStacksWithParent() {
@Test
public void testSetWorldPositionWorksWithNoParent() {
loc.setWorldPosition(pos1);
assertEquals(pos1, loc.getWorldPosition());
TeraAssert.assertEquals(pos1, loc.getWorldPosition(new Vector3f()), 0.0001f);
}

@Test
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
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
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
Expand All @@ -176,14 +177,14 @@ 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);
third.getWorldPosition(new Vector3f()), 0.000001f);

}

Expand All @@ -194,7 +195,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
Expand All @@ -215,16 +216,16 @@ 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
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
Expand All @@ -236,7 +237,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
Expand All @@ -249,7 +250,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
Expand All @@ -265,7 +266,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);
}


Expand Down
Loading

0 comments on commit f987e3b

Please sign in to comment.