Skip to content

Commit

Permalink
#94 Make all field in Body class protected
Browse files Browse the repository at this point in the history
  • Loading branch information
introfog committed Mar 14, 2021
1 parent 0e20ba6 commit 797729b
Show file tree
Hide file tree
Showing 17 changed files with 164 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected void domesticApplyAction(List<IBroadPhase> methods, Set<IShape> method
}
IShape[] arrayMethodShapes = methodShapes.toArray(new IShape[] {});
for (int i = 0; i < arrayMethodShapes.length; i += oneMovingBodyOfBodies) {
arrayMethodShapes[i].getBody().position.add(offset, i % 2 == 0 ? -1 : 1);
arrayMethodShapes[i].getBody().getPosition().add(offset, i % 2 == 0 ? -1 : 1);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,21 @@ protected void domesticApplyAction(List<IBroadPhase> methods, Set<IShape> method
callCounter = -iterationOneWay;
}

Vector2f center = methodShapes.stream().map(shape -> shape.getBody().position).collect(Collectors.toList()).stream().
Vector2f center = methodShapes.stream().map(shape -> shape.getBody().getPosition()).collect(Collectors.toList()).stream().
reduce((sum, current) -> {sum.add(current); return sum;}).orElse(new Vector2f());
center.mul(1.0f / methodShapes.size());

for (IShape shape : methodShapes) {
float dist = (float) Math.sqrt(Vector2f.distanceWithoutSqrt(center, shape.getBody().position));
float dist = (float) Math.sqrt(Vector2f.distanceWithoutSqrt(center, shape.getBody().getPosition()));
float cos = 0;
float sin = 0;
if (dist != 0) {
cos = (shape.getBody().position.x - center.x) / dist;
sin = (shape.getBody().position.y - center.y) / dist;
cos = (shape.getBody().getPosition().x - center.x) / dist;
sin = (shape.getBody().getPosition().y - center.y) / dist;
}
Vector2f offset = new Vector2f(cos, sin);
offset.mul(callCounter > 0 ? offsetValue : -offsetValue);
shape.getBody().position.add(offset);
shape.getBody().getPosition().add(offset);
}
}
}
26 changes: 8 additions & 18 deletions core/src/main/java/com/github/introfog/pie/core/World.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import com.github.introfog.pie.core.collisions.Manifold;
import com.github.introfog.pie.core.collisions.narrowphase.IShapeCollisionHandler;
import com.github.introfog.pie.core.math.MathPie;
import com.github.introfog.pie.core.shape.Body;
import com.github.introfog.pie.core.shape.IShape;
import com.github.introfog.pie.core.shape.ShapePair;

