void CollisionSolver::SolveObjectCollision(CollisionMesh& particle, const CollisionMesh& object) { if(particle.IsDynamic()) { if(object.GetShape() == Geometry::SPHERE) { SolveParticleSphereCollision(particle, object); } else { SolveParticleHullCollision(particle, object); } } }
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::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()); } } }