Esempio n. 1
0
        /// <summary>
        /// Constructs a new demo.
        /// </summary>
        /// <param name="game">Game owning this demo.</param>
        public BroadPhaseRemovalTestDemo(DemosGame game)
            : base(game)
        {
            Entity toAdd;

            //BoundingBox box = new BoundingBox(new Vector3(-5, 1, 1), new Vector3(5, 7, 7));
            BEPUutilities.BoundingBox box = new BEPUutilities.BoundingBox(new BEPUutilities.Vector3(-500, -500, -500), new BEPUutilities.Vector3(500, 500, 500));

            DynamicHierarchy dh = new DynamicHierarchy();

            Random rand = new Random(0);

            RawList <Entity> entities = new RawList <Entity>();

            for (int k = 0; k < 1000; k++)
            {
                BEPUutilities.Vector3 position = new BEPUutilities.Vector3((Fix64)rand.NextDouble() * (box.Max.X - box.Min.X) + box.Min.X,
                                                                           (Fix64)rand.NextDouble() * (box.Max.Y - box.Min.Y) + box.Min.Y,
                                                                           (Fix64)rand.NextDouble() * (box.Max.Z - box.Min.Z) + box.Min.Z);
                toAdd = new Box(position, 1, 1, 1, 1);

                entities.Add(toAdd);
            }

            testResults = new double[2];
            int runCount = 10;

            for (int k = 0; k < runCount; k++)
            {
                for (int i = 0; i < entities.Count; i++)
                {
                    dh.Add(entities[i].CollisionInformation);
                }

                long start = Stopwatch.GetTimestamp();
                for (int i = 0; i < entities.Count; i++)
                {
                    //dh.RemoveFast(entities[i].CollisionInformation);
                }
                long end = Stopwatch.GetTimestamp();
                testResults[0] += (end - start) / (double)Stopwatch.Frequency;



                for (int i = 0; i < entities.Count; i++)
                {
                    dh.Add(entities[i].CollisionInformation);
                }

                start = Stopwatch.GetTimestamp();
                for (int i = 0; i < entities.Count; i++)
                {
                    //dh.RemoveBrute(entities[i].CollisionInformation);
                }
                end             = Stopwatch.GetTimestamp();
                testResults[1] += (end - start) / (double)Stopwatch.Frequency;
            }
            testResults[0] /= runCount;
            testResults[1] /= runCount;
        }
Esempio n. 2
0
        public static BoundingBox Convert(BEPUutilities.BoundingBox boundingBox)
        {
            BoundingBox toReturn;

            Convert(ref boundingBox.Min, out toReturn.Min);
            Convert(ref boundingBox.Max, out toReturn.Max);
            return(toReturn);
        }
