void ParticleContact_2D::resolveVelocity() { if(p2 && contactNormal == glm::vec2(0,0)) contactNormal = glm::normalize(p1->Position - p2->Position); float separatingVelocity = calculateSeparatingVelocity(); if(separatingVelocity > 0) return; float velocityFromAcceleration = glm::dot(p1->Acceleration, contactNormal) * UpdateClock::DeltaTime(); if(p2) velocityFromAcceleration += glm::dot(p2->Acceleration, contactNormal) * UpdateClock::DeltaTime(); if(velocityFromAcceleration < 0) { // remove any closing velocity due to acceleration. separatingVelocity -= velocityFromAcceleration; } float totalInverseMass = p1->InverseMass; if(p2) totalInverseMass += p2->InverseMass; float totalImpulse = (-separatingVelocity) * (restitution + 1) / totalInverseMass; glm::vec2 directionalImpulse = totalImpulse * contactNormal; p1->AddImpulse(directionalImpulse); if(p2) p2->AddImpulse(-directionalImpulse); }
void ParticleContact::resolveVelocity(float t) { float separatingVelocity = calculateSeparatingVelocity(); if (separatingVelocity > 0) return; float newSepVelocity = -separatingVelocity*restitution; float deltaVelocity = newSepVelocity - separatingVelocity; float totalInverseMass = particle[0]->getInverseMass(); if (particle[1]) totalInverseMass += particle[1]->getInverseMass(); if (totalInverseMass <= 0) return; float impulse = deltaVelocity / totalInverseMass; Vector impulsePerIMass = contactNormal * impulse; particle[0]->setVelocity(particle[0]->getVelocity() + impulsePerIMass*particle[0]->getInverseMass()); if (particle[1]) { particle[1]->setVelocity(particle[1]->getVelocity() + impulsePerIMass* -particle[1]->getInverseMass()); } }