/// <summary> /// Main function for testing mesh-mesh collision /// </summary> /// <param name="_first"></param> /// <param name="_second"></param> /// <returns></returns> public bool DetectCollision(GeometricObject _first, GeometricObject _second, double tolerance) { List<Polygon> polygons1 = _first.Polygons; List<Polygon> polygons2 = _second.Polygons; var both = new List<Polygon>(polygons1); both.AddRange(polygons2); BSPNode bspNode = null; int max = BSPNode.Depth; Plane p = Plane.Empty; if (polygons1.Count < polygons2.Count) { bspNode = BSPNode.BuildBSPTree(ref both, 3, _first.Polygons[0].Points[0].ID, p); } else { bspNode = BSPNode.BuildBSPTree(ref both, 3, _second.Polygons[0].Points[0].ID, p); } if (max < BSPNode.Depth) { Console.WriteLine(BSPNode.Depth); } return bspNode != null; }
/// <summary> /// Main function for testing mesh-mesh collision /// </summary> /// <param name="_first"></param> /// <param name="_second"></param> /// <returns></returns> public static bool TestBspCollision(GeometricObject _first, GeometricObject _second) { List<Polygon> polygons1 = _first.Polygons; List<Polygon> polygons2 = _second.Polygons; var both = new List<Polygon>(polygons1); both.AddRange(polygons2); BSPNode bspNode = null; int max = BSPNode.Depth; if (polygons1.Count < polygons2.Count) { bspNode = BSPNode.BuildBSPTree(ref both, 3, _first.Polygons[0].Points[0].ID); } else { bspNode = BSPNode.BuildBSPTree(ref both, 3, _second.Polygons[0].Points[0].ID); } if (max < BSPNode.Depth) { Console.WriteLine(BSPNode.Depth); } return bspNode != null; }
/// <summary> /// Performs an overlap test between two elements. /// </summary> /// <param name="_first">first element</param> /// <param name="_second">second element</param> /// <param name="_overlapTolerance">overlap threshold</param> /// <returns>true if two objects overlap more than given tolerance</returns> public static bool TestOverlap(GeometricObject _first, GeometricObject _second, double _overlapTolerance) { AABox A = _first.BoundingBox; AABox B = _second.BoundingBox; // Matrix m = new Matrix(); Matrix fgi = Matrix.TransposeMatrix(_first.GeometryMatrix); Vector3 fgp = _first.Position; fgi.Translate( //quick inverse of position new Vector3(-fgi.M33*fgp.X - fgi.M34*fgp.Y - fgi.M24*fgp.Z, -fgi.M32*fgp.X - fgi.M33*fgp.Y - fgi.M34*fgp.Z, -fgi.M31*fgp.X - fgi.M32*fgp.Y - fgi.M33*fgp.Z)); Matrix mB2A = Matrix.Multiply(fgi, _second.GlobalTransformation); bool isCollision = BoxCollision.BoxOverlapTest(A, B, mB2A, _overlapTolerance); return isCollision; }
public bool DetectCollision(GeometricObject obj1, GeometricObject obj2, double tolerance) { return BodiesIntersect(obj1.Points, obj2.Points); }
/// <summary> /// Main function, that tests if two geometricObjects collide /// </summary> /// <param name="_obj1"></param> /// <param name="_obj2"></param> /// <returns></returns> public static bool CollisionTest(GeometricObject _obj1, GeometricObject _obj2) { bool isCollision = false; Options.CollisionStage collisionLevel = Options.CollisionLevel; foreach (ICollisionDetector detector in collisionDetectors) { if (AcceptTest(collisionLevel, detector.CollisionLevel)) { // CollisionTest test = new CollisionTest(detector.Name, _obj1, _obj2); //test.Start(); if (detector.DetectCollision(_obj1, _obj2, Tolerance)) { isCollision = true; //break; } else { isCollision = false; } //test.Stop(isCollision); //CollisionTestsManager.AddTest(test); } } if (isCollision) { AddCollidedObject(_obj1); AddCollidedObject(_obj2); } return isCollision; }
/// <summary> /// Adds collided object /// </summary> /// <param name="obj"></param> private static void AddCollidedObject(GeometricObject obj) { if (!collidedObjects.Contains(obj)) { collidedObjects.Add(obj); } }
/// <summary> /// Adding the mesh /// </summary> /// <param name="_name"></param> /// <returns></returns> public bool Add(string _name) { var obj = new GeometricObject(_name, new Vector3(0, 0, 0), Matrix.Identity, new Vector3(0, 0, 0)); if (_name.Equals("torus")) { obj.Mesh = Mesh.Torus(device, 2.0f, 3.5f, 8, 8); //obj.ScaleMatrix.Scale(50.0f, 50.0f, 50.0f); } else if (_name.Equals("torus2")) { obj.Mesh = Mesh.Torus(device, 1.0f, 4.5f, 10, 10); } else if (_name.Equals("sphere")) { obj.Mesh = Mesh.Sphere(device, 5, 8, 8); } else if (_name.Equals("box")) { obj.Mesh = Mesh.Box(device, 4, 3, 2); } else if (_name.Equals("polygon")) { obj.Mesh = Mesh.Polygon(device, 10, 4); } else if (_name.Equals("teapot")) { obj.Mesh = Mesh.Teapot(device); obj.ScaleMatrix.Scale(50.0f, 50.0f, 50.0f); } if (obj.Mesh != null) { objects.Add(obj); SpaceApplication.Options.ListObjects.Items.Add(obj); activeObject = obj; return true; } return false; }
public bool RemoveObject(GeometricObject _obj) { objects.Remove(_obj); ActiveObject = GetObject(0); return true; }
/// <summary> /// Adding the mesh /// </summary> /// <param name="_name"></param> /// <returns></returns> public bool AddTorus(double innerRadius, double outerRadius, int sides, int rings) { var obj = new GeometricObject("torus", new Vector3(0, 0, 0), Matrix.Identity, new Vector3(0, 0, 0)); obj.Mesh = Mesh.Torus(device, (float)innerRadius, (float) outerRadius, sides, rings); return this.AddObject(obj); }
/// <summary> /// Adding the mesh /// </summary> /// <param name="_name"></param> /// <returns></returns> public bool AddSphere(double radius, int slices, int stacks) { var obj = new GeometricObject("sphere", new Vector3(0, 0, 0), Matrix.Identity, new Vector3(0, 0, 0)); obj.Mesh = Mesh.Sphere(device, (float)radius, slices, stacks); return this.AddObject(obj); }
/// <summary> /// Adding the objects to the collection /// </summary> /// <param name="_obj"></param> /// <returns></returns> public bool AddObject(GeometricObject _obj) { if (_obj.Mesh != null) { objects.Add(_obj); SpaceApplication.Options.ListObjects.Items.Add(_obj); activeObject = _obj; activeObject.IsChanged = true; return true; } return false; }
public bool DetectCollision(GeometricObject obj1, GeometricObject obj2, double tolerance) { return TestOverlap(obj1, obj2, tolerance); }