Esempio n. 3
0
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            //Set up drawers
            ModelDrawer = new InstancedModelDrawer(this);

            //Create the space itself
            Space = new Space();

            var parallelLooper = new ParallelLooper();

            //This section lets the engine know that it can make use of multithreaded systems
            //by adding threads to its thread pool.
            if (Environment.ProcessorCount > 1)
            {
                for (int i = 0; i < Environment.ProcessorCount; i++)
                {
                    parallelLooper.AddThread();
                }
            }

            Space = new Space(parallelLooper);

            Space.ForceUpdater.Gravity = new Vector3(0, -9.81f, 0);
            Space.Add(new Box(Vector3.Zero, 30, 1, 30));

            //Create a bunch of boxes randomly.

            Random      random      = new Random();
            int         boxCount    = 100;
            BoundingBox spawnVolume = new BoundingBox(new Vector3(-10, 10, -10), new Vector3(10, 30, 10));

            for (int i = 0; i < boxCount; i++)
            {
                Vector3 position = new Vector3((float)(random.NextDouble() - 0.5f) * (spawnVolume.Max.X - spawnVolume.Min.X),
                                               (float)(random.NextDouble() - 0.5f) * (spawnVolume.Max.Y - spawnVolume.Min.Y),
                                               (float)(random.NextDouble() - 0.5f) * (spawnVolume.Max.Z - spawnVolume.Min.Z)) +
                                   (spawnVolume.Min + spawnVolume.Max) / 2;
                Space.Add(new Box(position, 1, 1, 1, 1));
            }


            #region DisplayObject creation
            foreach (Entity e in Space.Entities)
            {
                if ((string)e.Tag != "noDisplayObject")
                {
                    ModelDrawer.Add(e);
                }
                else//Remove the now unnecessary tag.
                {
                    e.Tag = null;
                }
            }
            #endregion
        }
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            //Set up drawers
            modelDrawer = new InstancedModelDrawer(this);


            //Create the space and tell it that it should keep track of buffered states.  This will let the
            //positions/orientations of entities be interpolated, producing a cleaner appearance.
            Space = new Space();
            Space.BufferedStates.Enabled = true;


            Space.ForceUpdater.Gravity = new Vector3(0, -9.81f, 0);
            Space.Add(new Box(Vector3.Zero, 30, 1, 30));  //Make the ground

            //Create a bunch of boxes randomly.

            Random random      = new Random();
            int    boxCount    = 50;
            var    spawnVolume = new BoundingBox(new Vector3(-5, 15, -5), new Vector3(5, 25, 5));

            for (int i = 0; i < boxCount; i++)
            {
                var position = new Vector3((float)(random.NextDouble() - 0.5f) * (spawnVolume.Max.X - spawnVolume.Min.X),
                                           (float)(random.NextDouble() - 0.5f) * (spawnVolume.Max.Y - spawnVolume.Min.Y),
                                           (float)(random.NextDouble() - 0.5f) * (spawnVolume.Max.Z - spawnVolume.Min.Z)) +
                               (spawnVolume.Min + spawnVolume.Max) / 2;
                Space.Add(new Box(position, 1, 1, 1, 1));
            }


            //Start up the physics loop.
            physicsThread = new Thread(PhysicsLoop);
            physicsThread.IsBackground = true;
            physicsThread.Start();

            #region DisplayObject creation
            foreach (Entity e in Space.Entities)
            {
                if ((string)e.Tag != "noDisplayObject")
                {
                    modelDrawer.Add(e);
                }
                else//Remove the now unnecessary tag.
                {
                    e.Tag = null;
                }
            }
            #endregion
        }
Esempio n. 5
0
        static BEPUutilities.BoundingBox[] GetBEPUQueryLocations(int count, BoundingBox bounds, Vector3 size)
        {
            Random random = new Random(5);

            BEPUutilities.BoundingBox[] boxes = new BEPUutilities.BoundingBox[count];
            var range = bounds.Max - bounds.Min;

            for (int i = 0; i < boxes.Length; ++i)
            {
                boxes[i].Min = new BEPUutilities.Vector3(
                    bounds.Min.X + ((float)random.NextDouble()) * range.X,
                    bounds.Min.Y + ((float)random.NextDouble()) * range.Y,
                    bounds.Min.Z + ((float)random.NextDouble()) * range.Z);
                boxes[i].Max = boxes[i].Min + new BEPUutilities.Vector3(size.X, size.Y, size.Z);
            }
            return(boxes);
        }
Esempio n. 6
0
 public static BoundingBox ToSharp(this BEPUutilities.BoundingBox bb)
 {
     return(new BoundingBox(bb.Min.ToSharp(), bb.Max.ToSharp()));
 }
Esempio n. 7
0
 public float SphereRadFor(BEPUutilities.BoundingBox bb)
 {
     return((float)(new Location(bb.Max - bb.Min).BiggestValue()));
 }
Esempio n. 8
0
 public static void Convert(ref BoundingBox boundingBox, out BEPUutilities.BoundingBox bepuBoundingBox)
 {
     Convert(ref boundingBox.Min, out bepuBoundingBox.Min);
     Convert(ref boundingBox.Max, out bepuBoundingBox.Max);
 }