Expand Down Expand Up @@ -151,7 +150,7 @@ private void step() {

// Integrate forces
// Hanna modification Euler's method is used!
shapes.forEach(this::integrateForces);
shapes.forEach(this::integrateForce);

// Narrow phase
manifolds.clear();
Expand Down Expand Up @@ -182,35 +181,26 @@ private void step() {

// Integrate forces
// Hanna modification Euler's method is used!
shapes.forEach(this::integrateForces);
shapes.forEach(this::integrateForce);

// Correct positions
manifolds.forEach(Manifold::correctPosition);

// Clear all forces
shapes.forEach(shape -> shape.getBody().force.set(0f, 0f));
shapes.forEach(shape -> shape.getBody().getForce().set(0f, 0f));
}

private void integrateForces(IShape shape) {
final Body body = shape.getBody();
if (body.getInvertedMass() == 0.0f) {
private void integrateForce(IShape shape) {
if (shape.getBody().getInvertedMass() == 0.0f) {
return;
}
final float halfDeltaTime = context.getFixedDeltaTime() * 0.5f;

body.velocity.add(body.force, body.getInvertedMass() * halfDeltaTime);
body.velocity.add(context.getGravity(), halfDeltaTime);
body.angularVelocity += body.torque * body.getInvertedInertia() * halfDeltaTime;
shape.integrateForce(context.getFixedDeltaTime() * 0.5f, context.getGravity());
}

private void integrateVelocity(IShape shape) {
final Body body = shape.getBody();
if (body.getInvertedMass() == 0.0f) {
if (shape.getBody().getInvertedMass() == 0.0f) {
return;
}

body.position.add(body.velocity, context.getFixedDeltaTime());

shape.setOrientation(body.getOrientation() + body.angularVelocity * context.getFixedDeltaTime());
shape.integrateVelocity(context.getFixedDeltaTime());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,16 @@ public void solve() {

for (int i = 0; i < contactCount; i++) {
// Calculate the contact points regarding centers
Vector2f radA = Vector2f.sub(contacts[i], a.position);
Vector2f radB = Vector2f.sub(contacts[i], b.position);
Vector2f radA = Vector2f.sub(contacts[i], a.getPosition());
Vector2f radB = Vector2f.sub(contacts[i], b.getPosition());

// Calculate the relative velocity
// Vec2 rv = B->velocity + Cross( B->angularVelocity, rb ) -
// A->velocity - Cross( A->angularVelocity, ra );
// relativeVelocity
Vector2f rv = Vector2f.sub(b.velocity, a.velocity);
rv.add(Vector2f.crossProduct(b.angularVelocity, radB));
rv.sub(Vector2f.crossProduct(a.angularVelocity, radA));
Vector2f rv = Vector2f.sub(b.getVelocity(), a.getVelocity());
rv.add(Vector2f.crossProduct(b.getAngularVelocity(), radB));
rv.sub(Vector2f.crossProduct(a.getAngularVelocity(), radA));

// Calculate the relative velocity relative to the normal direction
float velAlongNormal = Vector2f.dotProduct(rv, normal);
Expand Down Expand Up @@ -94,9 +94,9 @@ public void solve() {
// Friction work

// Recalculation relative speed after application of a normal impulse
rv = Vector2f.sub(b.velocity, a.velocity); //relativeVelocity
rv.add(Vector2f.crossProduct(b.angularVelocity, radB));
rv.sub(Vector2f.crossProduct(a.angularVelocity, radA));
rv = Vector2f.sub(b.getVelocity(), a.getVelocity()); //relativeVelocity
rv.add(Vector2f.crossProduct(b.getAngularVelocity(), radB));
rv.sub(Vector2f.crossProduct(a.getAngularVelocity(), radA));

// Calculate the tangent vector: tangent = rb - dotProduct (rv, normal) * normal
// Vec2 t = rv - (normal * Dot( rv, normal ));
Expand Down Expand Up @@ -135,39 +135,39 @@ public void correctPosition() {
}
Vector2f correction = Vector2f.mul(normal, penetration *
context.getCorrectPositionPercent() / (a.getInvertedMass() + b.getInvertedMass()));
a.position.sub(Vector2f.mul(correction, a.getInvertedMass()));
b.position.add(Vector2f.mul(correction, b.getInvertedMass()));
a.getPosition().sub(Vector2f.mul(correction, a.getInvertedMass()));
b.getPosition().add(Vector2f.mul(correction, b.getInvertedMass()));
}

private void initializeCollision() {
// Static friction - is a value that shows how much energy need to apply to moving the body,
// i.e. this is the threshold, if the energy is lower, then the body is at rest, if higher,
// then the body moves. Dynamic friction - friction in the usual sense, when bodies rub against
// each other, they lose part of their energy against each other
staticFriction = (float) StrictMath.sqrt(
a.staticFriction * a.staticFriction + b.staticFriction * b.staticFriction);
dynamicFriction = (float) StrictMath.sqrt(
a.dynamicFriction * a.dynamicFriction + b.dynamicFriction * b.dynamicFriction);
staticFriction = (float) StrictMath.sqrt(a.getStaticFriction() * a.getStaticFriction() +
b.getStaticFriction() * b.getStaticFriction());
dynamicFriction = (float) StrictMath.sqrt(a.getDynamicFriction() * a.getDynamicFriction() +
b.getDynamicFriction() * b.getDynamicFriction());

// Calculate the elasticity
e = Math.min(a.restitution, b.restitution);
e = Math.min(a.getRestitution(), b.getRestitution());

for (int i = 0; i < contactCount; ++i) {
// Calculate radii from COM to contact
// Vec2 ra = contacts[i] - A->position;
// Vec2 rb = contacts[i] - B->position;
Vector2f radA = Vector2f.sub(contacts[i], a.position);
Vector2f radB = Vector2f.sub(contacts[i], b.position);
Vector2f radA = Vector2f.sub(contacts[i], a.getPosition());
Vector2f radB = Vector2f.sub(contacts[i], b.getPosition());

// Vec2 rv = B->velocity + Cross( B->angularVelocity, rb ) -
// A->velocity - Cross( A->angularVelocity, ra );
// Calculate the relative speed
// Vec2 rv = B->velocity + Cross( B->angularVelocity, rb ) -
// A->velocity - Cross( A->angularVelocity, ra );
//relativeVelocity
Vector2f rv = Vector2f.sub(b.velocity, a.velocity);
rv.add(Vector2f.crossProduct(b.angularVelocity, radB));
rv.sub(Vector2f.crossProduct(a.angularVelocity, radA));
Vector2f rv = Vector2f.sub(b.getVelocity(), a.getVelocity());
rv.add(Vector2f.crossProduct(b.getAngularVelocity(), radB));
rv.sub(Vector2f.crossProduct(a.getAngularVelocity(), radA));

// Determine whether should perform a collision with a stop or not.
// The idea is that the only thing that moves this object is gravity,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public Manifold handleCollision(IShape aShape, IShape bShape, Context context) {
Circle circleB = (Circle) bShape;

Manifold manifold = new Manifold(circleA, circleB, context);
manifold.normal = Vector2f.sub(circleB.getBody().position, circleA.getBody().position);
manifold.normal = Vector2f.sub(circleB.getBody().getPosition(), circleA.getBody().getPosition());
final float distanceWithoutSqrt = manifold.normal.lengthWithoutSqrt();

if (!CircleCircleCollisionHandler.areIntersected(circleA, circleB, distanceWithoutSqrt)) {
Expand All @@ -50,12 +50,12 @@ public Manifold handleCollision(IShape aShape, IShape bShape, Context context) {
manifold.normal.normalize();
manifold.contacts[0].set(manifold.normal);
manifold.contacts[0].mul(circleA.getRadius());
manifold.contacts[0].add(circleA.getBody().position);
manifold.contacts[0].add(circleA.getBody().getPosition());

if (distanceWithoutSqrt == 0) {
manifold.normal.set(1f, 0f);
manifold.penetration = circleA.getRadius();
manifold.contacts[0].set(circleA.getBody().position);
manifold.contacts[0].set(circleA.getBody().getPosition());
}

return manifold;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public Manifold handleCollision(IShape aShape, IShape bShape, Context context) {
Manifold manifold = new Manifold(circleA, polygonB, context);
// Translate center coordinates to polygon coordinates
// center = B->u.Transpose( ) * (center - b->position);
Vector2f centerA = new Vector2f(circleA.getBody().position);
centerA.sub(polygonB.getBody().position);
Vector2f centerA = new Vector2f(circleA.getBody().getPosition());
centerA.sub(polygonB.getBody().getPosition());
polygonB.getRotateMatrix().transposeMul(centerA, centerA);

// Looking for the nearest edge of the polygon to the center of the circle,
Expand Down Expand Up @@ -105,7 +105,7 @@ public Manifold handleCollision(IShape aShape, IShape bShape, Context context) {

manifold.contacts[0].set(manifold.normal);
manifold.contacts[0].mul(circleA.getRadius());
manifold.contacts[0].add(circleA.getBody().position);
manifold.contacts[0].add(circleA.getBody().getPosition());
manifold.penetration = circleA.getRadius();
return manifold;
}
Expand Down Expand Up @@ -138,7 +138,7 @@ public Manifold handleCollision(IShape aShape, IShape bShape, Context context) {
n.normalize();
manifold.normal.set(n);
polygonB.getRotateMatrix().mul(v1, v1);
v1.add(polygonB.getBody().position);
v1.add(polygonB.getBody().getPosition());
manifold.contacts[0].set(v1);
} else if (dot2 <= 0f) {
// Closer to the second vertex
Expand All @@ -160,7 +160,7 @@ public Manifold handleCollision(IShape aShape, IShape bShape, Context context) {
n.normalize();
manifold.normal.set(n);
polygonB.getRotateMatrix().mul(v2, v2);
v2.add(polygonB.getBody().position);
v2.add(polygonB.getBody().getPosition());
manifold.contacts[0].set(v2);
} else {
// Closer to the front vertex
Expand All @@ -177,7 +177,7 @@ public Manifold handleCollision(IShape aShape, IShape bShape, Context context) {
manifold.normal.set(n);
manifold.contacts[0].set(manifold.normal);
manifold.contacts[0].mul(circleA.getRadius());
manifold.contacts[0].add(circleA.getBody().position);
manifold.contacts[0].add(circleA.getBody().getPosition());
}
return manifold;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ public Manifold handleCollision(IShape aShape, IShape bShape, Context context) {
// v1 = RefPoly->u * v1 + RefPoly->body->position;
// v2 = RefPoly->u * v2 + RefPoly->body->position;
refPoly.getRotateMatrix().mul(v1, v1);
v1.add(refPoly.getBody().position);
v1.add(refPoly.getBody().getPosition());
refPoly.getRotateMatrix().mul(v2, v2);
v2.add(refPoly.getBody().position);
v2.add(refPoly.getBody().getPosition());

// Calculate reference face side normal in world space
// Vec2 sidePlaneNormal = (v2 - v1);
Expand Down Expand Up @@ -207,10 +207,10 @@ private void findIncidentFace(Vector2f[] v, Polygon refPoly, Polygon incPoly, in
// v[1] = IncPoly->u * IncPoly->m_vertices[incidentFace] +
// IncPoly->body->position;
incPoly.getRotateMatrix().mul(incPoly.getVertices()[incidentFace], v[0]);
v[0].add(incPoly.getBody().position);
v[0].add(incPoly.getBody().getPosition());
incidentFace = incidentFace + 1 >= incPoly.getVertices().length ? 0 : incidentFace + 1;
incPoly.getRotateMatrix().mul(incPoly.getVertices()[incidentFace], v[1]);
v[1].add(incPoly.getBody().position);
v[1].add(incPoly.getBody().getPosition());
}

private int clip(Vector2f n, float c, Vector2f[] face) {
Expand Down Expand Up @@ -287,8 +287,8 @@ private static float findAxisLeastPenetration(int[] faceIndex, Polygon polygonA,
// v = buT * v;
Vector2f v = new Vector2f(polygonA.getVertices()[i]);
polygonA.getRotateMatrix().mul(v, v);
v.add(polygonA.getBody().position);
v.sub(polygonB.getBody().position);
v.add(polygonA.getBody().getPosition());
v.sub(polygonB.getBody().getPosition());
polygonB.getRotateMatrix().transposeMul(v, v);

// Calculate penetration (in local coordinates B)
Expand Down
Loading

0 comments on commit 797729b

Please sign in to comment.