// The is the top-level collision detection function. // // The MovingObject doesn't intersect anything now; would it // do so if the vector displacement is applied? // // If we do have a collision, we "creep up" on the object we collide // with until we're within stepsize of the minimum dimension of the // moving part // public bool TestCollision(MovingObject mo, float stepSize, ref Vector3 displacement, CollisionParms parms) { topLevelCalls++; parms.Initialize(); if (mo.parts.Count == 0) { return(false); } // Remember where the first part started Vector3 start = mo.parts[0].shape.center; Vector3 originalDisplacement = displacement; float len = displacement.Length; //MaybeDumpSphereTree(); int nsteps = (int)Math.Ceiling(len / stepSize); Vector3 step = (len <= stepSize ? displacement : displacement * (stepSize / len)); // Try to step the whole way if (!StepTowardCollision(mo, displacement, nsteps, step, parms)) { displacement = Vector3.Zero; return(false); } // Otherwise, we hit something. Step toward it // The minimum distance float smallStepSize = Math.Min(MO.InchesToMeters(MinInchesToObstacle), stepSize * MinFractionToObstacle); len = step.Length; nsteps = (int)Math.Ceiling(len / smallStepSize); Vector3 smallStep = step * (smallStepSize / len); bool hit = StepTowardCollision(mo, step, nsteps, smallStep, parms); displacement = originalDisplacement - (mo.parts[0].shape.center - start); return(hit); }