Esempio n. 9
0
        static BEPUutilities.BoundingBox[] GetBEPUQueryLocations(int count, BoundingBox bounds, Vector3 size)
        {
            Random random = new Random(5);
            BEPUutilities.BoundingBox[] boxes = new BEPUutilities.BoundingBox[count];
            var range = bounds.Max - bounds.Min;
            for (int i = 0; i < boxes.Length; ++i)
            {

                boxes[i].Min = new BEPUutilities.Vector3(
                    bounds.Min.X + ((float)random.NextDouble()) * range.X,
                    bounds.Min.Y + ((float)random.NextDouble()) * range.Y,
                    bounds.Min.Z + ((float)random.NextDouble()) * range.Z);
                boxes[i].Max = boxes[i].Min + new BEPUutilities.Vector3(size.X, size.Y, size.Z);
            }
            return boxes;
        }
Esempio n. 10
0
        public static void TestBEPU(TestCollidableBEPU[] leaves, BEPUutilities.BoundingBox[] queries, int queryCount, int selfTestCount, int refitCount)
        {
            GC.Collect();
            {
                var warmLeaves = GetLeavesBEPU(2, 2, 2, 10, 10);
                BoundingBoxTree <TestCollidableBEPU> tree = new BoundingBoxTree <TestCollidableBEPU>(warmLeaves);
                Console.WriteLine($"BEPU Cachewarm Build, root AABB: {tree.BoundingBox}");

                tree.Refit();

                RawList <TestCollidableBEPU> results = new RawList <TestCollidableBEPU>();
                BEPUutilities.BoundingBox    aabb    = new BEPUutilities.BoundingBox {
                    Min = new BEPUutilities.Vector3(0, 0, 0), Max = new BEPUutilities.Vector3(1, 1, 1)
                };

                results.Count = 0;
                tree.GetOverlaps(aabb, results);

                var overlaps = new List <TreeOverlapPair <TestCollidableBEPU, TestCollidableBEPU> >(100);
                for (int i = 0; i < selfTestCount; ++i)
                {
                    tree.GetOverlaps(tree, overlaps);
                }
            }
            GC.Collect();

            {
                var startTime = Stopwatch.GetTimestamp() / (double)Stopwatch.Frequency;
                BoundingBoxTree <TestCollidableBEPU> tree = new BoundingBoxTree <TestCollidableBEPU>(leaves);
                var endTime = Stopwatch.GetTimestamp() / (double)Stopwatch.Frequency;
                Console.WriteLine($"BEPU Build Time: {endTime - startTime}, root AABB: {tree.BoundingBox}");

                startTime = Stopwatch.GetTimestamp() / (double)Stopwatch.Frequency;
                for (int i = 0; i < refitCount; ++i)
                {
                    tree.Refit();
                }
                endTime = Stopwatch.GetTimestamp() / (double)Stopwatch.Frequency;
                Console.WriteLine($"BEPU Refit Time: {endTime - startTime}");

                RawList <TestCollidableBEPU> results = new RawList <TestCollidableBEPU>();
                var queryMask = queries.Length - 1;
                startTime = Stopwatch.GetTimestamp() / (double)Stopwatch.Frequency;
                for (int i = 0; i < queryCount; ++i)
                {
                    results.Count = 0;
                    tree.GetOverlaps(queries[i & queryMask], results);
                }
                endTime = Stopwatch.GetTimestamp() / (double)Stopwatch.Frequency;
                Console.WriteLine($"BEPU Query Time: {endTime - startTime}, overlaps: {results.Count}");

                //var overlaps = new RawList<TreeOverlapPair<TestCollidableBEPU, TestCollidableBEPU>>(280000);
                //startTime = Stopwatch.GetTimestamp() / (double)Stopwatch.Frequency;
                //for (int i = 0; i < selfTestCount; ++i)
                //{
                //    overlaps.Count = 0;
                //    tree.GetOverlaps(tree, overlaps);
                //}
                //endTime = Stopwatch.GetTimestamp() / (double)Stopwatch.Frequency;
                //Console.WriteLine($"BEPU SelfTree Time: {endTime - startTime}, overlaps: {overlaps.Count}");
            }
        }
