Esempio n. 1
0
        private static void TestSAP1D()
        {
            ISweepAndPrune <Object> sap = new SweepAndPrune1D <Object>(true);

            List <Object> objects = new List <Object>();

            objects.Add(ObjectFromPositionRadius(new Vector2(1, 1), 1));
            objects.Add(ObjectFromPositionRadius(new Vector2(0, 0), 1));
            objects.Add(ObjectFromPositionRadius(new Vector2(-1, 2), 3));
            objects.Add(ObjectFromPositionRadius(new Vector2(-1, -1), 0.5f));
            objects.Add(ObjectFromPositionRadius(new Vector2(10, 10), 1));
            objects.Add(ObjectFromPositionRadius(new Vector2(9, 9), 2));

            foreach (var o in objects)
            {
                sap.AddObject(o);
            }

            sap.Resolve();

            foreach (var o in objects)
            {
                Console.Write(o + " intersects with ");
                if (o.Intersectees.Count > 0)
                {
                    for (int i = 0; i < o.Intersectees.Count; i++)
                    {
                        Console.Write(o.Intersectees[i] + (i == o.Intersectees.Count - 1 ? Environment.NewLine : ", "));
                    }
                }
                else
                {
                    Console.WriteLine("nothing");
                }
            }
        }
Esempio n. 2
0
        private static void CompareSAP1DFunctionalities()
        {
            int     nObjects  = 500;
            Vector2 minPoint  = new Vector2(-100, -100);
            Vector2 maxPoint  = new Vector2(100, 100);
            float   minRadius = 0.2f;
            float   maxRadius = 4f;

            //ISAP1D<Object> sap = new SweepAndPrune1D<Object>(false);
            ISweepAndPrune <Object> sap  = new SweepAndPrune2D <Object>(false);
            ISweepAndPrune <Object> sap2 = new SweepAndPrune1D <Object>(false);

            //List<Object> objects = new List<Object>();

            Object[] objects  = new Object[nObjects];
            Object[] objects2 = new Object[nObjects];
            for (int i = 0; i < nObjects; i++)
            {
                objects[i] = RandomObject(minPoint, maxPoint, minRadius, maxRadius);
            }
            for (int i = 0; i < nObjects; i++)
            {
                objects2[i] = new Object(objects[i]);
            }

            //Object[] objects2 = new List<Object>(objects).ToArray();

            foreach (var o in objects)
            {
                sap.AddObject(o);
            }
            foreach (var o in objects2)
            {
                sap2.AddObject(o);
            }

            sap.Resolve();
            sap2.Resolve();
            Console.WriteLine("Total collisions (I / II): " + CountIntersections(objects) + " / " + CountIntersections(objects2));
            Console.WriteLine("According to brute force: " + BruteForceResolve(new List <Object>(objects)));

            for (int i = 0; i < objects.Length; i++)
            {
                if (Object.ReferenceEquals(objects[i], objects2[i]))
                {
                    throw new Exception("Not clones, same objects!");
                }
                //if (objects[i].Intersectees.Count != objects2[i].Intersectees.Count)
                //    throw new Exception("Algorithms are not returning the same results!");

                for (int j = 0; j < objects2[i].Intersectees.Count; j++)
                {
                    var o = objects2[i].Intersectees[j];
                    if (!objects[i].Intersectees.Contains(o) && !objects[((Object)o).ID - 100000].Intersectees.Contains(objects[i]))
                    {
                        Console.WriteLine("Two returned intersection for " + VersatileObject(objects2[i]) + " and " + VersatileObject((Object)objects2[i].Intersectees[j]));
                        //throw new Exception("Not equal!");
                        System.Diagnostics.Debugger.Break();
                    }
                }
            }
            Console.WriteLine("Everything went fine.");
        }