Matrix2f rot2 = new Matrix2f(body2.getRotation());
r1 = MathUtil.mul(rot1,anchor1);
r2 = MathUtil.mul(rot2,anchor2);
Vector2f p1 = new Vector2f(body1.getPosition());
p1.add(r1);
Vector2f p2 = new Vector2f(body2.getPosition());
p2.add(r2);
Vector2f dp = new Vector2f(p2);
dp.sub(p1);
Vector2f dv = new Vector2f(body2.getVelocity());
dv.add(MathUtil.cross(body2.getAngularVelocity(),r2));
dv.sub(body1.getVelocity());
dv.sub(MathUtil.cross(body1.getAngularVelocity(),r1));
ndp = new Vector2f(dp);
ndp.normalise();
restitute = -restitutionConstant*dv.dot(ndp);
Vector2f v1 = new Vector2f(ndp);
v1.scale(-body2.getInvMass() - body1.getInvMass());
Vector2f v2 = MathUtil.cross(MathUtil.cross(r2, ndp), r2);
v2.scale(-body2.getInvI());
Vector2f v3 = MathUtil.cross(MathUtil.cross(r1, ndp),r1);
v3.scale(-body1.getInvI());
Vector2f K1 = new Vector2f(v1);
K1.add(v2);
K1.add(v3);
K = K1.dot(ndp);
float length=dp.lengthSquared();
if(length<minDistance){
if(collideSide!=COLLIDE_MIN)
accumulateImpulse=0;
collideSide=COLLIDE_MIN;
biasImpulse=-biasFactor*(length-minDistance);
if(restitute<0)
restitute=0;
}else if(length>maxDistance){
if(collideSide!=COLLIDE_MAX)
accumulateImpulse=0;
collideSide=COLLIDE_MAX;
biasImpulse=-biasFactor*(length-maxDistance);
if(restitute>0)
restitute=0;
}else{
collideSide=COLLIDE_NONE;
accumulateImpulse=0;
}
restitute+=biasImpulse;
Vector2f impulse = new Vector2f(ndp);
impulse.scale(accumulateImpulse);
if (!body1.isStatic()) {
Vector2f accum1 = new Vector2f(impulse);
accum1.scale(body1.getInvMass());
body1.adjustVelocity(accum1);
body1.adjustAngularVelocity((body1.getInvI() * MathUtil.cross(r1, impulse)));
}
if (!body2.isStatic()) {
Vector2f accum2 = new Vector2f(impulse);
accum2.scale(-body2.getInvMass());
body2.adjustVelocity(accum2);
body2.adjustAngularVelocity(-(body2.getInvI() * MathUtil.cross(r2, impulse)));
}
}