// Test for collision with any object in this sphere public void FindIntersectingShapes(CollisionShape part, List <CollisionShape> shapes, 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)) { shapes.Add(containedShape); } 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)) { shapes.Add(obstacle); } 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; } cs.FindIntersectingShapes(part, shapes, timeStamp, ref collisionTestCount, parms); } }
public static void CreateRenderedNodes(CollisionShape shape, int id, ref List <RenderedNode> renderedNodes) { // To keep all the rendering stuff internal to this file, we // reach inside of collision objects to create the rendered // shapes if (renderedNodes == null) { renderedNodes = new List <RenderedNode>(); } switch (shape.ShapeType()) { case ShapeEnum.ShapeSphere: renderedNodes.Add(NewRenderedSphere(id, 0, shape.center, shape.radius * MO.DivMeter)); break; case ShapeEnum.ShapeCapsule: CollisionCapsule c = (CollisionCapsule)shape; float r = c.capRadius * MO.DivMeter; renderedNodes.Add(NewRenderedSphere(id, 0, c.bottomcenter, r)); renderedNodes.Add(NewRenderedSphere(id, 1, c.topcenter, r)); Vector3 seg = (c.topcenter - c.bottomcenter); renderedNodes.Add(NewRenderedObject("unit_cylinder.mesh", id, 2, c.center, new Vector3(r, seg.Length * MO.DivMeter, r), new Vector3(0f, 1f, 0f).GetRotationTo(seg))); break; case ShapeEnum.ShapeAABB: renderedNodes.Add(NewRenderedBox(id, 0, ((CollisionAABB)shape).OBB())); break; case ShapeEnum.ShapeOBB: renderedNodes.Add(NewRenderedBox(id, 0, (CollisionOBB)shape)); break; } }
// This function swaps the arguments so we only have to // implement the intersection function once public static bool TestCollisionSwapper(CollisionShape s1, CollisionShape s2, CollisionParms parms) { parms.swapped = true; return TestCollisionFunctions[(int)s2.ShapeType(), (int)s1.ShapeType()] (s2, s1, 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; }