Skip to content

Commit

Permalink
#94 Add some improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
introfog committed Feb 21, 2021
1 parent 813aace commit 01a0fcc
Show file tree
Hide file tree
Showing 13 changed files with 118 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public Set<IShape> getUnmodifiableShapes() {
* <p>
* 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
Expand All @@ -93,7 +93,7 @@ public final Set<ShapePair> calculateAabbCollisions() {
* Domestic method for calculating the shape Aabb collisions.
*
* <p>
* 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]);

Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
*/
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. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
}
20 changes: 9 additions & 11 deletions core/src/main/java/com/github/introfog/pie/core/shape/IShape.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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;
}

Expand All @@ -82,7 +80,7 @@ public Aabb getAabb() {
*
* @return the body
*/
public Body getBody() {
public final Body getBody() {
return body;
}

Expand All @@ -91,7 +89,7 @@ public Body getBody() {
*
* @return the rotation matrix
*/
public RotationMatrix2x2 getRotateMatrix() {
public final RotationMatrix2x2 getRotateMatrix() {
return rotateMatrix;
}

Expand All @@ -114,7 +112,7 @@ public RotationMatrix2x2 getRotateMatrix() {
* @return {@inheritDoc}
*/
@Override
public boolean equals(Object o) {
public final boolean equals(Object o) {
return this == o;
}

Expand All @@ -125,7 +123,7 @@ public boolean equals(Object o) {
* @return {@inheritDoc}
*/
@Override
public int hashCode() {
public final int hashCode() {
return shapeId;
}

Expand Down
71 changes: 30 additions & 41 deletions core/src/main/java/com/github/introfog/pie/core/shape/Polygon.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -62,7 +60,7 @@ public Polygon(float density, float restitution, float centreX, float centreY, L
}

List<Integer> hull = Polygon.calculateHullIndices(vertices);
vertexCount = hull.size();
final int vertexCount = hull.size();

if (vertexCount > MathPie.MAX_POLY_VERTEX_COUNT) {
// TODO create Pie custom exception
Expand Down Expand Up @@ -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);
}

Expand All @@ -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();
}
Expand All @@ -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);

Expand All @@ -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;
Expand All @@ -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<Integer> calculateHullIndices(List<Vector2f> vertices) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(";");
}
Expand Down
Loading

0 comments on commit 01a0fcc

Please sign in to comment.