diff --git a/core/src/main/java/com/github/introfog/pie/core/collisions/broadphase/AbstractBroadPhase.java b/core/src/main/java/com/github/introfog/pie/core/collisions/broadphase/AbstractBroadPhase.java index 9f7ea53..b3f1ab0 100644 --- a/core/src/main/java/com/github/introfog/pie/core/collisions/broadphase/AbstractBroadPhase.java +++ b/core/src/main/java/com/github/introfog/pie/core/collisions/broadphase/AbstractBroadPhase.java @@ -78,7 +78,7 @@ public Set getUnmodifiableShapes() { *

* Note, before calling the {@link #domesticCalculateAabbCollisions()} method, which calculates collisions, * the {@link IShape#computeAabb()} method is called for all shapes from the {@link #shapes}, because the - * broad phase needs the up-to-date Aabbs. + * broad phase needs the up-to-date aabbs. * * @return the {@link ShapePair} set in which each item represents * a unique shape pair and the Aabb of those shapes intersect @@ -93,7 +93,7 @@ public final Set calculateAabbCollisions() { * Domestic method for calculating the shape Aabb collisions. * *

- * Note, when this method is called, all shapes from {@link #shapes} have an up-to-date Aabb. + * Note, when this method is called, all shapes from {@link #shapes} have an up-to-date aabb. * * @return the {@link ShapePair} set in which each item represents * a unique shape pair and the Aabb of those shapes intersect diff --git a/core/src/main/java/com/github/introfog/pie/core/collisions/narrowphase/impl/CirclePolygonCollisionHandler.java b/core/src/main/java/com/github/introfog/pie/core/collisions/narrowphase/impl/CirclePolygonCollisionHandler.java index 31c1bd2..cfa078b 100644 --- a/core/src/main/java/com/github/introfog/pie/core/collisions/narrowphase/impl/CirclePolygonCollisionHandler.java +++ b/core/src/main/java/com/github/introfog/pie/core/collisions/narrowphase/impl/CirclePolygonCollisionHandler.java @@ -68,7 +68,7 @@ public Manifold handleCollision(IShape aShape, IShape bShape, Context context) { Vector2f projection = new Vector2f(); Vector2f realProjection = new Vector2f(); Vector2f tmpV = new Vector2f(); - for (int i = 0; i < polygonB.getVertexCount(); i++) { + for (int i = 0; i < polygonB.getVertices().length; i++) { tmpV.set(centerA); tmpV.sub(polygonB.getVertices()[i]); dotProduct = Vector2f.dotProduct(polygonB.getNormals()[i], tmpV); @@ -113,7 +113,7 @@ public Manifold handleCollision(IShape aShape, IShape bShape, Context context) { // Found the nearest edge to the center of the circle, and the center of the circle lies outside the polygon. // Now define the Voronoi region in which the center of the circle is located relative to the nearest edge of the polygon Vector2f v1 = new Vector2f(polygonB.getVertices()[indexFaceNormal]); - Vector2f v2 = new Vector2f(polygonB.getVertices()[(indexFaceNormal + 1) % polygonB.getVertexCount()]); + Vector2f v2 = new Vector2f(polygonB.getVertices()[(indexFaceNormal + 1) % polygonB.getVertices().length]); float dot1 = Vector2f.dotProduct(Vector2f.sub(centerA, v1), Vector2f.sub(v2, v1)); float dot2 = Vector2f.dotProduct(Vector2f.sub(centerA, v2), Vector2f.sub(v1, v2)); diff --git a/core/src/main/java/com/github/introfog/pie/core/collisions/narrowphase/impl/PolygonPolygonCollisionHandler.java b/core/src/main/java/com/github/introfog/pie/core/collisions/narrowphase/impl/PolygonPolygonCollisionHandler.java index b450114..06a406c 100644 --- a/core/src/main/java/com/github/introfog/pie/core/collisions/narrowphase/impl/PolygonPolygonCollisionHandler.java +++ b/core/src/main/java/com/github/introfog/pie/core/collisions/narrowphase/impl/PolygonPolygonCollisionHandler.java @@ -93,7 +93,7 @@ public Manifold handleCollision(IShape aShape, IShape bShape, Context context) { // Setup reference face vertices Vector2f v1 = new Vector2f(refPoly.getVertices()[referenceIndex]); - referenceIndex = referenceIndex + 1 == refPoly.getVertexCount() ? 0 : referenceIndex + 1; + referenceIndex = referenceIndex + 1 == refPoly.getVertices().length ? 0 : referenceIndex + 1; Vector2f v2 = new Vector2f(refPoly.getVertices()[referenceIndex]); // Transform vectors to world coordinates @@ -189,7 +189,7 @@ private void findIncidentFace(Vector2f[] v, Polygon refPoly, Polygon incPoly, in // Find most anti-normal face on incident polygon int incidentFace = 0; float minDot = Float.MAX_VALUE; - for (int i = 0; i < incPoly.getVertexCount(); ++i) { + for (int i = 0; i < incPoly.getVertices().length; ++i) { // real dot = Dot( referenceNormal, IncPoly->m_normals[i] ); float dotProduct = Vector2f.dotProduct(referenceNormal, incPoly.getNormals()[i]); @@ -208,7 +208,7 @@ private void findIncidentFace(Vector2f[] v, Polygon refPoly, Polygon incPoly, in // IncPoly->body->position; incPoly.getRotateMatrix().mul(incPoly.getVertices()[incidentFace], v[0]); v[0].add(incPoly.getBody().position); - incidentFace = incidentFace + 1 >= incPoly.getVertexCount() ? 0 : incidentFace + 1; + incidentFace = incidentFace + 1 >= incPoly.getVertices().length ? 0 : incidentFace + 1; incPoly.getRotateMatrix().mul(incPoly.getVertices()[incidentFace], v[1]); v[1].add(incPoly.getBody().position); } @@ -261,7 +261,7 @@ private static float findAxisLeastPenetration(int[] faceIndex, Polygon polygonA, float bestDistance = -Float.MAX_VALUE; int bestIndex = 0; - for (int i = 0; i < polygonA.getVertexCount(); ++i) { + for (int i = 0; i < polygonA.getVertices().length; ++i) { // Retrieve a face normal from A // Vec2 n = A->m_normals[i]; // Vec2 nw = A->u * n; diff --git a/core/src/main/java/com/github/introfog/pie/core/math/RotationMatrix2x2.java b/core/src/main/java/com/github/introfog/pie/core/math/RotationMatrix2x2.java index 9b1bd9c..2f5378c 100644 --- a/core/src/main/java/com/github/introfog/pie/core/math/RotationMatrix2x2.java +++ b/core/src/main/java/com/github/introfog/pie/core/math/RotationMatrix2x2.java @@ -17,12 +17,16 @@ import java.util.StringJoiner; -public class RotationMatrix2x2 { +public final class RotationMatrix2x2 { public float m00; public float m01; public float m10; public float m11; + public RotationMatrix2x2() { + setAngle(0); + } + public void setAngle(float radian) { float cos = (float) Math.cos(radian); float sin = (float) Math.sin(radian); diff --git a/core/src/main/java/com/github/introfog/pie/core/shape/Body.java b/core/src/main/java/com/github/introfog/pie/core/shape/Body.java index 5351672..76b7f56 100644 --- a/core/src/main/java/com/github/introfog/pie/core/shape/Body.java +++ b/core/src/main/java/com/github/introfog/pie/core/shape/Body.java @@ -25,17 +25,17 @@ */ public class Body { /** The density. */ - public float density; + public final float density; /** The restitution. */ - public float restitution; + public final float restitution; /** The inverted mass. */ public float invertedMass; /** The static friction. */ - public float staticFriction; + public final float staticFriction; /** The dynamic friction. */ - public float dynamicFriction; + public final float dynamicFriction; /** The torque. */ - public float torque; + public final float torque; /** The body orientation in radians. */ public float orientation; /** The angular velocity. */ @@ -43,11 +43,11 @@ public class Body { /** The inverted inertia. */ public float invertedInertia; /** The position. */ - public Vector2f position; + public final Vector2f position; /** The force. */ - public Vector2f force; + public final Vector2f force; /** The velocity. */ - public Vector2f velocity; + public final Vector2f velocity; /** * Instantiates a new {@link Body} instance. diff --git a/core/src/main/java/com/github/introfog/pie/core/shape/Circle.java b/core/src/main/java/com/github/introfog/pie/core/shape/Circle.java index 74ed463..c82034f 100644 --- a/core/src/main/java/com/github/introfog/pie/core/shape/Circle.java +++ b/core/src/main/java/com/github/introfog/pie/core/shape/Circle.java @@ -22,7 +22,7 @@ */ public class Circle extends IShape { /** The radius of circle. */ - protected final float radius; + private final float radius; /** * Instantiates a new {@link Circle} instance based on radius, coordinates of center, density and restitution. @@ -50,30 +50,30 @@ public Circle(float radius, float centreX, float centreY, float density, float r * * @return the radius */ - public float getRadius() { + public final float getRadius() { return radius; } @Override public void computeAabb() { - aabb.min.set(body.position.x - radius, body.position.y - radius); - aabb.max.set(body.position.x + radius, body.position.y + radius); + getAabb().min.set(getBody().position.x - radius, getBody().position.y - radius); + getAabb().max.set(getBody().position.x + radius, getBody().position.y + radius); } @Override public String toString() { return new StringJoiner("; ", "{", "}") - .add("center=" + body.position) + .add("center=" + getBody().position) .add("radius=" + radius) .toString(); } @Override protected void computeMassAndInertia() { - float mass = (float) Math.PI * radius * radius * body.density; - body.invertedMass = (mass == 0f) ? 0f : 1f / mass; + float mass = (float) Math.PI * radius * radius * getBody().density; + getBody().invertedMass = (mass == 0f) ? 0f : 1f / mass; - float inertia = radius * radius / (body.invertedMass == 0 ? 1 : body.invertedMass); - body.invertedInertia = (inertia != 0.0f) ? 1.0f / inertia : 0.0f; + float inertia = radius * radius / (getBody().invertedMass == 0 ? 1 : getBody().invertedMass); + getBody().invertedInertia = (inertia != 0.0f) ? 1.0f / inertia : 0.0f; } } diff --git a/core/src/main/java/com/github/introfog/pie/core/shape/IShape.java b/core/src/main/java/com/github/introfog/pie/core/shape/IShape.java index a8f2b30..09fff25 100644 --- a/core/src/main/java/com/github/introfog/pie/core/shape/IShape.java +++ b/core/src/main/java/com/github/introfog/pie/core/shape/IShape.java @@ -27,14 +27,13 @@ public abstract class IShape { private static final AtomicInteger lastShapeId = new AtomicInteger(Integer.MIN_VALUE); + private final int shapeId; /** The shape axis aligned bounding box. */ - protected final Aabb aabb; + private final Aabb aabb; /** The body that stores shape physical parameters. */ - protected final Body body; + private final Body body; /** The rotation matrix. */ - protected final RotationMatrix2x2 rotateMatrix; - - private final int shapeId; + private final RotationMatrix2x2 rotateMatrix; /** * Instantiates a new {@link IShape} instance. @@ -44,7 +43,6 @@ public IShape(float centreX, float centreY, float density, float restitution) { aabb = new Aabb(); body = new Body(centreX, centreY, density, restitution); rotateMatrix = new RotationMatrix2x2(); - rotateMatrix.setAngle(0f); } /** @@ -73,7 +71,7 @@ public void applyImpulse(Vector2f impulse, Vector2f contactVector) { * * @return the axis aligned bounding box */ - public Aabb getAabb() { + public final Aabb getAabb() { return aabb; } @@ -82,7 +80,7 @@ public Aabb getAabb() { * * @return the body */ - public Body getBody() { + public final Body getBody() { return body; } @@ -91,7 +89,7 @@ public Body getBody() { * * @return the rotation matrix */ - public RotationMatrix2x2 getRotateMatrix() { + public final RotationMatrix2x2 getRotateMatrix() { return rotateMatrix; } @@ -114,7 +112,7 @@ public RotationMatrix2x2 getRotateMatrix() { * @return {@inheritDoc} */ @Override - public boolean equals(Object o) { + public final boolean equals(Object o) { return this == o; } @@ -125,7 +123,7 @@ public boolean equals(Object o) { * @return {@inheritDoc} */ @Override - public int hashCode() { + public final int hashCode() { return shapeId; } diff --git a/core/src/main/java/com/github/introfog/pie/core/shape/Polygon.java b/core/src/main/java/com/github/introfog/pie/core/shape/Polygon.java index 63715ab..7f35c07 100644 --- a/core/src/main/java/com/github/introfog/pie/core/shape/Polygon.java +++ b/core/src/main/java/com/github/introfog/pie/core/shape/Polygon.java @@ -28,12 +28,10 @@ * and array of normals (calculated when creating an object to improve performance). */ public class Polygon extends IShape { - /** The count of polygon vertices. */ - protected final int vertexCount; /** The array of polygon vertices. */ - protected final Vector2f[] vertices; + private final Vector2f[] vertices; /** The array of polygon normals. */ - protected final Vector2f[] normals; + private final Vector2f[] normals; /** * Instantiates a new {@link Polygon} instance based on density, @@ -62,7 +60,7 @@ public Polygon(float density, float restitution, float centreX, float centreY, L } List hull = Polygon.calculateHullIndices(vertices); - vertexCount = hull.size(); + final int vertexCount = hull.size(); if (vertexCount > MathPie.MAX_POLY_VERTEX_COUNT) { // TODO create Pie custom exception @@ -111,21 +109,12 @@ public static Polygon generateRectangle(float centerX, float centerY, float widt return new Polygon(density, restitution, centerX, centerY, vertices); } - /** - * Gets the count of polygon vertices. - * - * @return the count of vertices - */ - public int getVertexCount() { - return vertexCount; - } - /** * Gets the copy of array of polygon vertices. * * @return the array of vertices */ - public Vector2f[] getVertices() { + public final Vector2f[] getVertices() { return Arrays.copyOf(vertices, vertices.length); } @@ -134,44 +123,44 @@ public Vector2f[] getVertices() { * * @return the array of normals */ - public Vector2f[] getNormals() { + public final Vector2f[] getNormals() { return Arrays.copyOf(normals, normals.length); } @Override public void computeAabb() { - aabb.min.x = Float.MAX_VALUE; - aabb.min.y = Float.MAX_VALUE; + getAabb().min.x = Float.MAX_VALUE; + getAabb().min.y = Float.MAX_VALUE; - aabb.max.x = -Float.MAX_VALUE; - aabb.max.y = -Float.MAX_VALUE; + getAabb().max.x = -Float.MAX_VALUE; + getAabb().max.y = -Float.MAX_VALUE; Vector2f tmpV = new Vector2f(); - for (int i = 0; i < vertexCount; i++) { + for (int i = 0; i < vertices.length; i++) { tmpV.set(vertices[i]); - rotateMatrix.mul(tmpV, tmpV); - if (tmpV.x < aabb.min.x) { - aabb.min.x = tmpV.x; + getRotateMatrix().mul(tmpV, tmpV); + if (tmpV.x < getAabb().min.x) { + getAabb().min.x = tmpV.x; } - if (tmpV.y < aabb.min.y) { - aabb.min.y = tmpV.y; + if (tmpV.y < getAabb().min.y) { + getAabb().min.y = tmpV.y; } - if (tmpV.x > aabb.max.x) { - aabb.max.x = tmpV.x; + if (tmpV.x > getAabb().max.x) { + getAabb().max.x = tmpV.x; } - if (tmpV.y > aabb.max.y) { - aabb.max.y = tmpV.y; + if (tmpV.y > getAabb().max.y) { + getAabb().max.y = tmpV.y; } } - aabb.min.add(body.position); - aabb.max.add(body.position); + getAabb().min.add(getBody().position); + getAabb().max.add(getBody().position); } @Override public String toString() { return new StringJoiner("; ", "{", "}") - .add("center=" + body.position) + .add("center=" + getBody().position) .add("vertices=" + Arrays.toString(vertices)) .toString(); } @@ -183,12 +172,12 @@ public String toString() { * @return a new vector object that coincides in coordinates with the most * distant polygon vertex in the given direction */ - public Vector2f calculateSupportVertex(Vector2f direction) { + public final Vector2f calculateSupportVertex(Vector2f direction) { // Looking for the most distant vertex in a given direction float bestProjection = -Float.MAX_VALUE; final Vector2f bestVertex = new Vector2f(); - for (int i = 0; i < vertexCount; ++i) { + for (int i = 0; i < vertices.length; ++i) { Vector2f v = vertices[i]; float projection = Vector2f.dotProduct(v, direction); @@ -207,10 +196,10 @@ protected void computeMassAndInertia() { float I = 0f; final float k_inv3 = 1f / 3f; - for (int i = 0; i < vertexCount; ++i) { + for (int i = 0; i < vertices.length; ++i) { // Split the convex polygon into triangles for which one of the points (0, 0) Vector2f p1 = vertices[i]; - Vector2f p2 = vertices[(i + 1) % vertexCount]; + Vector2f p2 = vertices[(i + 1) % vertices.length]; float D = Vector2f.crossProduct(p1, p2); float triangleArea = 0.5f * D; @@ -222,10 +211,10 @@ protected void computeMassAndInertia() { I += (0.25f * k_inv3 * D) * (intX2 + intY2); } - float mass = body.density * area; - body.invertedMass = (mass != 0f) ? 1f / mass : 0f; - float inertia = I * body.density; - body.invertedInertia = (inertia != 0f) ? 1f / inertia : 0f; + float mass = getBody().density * area; + getBody().invertedMass = (mass != 0f) ? 1f / mass : 0f; + float inertia = I * getBody().density; + getBody().invertedInertia = (inertia != 0f) ? 1f / inertia : 0f; } private static List calculateHullIndices(List vertices) { diff --git a/core/src/main/java/com/github/introfog/pie/core/util/ShapeIOUtil.java b/core/src/main/java/com/github/introfog/pie/core/util/ShapeIOUtil.java index fe34378..69e6d5d 100644 --- a/core/src/main/java/com/github/introfog/pie/core/util/ShapeIOUtil.java +++ b/core/src/main/java/com/github/introfog/pie/core/util/ShapeIOUtil.java @@ -127,7 +127,7 @@ public static String convertShapeToString(IShape shape) { str.append(circle.getRadius()).append(";"); } else if (shape instanceof Polygon) { Polygon polygon = (Polygon) shape; - str.append(polygon.getVertexCount()).append(";"); + str.append(polygon.getVertices().length).append(";"); for (Vector2f vec : polygon.getVertices()) { str.append(vec.x).append(";").append(vec.y).append(";"); } diff --git a/core/src/test/java/com/github/introfog/pie/core/shape/CircleTest.java b/core/src/test/java/com/github/introfog/pie/core/shape/CircleTest.java index 17f4dfc..a2a2b4f 100644 --- a/core/src/test/java/com/github/introfog/pie/core/shape/CircleTest.java +++ b/core/src/test/java/com/github/introfog/pie/core/shape/CircleTest.java @@ -34,9 +34,9 @@ public void paramConstructorWithNegativeRadiusTest() { public void paramConstructorTest() { Circle circle = new Circle(10, 1, 3, 0.1f, 0.2f); - Assert.assertEquals(new Vector2f(1, 3), circle.body.position); - Assert.assertEquals(0.1f, circle.body.density, PieTest.FLOAT_EPSILON_COMPARISON); - Assert.assertEquals(0.2f, circle.body.restitution, PieTest.FLOAT_EPSILON_COMPARISON); + Assert.assertEquals(new Vector2f(1, 3), circle.getBody().position); + Assert.assertEquals(0.1f, circle.getBody().density, PieTest.FLOAT_EPSILON_COMPARISON); + Assert.assertEquals(0.2f, circle.getBody().restitution, PieTest.FLOAT_EPSILON_COMPARISON); } @Test @@ -80,16 +80,16 @@ public void equalsToAnotherClassTest() { public void computeMassAndInertiaTest() { Circle circle = new Circle(10, 0, 0, (float) (1 / Math.PI), 0); - Assert.assertEquals(1f / 100f, circle.body.invertedMass, PieTest.FLOAT_EPSILON_COMPARISON); - Assert.assertEquals(1f / 10000f, circle.body.invertedInertia, PieTest.FLOAT_EPSILON_COMPARISON); + Assert.assertEquals(1f / 100f, circle.getBody().invertedMass, PieTest.FLOAT_EPSILON_COMPARISON); + Assert.assertEquals(1f / 10000f, circle.getBody().invertedInertia, PieTest.FLOAT_EPSILON_COMPARISON); } @Test public void computeMassAndInertiaZeroCircleTest() { Circle circle = new Circle(0, 0, 0, (float) (1 / Math.PI), 0); - Assert.assertEquals(0, circle.body.invertedMass, PieTest.FLOAT_EPSILON_COMPARISON); - Assert.assertEquals(0, circle.body.invertedInertia, PieTest.FLOAT_EPSILON_COMPARISON); + Assert.assertEquals(0, circle.getBody().invertedMass, PieTest.FLOAT_EPSILON_COMPARISON); + Assert.assertEquals(0, circle.getBody().invertedInertia, PieTest.FLOAT_EPSILON_COMPARISON); } @Test diff --git a/core/src/test/java/com/github/introfog/pie/core/shape/IShapeTest.java b/core/src/test/java/com/github/introfog/pie/core/shape/IShapeTest.java index 1d7fab6..25bda53 100644 --- a/core/src/test/java/com/github/introfog/pie/core/shape/IShapeTest.java +++ b/core/src/test/java/com/github/introfog/pie/core/shape/IShapeTest.java @@ -15,13 +15,10 @@ */ package com.github.introfog.pie.core.shape; -import com.github.introfog.pie.core.math.RotationMatrix2x2; import com.github.introfog.pie.core.math.Vector2f; import com.github.introfog.pie.test.PieTest; import com.github.introfog.pie.test.annotations.UnitTest; -import java.lang.reflect.Field; - import org.junit.Assert; import org.junit.Test; import org.junit.experimental.categories.Category; @@ -29,44 +26,29 @@ @Category(UnitTest.class) public class IShapeTest extends PieTest { @Test - public void defaultConstructorTest() throws NoSuchFieldException, IllegalAccessException { + public void defaultConstructorTest() { IShape shape = new Circle(10, 0,0, 0, 0); - Assert.assertNotNull(shape.aabb); - Assert.assertNotNull(shape.rotateMatrix); + Assert.assertNotNull(shape.getAabb()); + Assert.assertNotNull(shape.getRotateMatrix()); + - Field field = RotationMatrix2x2.class.getDeclaredField("m00"); - field.setAccessible(true); - Assert.assertEquals(1, (float) field.get(shape.rotateMatrix), PieTest.FLOAT_EPSILON_COMPARISON); - field = RotationMatrix2x2.class.getDeclaredField("m01"); - field.setAccessible(true); - Assert.assertEquals(0, (float) field.get(shape.rotateMatrix), PieTest.FLOAT_EPSILON_COMPARISON); - field = RotationMatrix2x2.class.getDeclaredField("m10"); - field.setAccessible(true); - Assert.assertEquals(0, (float) field.get(shape.rotateMatrix), PieTest.FLOAT_EPSILON_COMPARISON); - field = RotationMatrix2x2.class.getDeclaredField("m11"); - field.setAccessible(true); - Assert.assertEquals(1, (float) field.get(shape.rotateMatrix), PieTest.FLOAT_EPSILON_COMPARISON); + Assert.assertEquals(1, shape.getRotateMatrix().m00, PieTest.FLOAT_EPSILON_COMPARISON); + Assert.assertEquals(0, shape.getRotateMatrix().m01, PieTest.FLOAT_EPSILON_COMPARISON); + Assert.assertEquals(0, shape.getRotateMatrix().m10, PieTest.FLOAT_EPSILON_COMPARISON); + Assert.assertEquals(1, shape.getRotateMatrix().m11, PieTest.FLOAT_EPSILON_COMPARISON); } @Test - public void setOrientationTest() throws NoSuchFieldException, IllegalAccessException { + public void setOrientationTest() { IShape shape = new Circle(10, 0,0, 0, 0); shape.setOrientation((float) Math.PI / 3); - Assert.assertEquals((float) Math.PI / 3, shape.body.orientation, PieTest.FLOAT_EPSILON_COMPARISON); + Assert.assertEquals((float) Math.PI / 3, shape.getBody().orientation, PieTest.FLOAT_EPSILON_COMPARISON); - Field field = RotationMatrix2x2.class.getDeclaredField("m00"); - field.setAccessible(true); - Assert.assertEquals(0.5f, (float) field.get(shape.rotateMatrix), PieTest.FLOAT_EPSILON_COMPARISON); - field = RotationMatrix2x2.class.getDeclaredField("m01"); - field.setAccessible(true); - Assert.assertEquals(-Math.sqrt(3) / 2, (float) field.get(shape.rotateMatrix), PieTest.FLOAT_EPSILON_COMPARISON); - field = RotationMatrix2x2.class.getDeclaredField("m10"); - field.setAccessible(true); - Assert.assertEquals(Math.sqrt(3) / 2, (float) field.get(shape.rotateMatrix), PieTest.FLOAT_EPSILON_COMPARISON); - field = RotationMatrix2x2.class.getDeclaredField("m11"); - field.setAccessible(true); - Assert.assertEquals(0.5f, (float) field.get(shape.rotateMatrix), PieTest.FLOAT_EPSILON_COMPARISON); + Assert.assertEquals(0.5f, shape.getRotateMatrix().m00, PieTest.FLOAT_EPSILON_COMPARISON); + Assert.assertEquals(-Math.sqrt(3) / 2, shape.getRotateMatrix().m01, PieTest.FLOAT_EPSILON_COMPARISON); + Assert.assertEquals(Math.sqrt(3) / 2, shape.getRotateMatrix().m10, PieTest.FLOAT_EPSILON_COMPARISON); + Assert.assertEquals(0.5f, shape.getRotateMatrix().m11, PieTest.FLOAT_EPSILON_COMPARISON); } @Test @@ -74,7 +56,7 @@ public void applyImpulseTest() { IShape shape = new Circle(1, 0,0, (float) (1 / Math.PI), 0); shape.applyImpulse(new Vector2f(10, 10), new Vector2f(1, 0)); - Assert.assertEquals(new Vector2f(10, 10), shape.body.velocity); - Assert.assertEquals(10, shape.body.angularVelocity, PieTest.FLOAT_EPSILON_COMPARISON); + Assert.assertEquals(new Vector2f(10, 10), shape.getBody().velocity); + Assert.assertEquals(10, shape.getBody().angularVelocity, PieTest.FLOAT_EPSILON_COMPARISON); } } diff --git a/core/src/test/java/com/github/introfog/pie/core/shape/PolygonTest.java b/core/src/test/java/com/github/introfog/pie/core/shape/PolygonTest.java index f6fe53c..4eca1dc 100644 --- a/core/src/test/java/com/github/introfog/pie/core/shape/PolygonTest.java +++ b/core/src/test/java/com/github/introfog/pie/core/shape/PolygonTest.java @@ -32,32 +32,32 @@ public class PolygonTest extends PieTest { @Test public void staticGenerateRectangleTest() { Polygon rectangle = Polygon.generateRectangle(0, 0, 15, 10, MathPie.STATIC_BODY_DENSITY, 0.2f); - Assert.assertEquals(4, rectangle.vertexCount); - Assert.assertEquals(4, rectangle.normals.length); - Assert.assertEquals(4, rectangle.vertices.length); + Assert.assertEquals(4, rectangle.getVertices().length); + Assert.assertEquals(4, rectangle.getNormals().length); + Assert.assertEquals(4, rectangle.getVertices().length); Vector2f vec = new Vector2f(7.5f, -5.0f); - Assert.assertEquals(vec, rectangle.vertices[0]); + Assert.assertEquals(vec, rectangle.getVertices()[0]); vec.set(7.5f, 5.0f); - Assert.assertEquals(vec, rectangle.vertices[1]); + Assert.assertEquals(vec, rectangle.getVertices()[1]); vec.set(-7.5f, 5.0f); - Assert.assertEquals(vec, rectangle.vertices[2]); + Assert.assertEquals(vec, rectangle.getVertices()[2]); vec.set(-7.5f, -5.0f); - Assert.assertEquals(vec, rectangle.vertices[3]); + Assert.assertEquals(vec, rectangle.getVertices()[3]); vec.set(1, -0.0f); - Assert.assertEquals(vec, rectangle.normals[0]); + Assert.assertEquals(vec, rectangle.getNormals()[0]); vec.set(0, 1); - Assert.assertEquals(vec, rectangle.normals[1]); + Assert.assertEquals(vec, rectangle.getNormals()[1]); vec.set(-1, -0.0f); - Assert.assertEquals(vec, rectangle.normals[2]); + Assert.assertEquals(vec, rectangle.getNormals()[2]); vec.set(0, -1); - Assert.assertEquals(vec, rectangle.normals[3]); + Assert.assertEquals(vec, rectangle.getNormals()[3]); vec.set(-7.5f, -5.0f); - Assert.assertEquals(vec, rectangle.aabb.min); + Assert.assertEquals(vec, rectangle.getAabb().min); vec.set(7.5f, 5.0f); - Assert.assertEquals(vec, rectangle.aabb.max); + Assert.assertEquals(vec, rectangle.getAabb().max); } @Test @@ -73,33 +73,33 @@ public void paramConstructorTest() { vertices.add(new Vector2f(0, 4.99f)); Polygon polygon = new Polygon(0.1f, 0.2f, 0, 0, vertices); - Assert.assertEquals(4, polygon.vertexCount); - Assert.assertEquals(4, polygon.normals.length); - Assert.assertEquals(4, polygon.vertices.length); + Assert.assertEquals(4, polygon.getVertices().length); + Assert.assertEquals(4, polygon.getNormals().length); + Assert.assertEquals(4, polygon.getVertices().length); // Check that the polygon has become convex (there are only 4 vertices) Vector2f vec = new Vector2f(7.5f, -5.0f); - Assert.assertEquals(vec, polygon.vertices[0]); + Assert.assertEquals(vec, polygon.getVertices()[0]); vec.set(7.5f, 5.0f); - Assert.assertEquals(vec, polygon.vertices[1]); + Assert.assertEquals(vec, polygon.getVertices()[1]); vec.set(-7.5f, 5.0f); - Assert.assertEquals(vec, polygon.vertices[2]); + Assert.assertEquals(vec, polygon.getVertices()[2]); vec.set(-7.5f, -5.0f); - Assert.assertEquals(vec, polygon.vertices[3]); + Assert.assertEquals(vec, polygon.getVertices()[3]); vec.set(1, -0.0f); - Assert.assertEquals(vec, polygon.normals[0]); + Assert.assertEquals(vec, polygon.getNormals()[0]); vec.set(0, 1); - Assert.assertEquals(vec, polygon.normals[1]); + Assert.assertEquals(vec, polygon.getNormals()[1]); vec.set(-1, -0.0f); - Assert.assertEquals(vec, polygon.normals[2]); + Assert.assertEquals(vec, polygon.getNormals()[2]); vec.set(0, -1); - Assert.assertEquals(vec, polygon.normals[3]); + Assert.assertEquals(vec, polygon.getNormals()[3]); vec.set(-7.5f, -5.0f); - Assert.assertEquals(vec, polygon.aabb.min); + Assert.assertEquals(vec, polygon.getAabb().min); vec.set(7.5f, 5.0f); - Assert.assertEquals(vec, polygon.aabb.max); + Assert.assertEquals(vec, polygon.getAabb().max); } @Test @@ -115,7 +115,7 @@ public void verticesInCircleTest() { vertices.add(new Vector2f(0, 0)); Polygon polygon = new Polygon(0.1f, 0.2f, 0, 0, vertices); - Assert.assertEquals(MathPie.MAX_POLY_VERTEX_COUNT, polygon.vertices.length); + Assert.assertEquals(MathPie.MAX_POLY_VERTEX_COUNT, polygon.getVertices().length); } @Test @@ -170,8 +170,8 @@ public void equalsToAnotherClassTest() { public void computeMassAndInertiaTest() { Polygon polygon = Polygon.generateRectangle(0, 0, 1, 2, 5, 0.2f); - Assert.assertEquals(1f / 10f, polygon.body.invertedMass, PieTest.FLOAT_EPSILON_COMPARISON); - Assert.assertEquals(1f / 4.166666f, polygon.body.invertedInertia, PieTest.FLOAT_EPSILON_COMPARISON); + Assert.assertEquals(1f / 10f, polygon.getBody().invertedMass, PieTest.FLOAT_EPSILON_COMPARISON); + Assert.assertEquals(1f / 4.166666f, polygon.getBody().invertedInertia, PieTest.FLOAT_EPSILON_COMPARISON); } @Test diff --git a/core/src/test/java/com/github/introfog/pie/core/util/ShapeIOUtilTest.java b/core/src/test/java/com/github/introfog/pie/core/util/ShapeIOUtilTest.java index f4d0b2f..1f6bf2a 100644 --- a/core/src/test/java/com/github/introfog/pie/core/util/ShapeIOUtilTest.java +++ b/core/src/test/java/com/github/introfog/pie/core/util/ShapeIOUtilTest.java @@ -75,7 +75,7 @@ public void readShapesTest() throws IOException { Polygon polygon = Polygon.generateRectangle(20, 23, 15, 17, 0.17f, 1.23f); Polygon actPolygon = (Polygon) (shapes[0] instanceof Polygon ? shapes[0] : shapes[1]); - Assert.assertEquals(polygon.getVertexCount(), actPolygon.getVertexCount()); + Assert.assertEquals(polygon.getVertices().length, actPolygon.getVertices().length); Assert.assertArrayEquals(polygon.getVertices(), actPolygon.getVertices()); Assert.assertEquals(polygon.getBody(), actPolygon.getBody()); } @@ -91,7 +91,7 @@ public void convertStringToShapeTest() { String strPolygon = "Polygon;4;7.5;-8.5;7.5;8.5;-7.5;8.5;-7.5;-8.5;20.0;23.0;0.17;1.23"; Polygon polygon = Polygon.generateRectangle(20, 23, 15, 17, 0.17f, 1.23f); Polygon actPolygon = (Polygon) ShapeIOUtil.convertStringToShape(strPolygon); - Assert.assertEquals(polygon.getVertexCount(), actPolygon.getVertexCount()); + Assert.assertEquals(polygon.getVertices().length, actPolygon.getVertices().length); Assert.assertArrayEquals(polygon.getVertices(), actPolygon.getVertices()); Assert.assertEquals(polygon.getBody(), actPolygon.getBody()); }