void CollisionSolver::SolveParticleSphereCollision(CollisionMesh& particle, const CollisionMesh& sphere) { D3DXVECTOR3 sphereToParticle = particle.GetPosition() - sphere.GetPosition(); const float lengthSqr = D3DXVec3LengthSq(&sphereToParticle); const float combinedRadius = sphere.GetRadius() + particle.GetRadius(); if (lengthSqr < (combinedRadius*combinedRadius)) { const float length = std::sqrt(lengthSqr); sphereToParticle /= length; particle.ResolveCollision(sphereToParticle * fabs(combinedRadius-length), sphere.GetVelocity(), sphere.GetShape()); } }
void CollisionSolver::SolveParticleCollision(CollisionMesh& particleA, CollisionMesh& particleB) { D3DXVECTOR3 particleToParticle = particleB.GetPosition() - particleA.GetPosition(); const float lengthSqr = D3DXVec3LengthSq(&particleToParticle); const float combinedRadius = particleA.GetRadius() + particleB.GetRadius(); if (lengthSqr < (combinedRadius*combinedRadius)) { const float length = std::sqrt(lengthSqr); particleToParticle /= std::sqrt(length); const D3DXVECTOR3 translation = particleToParticle*fabs(combinedRadius-length); particleA.ResolveCollision(-translation); particleB.ResolveCollision(translation); } }
void CollisionSolver::SolveParticleHullCollision(CollisionMesh& particle, const CollisionMesh& hull) { // Determine if within a rough radius of the convex hull const D3DXVECTOR3 sphereToParticle = particle.GetPosition() - hull.GetPosition(); const float lengthSqr = D3DXVec3LengthSq(&sphereToParticle); const float extendedParticleRadius = particle.GetRadius() * 2.0f; const float combinedRadius = hull.GetRadius() + extendedParticleRadius; if (lengthSqr < (combinedRadius*combinedRadius)) { Simplex simplex; if(AreConvexHullsColliding(particle, hull, simplex)) { simplex.GenerateFaces(); const D3DXVECTOR3 penetration = GetConvexHullPenetration(particle, hull, simplex); particle.ResolveCollision(penetration, hull.GetVelocity(), hull.GetShape()); } } }