Example #1
0
        private void SetupColliders()
        {
            float delta     = 0.03f;
            float space     = 0.01f;
            float distance2 = space / 2;
            float size      = delta - space;
            int   i         = 0;

            for (float x = -0.9f; x < 0.9f; x += delta)
            {
                for (float y = -0.9f; y < 0.9f; y += delta)
                {
                    var collider = new Collider(x, y, size, size)
                    {
                        Velocity = RandomVectors.Velocity()
                    };
                    colliders.Add(collider);
                    ++i;
                }
            }
            float scale = 2f;

            collisionGrid       = new CollisionGrid(windowBorders, size * scale, size * scale);
            collisionGridCenter = new CollisionGridCenter(windowBorders, size * scale, size * scale);
        }
Example #2
0
        public static HashSet <(ICollider, ICollider)> FindAllCollisions(this CollisionGrid <ICollider> collisionGrid, IColliderProvider scene)
        {
            var result = new HashSet <(ICollider, ICollider)>();

            collisionGrid.Clear();
            foreach (var collider in scene.Collider)
            {
                collisionGrid.Add(collider);
            }
            collisionGrid.FindAllCollisions((c1, c2) => CollisionDetection.ExactCollision(result, c1, c2));
            return(result);
        }
Example #3
0
 public CollisionAlgoDebug()
 {
     collisionGrid = new CollisionGrid <ICollider>(-1f, -1f, 2f, 2f, 32, 32);
     result        = new HashSet <(ICollider, ICollider)>();
 }
Example #4
0
        private static void Main(string[] _)
        {
            var size       = (uint)(VideoMode.DesktopMode.Height * 0.8f);
            var gameWindow = new GameWindow(GameWindowSettings.Default, new NativeWindowSettings {
                Profile = ContextProfile.Compatability
            })
            {
                IsVisible = false
            };
            // required before RenderWindow construction for OpenGL use
            var window = new RenderWindow(new VideoMode(size, size), "Collision Grid");

            window.Closed += (_1, _2) => window.Close();
            window.SetKeyRepeatEnabled(false);
            window.SetVerticalSyncEnabled(true);

            var scene              = new SceneAdapter(8000, 0.007f, 0.004f);
            var parameters         = new Parameters();
            var collisionDetection = new CollisionDetection(scene);

            scene.OnChange += (_1, _2) => collisionDetection.Update();

            var style = new LightStyle();

            var ui = new Ui(window, style);

            void RecreateUi()
            {
                ui.Clear();
                ui.AddPropertyGrid(parameters);
                ui.AddPropertyGrid(scene);
                ui.AddPropertyGrid(collisionDetection);
                if (collisionDetection.Algorithm is ICollisionGrid <ICollider> collGrid)
                {
                    foreach (var grid in collGrid.Grids.Reverse())
                    {
                        ui.AddCountGrid(grid);
                    }
                }
            }

            RecreateUi();
            collisionDetection.OnUpdate += (_1, _2) => RecreateUi();

            var view = new View(style);

            window.Resized += (_1, a) => View.Resize((int)a.Width, (int)a.Height);
            //window.Resized += (_1, a) => ui.Resize((int)a.Width, (int)a.Height);

            bool drawUi            = true;
            bool drawVisualization = true;

            window.KeyPressed += (_1, a) =>
            {
                switch (a.Code)
                {
                case Keyboard.Key.Escape: window.Close(); break;

                case Keyboard.Key.Tab: drawUi = !drawUi; break;

                case Keyboard.Key.V: drawVisualization = !drawVisualization; break;
                }
            };

            var refCollisionAlgo = new CollisionGrid <ICollider>(-1f, -1f, 2f, 2f, 32, 32);
            IEnumerable <ICollider> highlight = Enumerable.Empty <ICollider>();
            var clock = new Clock();

            while (window.IsOpen)
            {
                window.DispatchEvents();
                var deltaTime = clock.Restart().AsSeconds();
                scene.Update(deltaTime);

                if (parameters.CollisionDetection)
                {
                    var collidingSet = collisionDetection.FindCollisions();
                    if (parameters.DebugAlgo)
                    {
                        var result = refCollisionAlgo.FindAllCollisions(scene);
                        collidingSet.SymmetricExceptWith(result);
                        highlight = collidingSet.Flatten();
                    }
                    else
                    {
                        highlight = collidingSet.Flatten();
                    }
                }

                view.Draw(scene.Collider, highlight, parameters.DebugAlgo);

                window.PushGLStates();
                if (drawVisualization)
                {
                    ui.DrawVisualization();
                }
                if (drawUi)
                {
                    ui.DrawUi();
                }
                window.PopGLStates();
                window.Display();                 //buffer swap for double buffering and wait for next frame
            }
        }