Esempio n. 11
0
        public static void TestBEPU(TestCollidableBEPU[] leaves, BEPUutilities.BoundingBox[] queries, int queryCount, int selfTestCount, int refitCount)
        {
            GC.Collect();
            {
                var warmLeaves = GetLeavesBEPU(2, 2, 2, 10, 10);
                BoundingBoxTree<TestCollidableBEPU> tree = new BoundingBoxTree<TestCollidableBEPU>(warmLeaves);
                Console.WriteLine($"BEPU Cachewarm Build, root AABB: {tree.BoundingBox}");

                tree.Refit();

                RawList<TestCollidableBEPU> results = new RawList<TestCollidableBEPU>();
                BEPUutilities.BoundingBox aabb = new BEPUutilities.BoundingBox { Min = new BEPUutilities.Vector3(0, 0, 0), Max = new BEPUutilities.Vector3(1, 1, 1) };

                results.Count = 0;
                tree.GetOverlaps(aabb, results);

                var overlaps = new List<TreeOverlapPair<TestCollidableBEPU, TestCollidableBEPU>>(100);
                for (int i = 0; i < selfTestCount; ++i)
                {
                    tree.GetOverlaps(tree, overlaps);
                }
            }
            GC.Collect();

            {

                var startTime = Stopwatch.GetTimestamp() / (double)Stopwatch.Frequency;
                BoundingBoxTree<TestCollidableBEPU> tree = new BoundingBoxTree<TestCollidableBEPU>(leaves);
                var endTime = Stopwatch.GetTimestamp() / (double)Stopwatch.Frequency;
                Console.WriteLine($"BEPU Build Time: {endTime - startTime}, root AABB: {tree.BoundingBox}");

                startTime = Stopwatch.GetTimestamp() / (double)Stopwatch.Frequency;
                for (int i = 0; i < refitCount; ++i)
                {
                    tree.Refit();
                }
                endTime = Stopwatch.GetTimestamp() / (double)Stopwatch.Frequency;
                Console.WriteLine($"BEPU Refit Time: {endTime - startTime}");

                RawList<TestCollidableBEPU> results = new RawList<TestCollidableBEPU>();
                var queryMask = queries.Length - 1;
                startTime = Stopwatch.GetTimestamp() / (double)Stopwatch.Frequency;
                for (int i = 0; i < queryCount; ++i)
                {
                    results.Count = 0;
                    tree.GetOverlaps(queries[i & queryMask], results);
                }
                endTime = Stopwatch.GetTimestamp() / (double)Stopwatch.Frequency;
                Console.WriteLine($"BEPU Query Time: {endTime - startTime}, overlaps: {results.Count}");

                //var overlaps = new RawList<TreeOverlapPair<TestCollidableBEPU, TestCollidableBEPU>>(280000);
                //startTime = Stopwatch.GetTimestamp() / (double)Stopwatch.Frequency;
                //for (int i = 0; i < selfTestCount; ++i)
                //{
                //    overlaps.Count = 0;
                //    tree.GetOverlaps(tree, overlaps);
                //}
                //endTime = Stopwatch.GetTimestamp() / (double)Stopwatch.Frequency;
                //Console.WriteLine($"BEPU SelfTree Time: {endTime - startTime}, overlaps: {overlaps.Count}");
            }
        }
