So, my problem is that once a shape gets a substantial amount of angular momentum(anything visually noticeable), then the collision no longer works. It collides, slows down, but doesn't rebound as elasticity dictates, nor does it 'impulse' in one pass, instead gradually slowing down until it becomes stagnant.
public Vec2 getVelocityAt(Vec2 p) {
return getLinearVelocity().add(new Vec2(-getAngularVelocity() * p.getY(), getAngularVelocity() * p.getX()));
}
I'm fairly certain the issue is directly related to the above code, I'm following the equations here: http://www.myphysicslab.com/collision.html
public boolean checkBounds() {
if(bX1 == bX2 || bY1 == bY2) {
return false;
}
double xa = 0D;
double ya = 0D;
int a = 0;
boolean hC = false;
double xn = 0D;
double yn = 0D;
for(double[] vertex : this.getShape().getVerticies()) {
if(vertex[0] >= bX2) {
xa += vertex[0];
ya += vertex[1];
a++;
hC = true;
xn -= 1D;
}
if(vertex[0] <= bX1) {
xa += vertex[0];
ya += vertex[1];
a++;
hC = true;
xn += 1D;
}
if(vertex[1] >= bY2) {
xa += vertex[0];
ya += vertex[1];
a++;
hC = true;
yn -= 1D;
}
if(vertex[1] <= bY1) {
xa += vertex[0];
ya += vertex[1];
a++;
hC = true;
yn += 1D;
}
}
if(hC) {
Vec2 n = new Vec2(xn, yn).uscale();
Vec2 cp = new Vec2(xa / a, ya / a);
Vec2 rap = cp.sub(getShape().getCM());
Vec2 va = getVelocityAt(rap);
Vec2 rva = va;
Vec2 rvb = Vec2.ZERO_VEC.sub(va);
double pj = -(1D + getElasticity()) * rva.dot(n);
double j = pj / (1 / getShape().getMass() + Math.pow(rap.cross(n), 2D) / getShape().getMass());
applyForce(new AppliedForce(n.scale(j), rap, getShape().getMass()));
}
return hC;
}
public void applyForce(AppliedForce... f) {
for(AppliedForce vec : f) {
Vec2 a = vec.getForce().divide(getShape().getMass());
applyLinearAcceleration(a);
double aa = a.relativeCosine(getShape().getTangent(vec.getOffsetCM())) * a.getMagnitude() / getShape().getMass();
applyAngularAcceleration(aa);
}
}
If there is no angular velocity, then no issue occurs, and is pronounced more by higher angular velocity. I think the velocity at a point function is the root of the issue, but not certain as to why. I have done the debugging, not having found much, I assume it is a misunderstanding of the math. Does anyone know what I did wrong?