void PenetratingResponse(CRigidBody body1, CRigidBody body2, float RelVel, Vector3 Normal) { var CoefResti = (body1.mRestitution + body2.mRestitution) / 2; //set numerator var Numer = -(1 + CoefResti) * RelVel; float Denom; if (body1.mMass == 0) { Denom = 1 / body2.mMass; } else if (body2.mMass == 0) { Denom = 1 / body1.mMass; } else { Denom = 1 / body1.mMass + 1 / body2.mMass; } var Impulse = Numer / Denom; body1.ApplyImpulse(-Normal * Impulse); body2.ApplyImpulse(Normal * Impulse); // friction /*var RV = body2.mVelocity - body1.mVelocity; * var tangent = RV - Vector3.Dot (RV, Normal) * Normal; * tangent.Normalize (); * float mag = -Vector3.Dot (RV,tangent) / (1/body1.mMass + 1/body2.mMass); * * float staticcoef = Mathf.Sqrt ((body1.mStaticFriction * body1.mStaticFriction) + (body2.mStaticFriction * body2.mStaticFriction)); * if (Mathf.Abs (mag) < Impulse * staticcoef) * { * body1.ApplyForce( mag * tangent); * body2.ApplyForce(mag* -tangent); * } * else * { * float dynamiccoef = Mathf.Sqrt ((body1.mDynamicFriction * body1.mDynamicFriction) + (body2.mDynamicFriction * body2.mDynamicFriction)); * body1.ApplyForce( -Impulse * tangent * dynamiccoef); * body2.ApplyForce(Impulse * -tangent * dynamiccoef); * }*/ }
void PenetratingResponse(CRigidBody body1, CRigidBody body2,float RelVel, Vector3 Normal) { var CoefResti = (body1.mRestitution+body2.mRestitution)/2; //set numerator var Numer = -(1+CoefResti)*RelVel; float Denom; if(body1.mMass == 0) Denom = 1/body2.mMass; else if(body2.mMass == 0) Denom = 1/body1.mMass; else Denom = 1/body1.mMass + 1/body2.mMass; var Impulse = Numer / Denom; body1.ApplyImpulse (-Normal*Impulse); body2.ApplyImpulse (Normal*Impulse); // friction /*var RV = body2.mVelocity - body1.mVelocity; var tangent = RV - Vector3.Dot (RV, Normal) * Normal; tangent.Normalize (); float mag = -Vector3.Dot (RV,tangent) / (1/body1.mMass + 1/body2.mMass); float staticcoef = Mathf.Sqrt ((body1.mStaticFriction * body1.mStaticFriction) + (body2.mStaticFriction * body2.mStaticFriction)); if (Mathf.Abs (mag) < Impulse * staticcoef) { body1.ApplyForce( mag * tangent); body2.ApplyForce(mag* -tangent); } else { float dynamiccoef = Mathf.Sqrt ((body1.mDynamicFriction * body1.mDynamicFriction) + (body2.mDynamicFriction * body2.mDynamicFriction)); body1.ApplyForce( -Impulse * tangent * dynamiccoef); body2.ApplyForce(Impulse * -tangent * dynamiccoef); }*/ }