The idea is simple, it is to calculate the velocity of two objects after constraints.
Constraint code:
const VECTOR3<T> vRadiusObject1 = param.vObj1ContactPoint - param.Obj1.vBarycenter;
const VECTOR3<T> vRadiusObject2 = param.vObj2ContactPoint - param.Obj2.vBarycenter;
const VECTOR3<T> vRelativeVelocity1 = param.Obj1.vVelocity + cross(param.Obj1.vAngularVelocity, vRadiusObject1);
const VECTOR3<T> vRelativeVelocity2 = param.Obj2.vVelocity + cross(param.Obj2.vAngularVelocity, vRadiusObject2);
T fImpulse = (-dot(param.vDirection, vRelativeVelocity1 - vRelativeVelocity2) * (1 + param.RestitutionCoefficient)) / (param.Obj1.fInverseMass + param.Obj2.fInverseMass +
dot(cross(cross(vRadiusObject1, param.vDirection), vRadiusObject1) * param.Obj1.mtInverseInertia +
cross(cross(vRadiusObject2, param.vDirection), vRadiusObject2) * param.Obj2.mtInverseInertia, param.vDirection ));
param.pOutVelocity1->vVelocity = param.Obj1.vVelocity + (param.vDirection * fImpulse * param.Obj1.fInverseMass);
param.pOutVelocity1->vAngularVelocity = param.Obj1.vAngularVelocity + (cross(vRadiusObject1, param.vDirection) * fImpulse * param.Obj1.mtInverseInertia);
param.pOutVelocity2->vVelocity = param.Obj2.vVelocity - (param.vDirection * fImpulse * param.Obj2.fInverseMass);
param.pOutVelocity2->vAngularVelocity = param.Obj2.vAngularVelocity - (cross(vRadiusObject2, param.vDirection) * fImpulse * param.Obj2.mtInverseInertia);
return fImpulse;
Use the formula: constraint formula
But it was found that the calculated point velocities of the two objects were not the same.
Calculate Point Velocity Code:
const VECTOR3<T> vRadiusObject = vPoint - vCenter;
return motion.vVelocity + cross(motion.vAngularVelocity, vRadiusObject);
The Test Data:
float fMass1 = 100;
float fMass2 = 230;
const VECTOR3<float> vCenter[] = { VECTOR3<float>(0, 9, 0), VECTOR3<float>(0, 0, 0) };
const VECTOR3<float> ContactPoint[] = { VECTOR3<float>(-3, 4.5, -3), VECTOR3<float>(-3, 4.5, 3), VECTOR3<float>(3, 4.5, -3), VECTOR3<float>(3, 4.5, 3) };
const VECTOR3<float> vSize = VECTOR3<float>(6, 9, 6);
Motion<> motion1, motion2;
MATRIX44<float> mtOBB1 = MATRIX44<float>::Identity;
MATRIX44<float> mtOBB2 = MATRIX44<float>::Identity;
motion1.vVelocity = VECTOR3<float>(0, 200, 0);
motion1.vAngularVelocity = VECTOR3<float>(0, 2, 0);
motion2.vVelocity = VECTOR3<float>(0, 100, 0);
motion2.vAngularVelocity = VECTOR3<float>(10, 0, 0);
I calculated "ContactPoint[0]"(-3, 4.5, -3) The velocity of the constraint point after constraint calculation:
OBJECT1 ContactPoint[0] : {x=3.92508936 y=160.614685 z=46.1242218}
OBJECT2 ContactPoint[0] : {x=8.37378883 y=138.909134 z=40.2669983}
But according to the derivation of the formula they should be equal. WHY????
Here is my test project,full source code: https://github.com/noodlerun/impulse.git
I expect that the relative velocities of the constraint points of the two objects should be equal.