Esempio n. 12
0
        protected override void Update(GameTime gameTime)
        {
            _keyboardState = Keyboard.GetState();
            MouseState     = Mouse.GetState();
            Entity mainEntity = _manager.MainPlayer.Ball.Form;

            //Managing the different interactions following the current state of the game
            if (_launched)
            {
                //Updating the camera
                Camera.Update((float)gameTime.ElapsedGameTime.TotalSeconds);

                if (_keyboardState.IsKeyDown(Keys.Escape))
                {
                    //Quit using Escape
                    Exit();
                    return;
                }

                //Managing the loading of the shot
                if (!_manager.MainPlayer.Ball.IsMoving())
                {
                    if (MouseState.LeftButton == ButtonState.Pressed)
                    {
                        if (_chargeBar.Charge <= _chargeBar.ChargeMax)
                        {
                            _chargeBar.Charge += 0.1f * (float)gameTime.ElapsedGameTime.TotalMilliseconds;
                        }
                        if (_chargeBar.Charge >= _chargeBar.ChargeMax)
                        {
                            _chargeBar.Charge = _chargeBar.ChargeMax;
                        }
                    }
                    if (_lastMouseState.LeftButton == ButtonState.Pressed)
                    {
                        //Hitting the ball
                        if (MouseState.LeftButton == ButtonState.Released)
                        {
                            mainEntity.LinearVelocity += Camera.Camera.ViewDirection * _chargeBar.Charge;
                            _sound.Hit(_chargeBar);
                            _chargeBar.Charge = 0;
                            _manager.NbHits++;
                        }
                    }
                }
                else
                {
                    _chargeBar.Charge = 0;
                }
                _lastMouseState = MouseState;

                //Managing the hole and the finish of the course
                if (mainEntity.CollisionInformation.BoundingBox.Intersects(_manager.MainLevel.BoundingArrive) && !_manager.Loading)
                {
                    _sound.Success();
                    BoundingBox box = new BoundingBox(new Vector3(-1, -21, -1), new Vector3(1, -19, 1));
                    mainEntity.CollisionInformation.BoundingBox = box;
                    mainEntity.Position       = Vector3.Zero;
                    mainEntity.LinearVelocity = Vector3.Zero;
                    _manager.LoadNextLevel();
                }

                //Managing a fall and the reset of position in case of bug
                if (mainEntity.Position.Y < -50f || _keyboardState.IsKeyDown(Keys.R))
                {
                    _sound.Out();
                    mainEntity.LinearVelocity = Vector3.Zero;
                    mainEntity.Position       = Vector3.Zero;
                }

                _chargeBar.Update(gameTime);
                _manager.Space.Update();
                base.Update(gameTime);
            }
            //If the game is not launched or ended the start HUD is showed
            else if (!_launched && !_manager.Ended)
            {
                //Using Apos.GUI to show the HUD
                GuiHelper.UpdateSetup();
                _ui.UpdateAll(gameTime);

                // Creating the HUD
                Panel.Push().XY = new Vector2(Graphics.PreferredBackBufferWidth / 2, Graphics.PreferredBackBufferHeight / 2);
                Label.Put("MiniGolf");
                if (Button.Put("Launch Game").Clicked)
                {
                    IsMouseVisible = false;
                    _launched      = true;
                    _sound.PlayAmbiant();
                }
                if (Button.Put("Quit").Clicked)
                {
                    Exit();
                }

                Panel.Pop();


                GuiHelper.UpdateCleanup();
            }

            //Showing the score board at the end using Apos.GUI
            if (_manager.Ended)
            {
                int i = 1;
                GuiHelper.UpdateSetup();
                _ui.UpdateAll(gameTime);

                // Create your UI.
                Panel.Push().XY = new Vector2(Graphics.PreferredBackBufferWidth / 2, Graphics.PreferredBackBufferHeight / 2);
                Label.Put("Course finished");
                Label.Put("Player : " + _manager.MainPlayer.Name);
                foreach (var score in _manager.MainPlayer.Score)
                {
                    Label.Put($"Level {i} : " + score.ToString());
                    i++;
                }
                Label.Put("Press Echap to quit");
                if (Button.Put("Quit").Clicked)
                {
                    Exit();
                    return;
                }

                Panel.Pop();

                // Call UpdateCleanup at the end.
                GuiHelper.UpdateCleanup();

                if (_keyboardState.IsKeyDown(Keys.Escape))
                {
                    Exit();
                    return;
                }
            }
            base.Update(gameTime);
        }
Esempio n. 13
0
 public static BoundingBox Convert(BEPUutilities.BoundingBox b)
 {
     return(new BoundingBox(Convert(b.Min), Convert(b.Max)));
 }