// Test all the parts of a moving object to see if they collide with // any obstacle public bool CollideAfterAddingDisplacement(MovingObject mo, Vector3 step, CollisionParms parms) { for (int i = 0; i < mo.parts.Count; i++) { MovingPart movingPart = mo.parts[i]; collisionTimeStamp++; movingPart.AddDisplacement(step); SphereTreeNode s = sphereTree.FindSmallestContainer(movingPart.shape, movingPart.sphere); //MaybeDumpSphereTree(); movingPart.sphere = s; partCalls++; if (s.TestSphereCollision(movingPart.shape, collisionTimeStamp, ref collisionTestCount, parms)) { // We hit something, so back out the displacements // added to the parts tested so far for (int j = 0; j <= i; j++) { MovingPart displacedPart = mo.parts[j]; displacedPart.AddDisplacement(-step); //MaybeDumpSphereTree(); } return(true); } } return(false); }
public bool ShapeCollides(CollisionShape shape, CollisionParms parms) { collisionTimeStamp++; SphereTreeNode s = sphereTree.FindSmallestContainer(shape, null); return(s.TestSphereCollision(shape, collisionTimeStamp, ref collisionTestCount, parms)); }
// Test for collision with any object in this sphere public bool TestSphereCollision(CollisionShape part, ulong timeStamp, ref int collisionTestCount, CollisionParms parms) { if (containedShape != null) { collisionTestCount++; parms.swapped = false; if (Primitives.TestCollisionFunctions[(int)part.ShapeType(), (int)containedShape.ShapeType()] (part, containedShape, parms)) { parms.part = part; parms.obstacle = containedShape; return(true); } else { containedShape.timeStamp = timeStamp; } } // Iterate over the shapes in this sphere and not wholly // contained in a subsphere foreach (CollisionShape obstacle in intersectingShapes) { if (obstacle.timeStamp == timeStamp) { continue; } collisionTestCount++; parms.swapped = false; if (Primitives.TestCollisionFunctions[(int)part.ShapeType(), (int)obstacle.ShapeType()] (part, obstacle, parms)) { parms.part = part; parms.obstacle = obstacle; return(true); } else { obstacle.timeStamp = timeStamp; } } // Now iterate over subspheres for (int i = 0; i < SphereTreeNode.childCount; i++) { SphereTreeNode cs = children[i]; if (cs == null) { continue; } // Skip any sphere that doesn't overlap the part in question if (!cs.SphereOverlap(part)) { continue; } if (cs.TestSphereCollision(part, timeStamp, ref collisionTestCount, parms)) { return(true); } } return(false); }