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 43922b0
Show file tree
Hide file tree
Showing 13 changed files with 124 additions and 151 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
16 changes: 8 additions & 8 deletions core/src/main/java/com/github/introfog/pie/core/shape/Body.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,29 @@
*/
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. */
public float angularVelocity;
/** 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.
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
Loading

0 comments on commit 43922b0

Please sign in to comment.