Пример #1
0
        public void TestQuadtreeSplitMerge()
        {
            TestQuadTreeNode root = new TestQuadTreeNode();

            root.NodeData = new QuadTreeNodeData <TestQuadTreeNode>(
                new BoundingBox(new Vector3(-100, -5, -100), new Vector3(100, 5, 100)).dx()
                );

            QuadTree.Split(root, 4);

            QuadTree.Merge(root.NodeData.LowerRight.NodeData.LowerRight.NodeData.UpperLeft);

            QuadTree.MergeRecursive(root.NodeData.LowerLeft.NodeData.UpperRight);

            QuadTree.MergeRecursive(root.NodeData.UpperRight);

            QuadTreeVisualizerXNA visualizer = new QuadTreeVisualizerXNA();

            XNAGame game = new XNAGame();

            game.DrawEvent +=
                delegate
            {
                visualizer.RenderNodeGroundBoundig(game, root);
                visualizer.RenderNodeBoundingBox(game, root);
            };

            game.Run();
        }
Пример #2
0
        public void TestTestSphereShooter()
        {
            var game = new XNAGame();

            game.IsFixedTimeStep = false;
            //game.DrawFps = true;

            PhysicsEngine engine = new PhysicsEngine();

            engine.Initialize();
            var debugRenderer = new PhysicsDebugRendererXNA(game, engine.Scene);

            game.AddXNAObject(debugRenderer);
            game.AddXNAObject(engine);


            ClientPhysicsQuadTreeNode root;

            int numNodes = 20;

            root = new ClientPhysicsQuadTreeNode(
                new BoundingBox(
                    new Vector3(-numNodes * numNodes / 2f, -100, -numNodes * numNodes / 2f),
                    new Vector3(numNodes * numNodes / 2f, 100, numNodes * numNodes / 2f)));

            QuadTree.Split(root, 5);


            var shooter = new TestSphereShooter(game, engine, root, game.SpectaterCamera);

            game.AddXNAObject(shooter);

            var visualizer = new QuadTreeVisualizerXNA();

            game.DrawEvent += delegate
            {
                visualizer.RenderNodeGroundBoundig(game, root,
                                                   delegate(ClientPhysicsQuadTreeNode node, out Color col)
                {
                    col = Color.Green;

                    return(node.PhysicsObjects.Count == 0);
                });

                visualizer.RenderNodeGroundBoundig(game, root,
                                                   delegate(ClientPhysicsQuadTreeNode node, out Color col)
                {
                    col = Color.Orange;

                    return(node.PhysicsObjects.Count > 0);
                });
            };

            game.Run();
        }
Пример #3
0
        public void TestQuadtreeVisualizer()
        {
            TestQuadTreeNode      root       = createSimpleQuadtree();
            QuadTreeVisualizerXNA visualizer = new QuadTreeVisualizerXNA();

            XNAGame game = new XNAGame();

            game.DrawEvent += delegate
            {
                visualizer.RenderNodeGroundBoundig(game, root);
            };

            game.Run();
        }
        public void TestRendererFrustumCulling()
        {
            XNAGame             game     = new XNAGame();
            Vector3             radius   = new Vector3(100, 1000, 100);
            FrustumCullerSimple culler   = new FrustumCullerSimple(new BoundingBox(-radius, radius).dx(), 5);
            SimpleRenderer      renderer = new SimpleRenderer(game, culler);

            game.AddXNAObject(renderer);

            game.DrawFps         = true;
            game.IsFixedTimeStep = false;

            QuadTreeVisualizerXNA visualizer  = new QuadTreeVisualizerXNA();
            List <TestCullObject> cullObjects = new List <TestCullObject>();


            SpectaterCamera cullCam = new SpectaterCamera(game, 10f, 80);

            cullCam.Positie         = new Vector3(8, 10, 8);
            cullCam.EnableUserInput = false;


            SpectaterCamera renderCam = game.SpectaterCamera;


            renderer.CullCamera   = cullCam;
            renderer.RenderCamera = renderCam;

            bool rotate       = true;
            int  selectedNode = -1;



            Seeder seeder = new Seeder(2);

            for (int i = 0; i < 1000; i++)
            {
                Vector3 pos;
                pos.X = seeder.NextFloat(-90, 90);
                pos.Y = seeder.NextFloat(9, 11);
                pos.Z = seeder.NextFloat(-90, 90);

                float iRadius = seeder.NextFloat(0.3f, 2);

                if (seeder.NextInt(0, 2) == 0)
                {
                    SimpleBoxMesh mesh = renderer.CreateBoxMesh();
                    mesh.WorldMatrix = Matrix.CreateTranslation(pos);
                    mesh.Dimensions  = Vector3.One * iRadius;
                    renderer.UpdateRenderable(mesh);
                }
                else
                {
                    SimpleSphereMesh mesh = renderer.CreateSphereMesh();
                    mesh.WorldMatrix = Matrix.CreateTranslation(pos);
                    mesh.Radius      = iRadius;
                    renderer.UpdateRenderable(mesh);
                }
            }


            game.UpdateEvent +=
                delegate
            {
                if (rotate)
                {
                    cullCam.AngleHorizontal += game.Elapsed * MathHelper.Pi * (1 / 8f);
                }

                if (game.Keyboard.IsKeyPressed(Keys.Add))
                {
                    selectedNode++;
                }
                if (game.Keyboard.IsKeyPressed(Keys.Subtract))
                {
                    selectedNode--;
                }

                if (game.Keyboard.IsKeyPressed(Keys.Enter))
                {
                    int count = -1;
                    visualizer.RenderNodeGroundBoundig(game, culler.RootNode,
                                                       delegate(FrustumCuller.CullNode node, out Color col)
                    {
                        col = Color.Red;
                        count++;
                        if (count == selectedNode)
                        {
                            node.Tag = "SELECTED!";
                        }
                        return(count == selectedNode);
                    });
                }

                if (game.Keyboard.IsKeyPressed(Keys.NumPad0))
                {
                    rotate = !rotate;
                }
            };

            game.DrawEvent +=
                delegate
            {
                game.LineManager3D.AddViewFrustum(new BoundingFrustum(cullCam.ViewProjection), Color.Black);
                for (int i = 0; i < cullObjects.Count; i++)
                {
                    game.LineManager3D.AddBox(cullObjects[i].BoundingBox.xna(), Color.Red);
                }
                visualizer.RenderNodeGroundBoundig(game, culler.RootNode,
                                                   delegate(FrustumCuller.CullNode node, out Color col)
                {
                    if (culler.View.IsNodeVisible(node))
                    {
                        col = Color.Orange;
                    }
                    else
                    {
                        col = Color.Green;
                    }

                    return(true);
                });


                /*int count = -1;
                 * visualizer.RenderNodeGroundBoundig(game, culler.RootNode,
                 * delegate(Culler.CullNode node, out Color col)
                 * {
                 *  col = Color.Red;
                 *  count++;
                 *  return count == selectedNode;
                 * });*/
            };



            game.Run();
        }
        public void TestOBJToRAMMeshConverterPerObjectVisual()
        {
            var c = new OBJToRAMMeshConverter(new RAMTextureFactory());


            var importer = new ObjImporter();

            importer.AddMaterialFileStream("Town001.mtl", new FileStream(TestFiles.TownMtl, FileMode.Open));
            importer.ImportObjFile(TestFiles.TownObj);

            var meshes = c.CreateMeshesFromObjects(importer);

            var texturePool           = new TexturePool();
            var meshpartPool          = new MeshPartPool();
            var vertexDeclarationPool = new VertexDeclarationPool();

            var renderer = new SimpleMeshRenderer(texturePool, meshpartPool, vertexDeclarationPool);

            vertexDeclarationPool.SetVertexElements <TangentVertex>(TangentVertex.VertexElements);



            var spheres = new List <ClientPhysicsTestSphere>();
            var engine  = new PhysicsEngine();
            PhysicsDebugRendererXNA debugRenderer = null;

            var builder = new MeshPhysicsActorBuilder(new MeshPhysicsPool());

            TheWizards.Client.ClientPhysicsQuadTreeNode root;

            int numNodes = 20;

            root = new ClientPhysicsQuadTreeNode(
                new BoundingBox(
                    new Vector3(-numNodes * numNodes / 2f, -100, -numNodes * numNodes / 2f),
                    new Vector3(numNodes * numNodes / 2f, 100, numNodes * numNodes / 2f)));

            QuadTree.Split(root, 5);

            var physicsElementFactoryXNA = new MeshPhysicsFactoryXNA(engine, root);
            var physicsElementFactory    = physicsElementFactoryXNA.Factory;

            var physicsElements = new List <MeshStaticPhysicsElement>();

            for (int i = 0; i < 0 * 100 + 1 * meshes.Count; i++)
            {
                var mesh = meshes[i];
                var el   = renderer.AddMesh(mesh);
                el.WorldMatrix = Matrix.CreateTranslation(Vector3.Right * 0 * 2 + Vector3.UnitZ * 0 * 2);

                var pEl = physicsElementFactory.CreateStaticElement(mesh, Matrix.Identity);
                physicsElements.Add(pEl);
            }

            var game = new XNAGame();

            game.IsFixedTimeStep                    = false;
            game.DrawFps                            = true;
            game.SpectaterCamera.FarClip            = 5000;
            game.Graphics1.PreparingDeviceSettings += delegate(object sender, PreparingDeviceSettingsEventArgs e)
            {
                DisplayMode displayMode = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode;
                e.GraphicsDeviceInformation.PresentationParameters.BackBufferFormat = displayMode.Format;
                e.GraphicsDeviceInformation.PresentationParameters.BackBufferWidth  = displayMode.Width;
                e.GraphicsDeviceInformation.PresentationParameters.BackBufferHeight = displayMode.Height;
                game.SpectaterCamera.AspectRatio = displayMode.Width / (float)displayMode.Height;
            };
            game.Graphics1.ToggleFullScreen();

            var sphereMesh = new SphereMesh(0.3f, 20, Color.Green);
            var visualizer = new QuadTreeVisualizerXNA();

            game.AddXNAObject(physicsElementFactoryXNA);

            game.AddXNAObject(texturePool);
            game.AddXNAObject(meshpartPool);
            game.AddXNAObject(vertexDeclarationPool);
            game.AddXNAObject(renderer);


            game.InitializeEvent += delegate
            {
                engine.Initialize();
                debugRenderer = new PhysicsDebugRendererXNA(game, engine.Scene);
                debugRenderer.Initialize(game);
                sphereMesh.Initialize(game);
            };

            bool showPhysics = true;

            game.DrawEvent += delegate
            {
                if (game.Keyboard.IsKeyPressed(Keys.P))
                {
                    showPhysics = !showPhysics;
                }
                if (showPhysics)
                {
                    debugRenderer.Render(game);
                }
                visualizer.RenderNodeGroundBoundig(game, root,
                                                   delegate(ClientPhysicsQuadTreeNode node, out Color col)
                {
                    col = Color.Green;

                    return(node.PhysicsObjects.Count == 0);
                });

                visualizer.RenderNodeGroundBoundig(game, root,
                                                   delegate(ClientPhysicsQuadTreeNode node, out Color col)
                {
                    col = Color.Orange;

                    return(node.PhysicsObjects.Count > 0);
                });

                for (int i = 0; i < physicsElements.Count; i++)
                {
                    var el = physicsElements[i];
                    //game.LineManager3D.AddBox(BoundingBox.CreateFromSphere( el.BoundingSphere), Color.Orange);
                }
                for (int i = 0; i < spheres.Count; i++)
                {
                    sphereMesh.WorldMatrix = Matrix.CreateTranslation(spheres[i].Center);
                    sphereMesh.Render(game);
                }
            };
            game.UpdateEvent += delegate
            {
                engine.Update(game.Elapsed);
                sphereMesh.Update(game);
                if (game.Keyboard.IsKeyPressed(Microsoft.Xna.Framework.Input.Keys.F))
                {
                    var iSphere = new ClientPhysicsTestSphere(engine.Scene,
                                                              game.SpectaterCamera.CameraPosition + game.SpectaterCamera.CameraDirection
                                                              , 0.3f);

                    iSphere.InitDynamic();
                    iSphere.Actor.LinearVelocity = game.SpectaterCamera.CameraDirection * 10;

                    spheres.Add(iSphere);
                }



                for (int i = 0; i < spheres.Count; i++)
                {
                    spheres[i].Update(root, game);
                }
            };

            game.Run();
        }
Пример #6
0
        public void TestPhysicsQuadTreeOrdenObjects()
        {
            ClientPhysicsQuadTreeNode root = CreateTestClientPhysicsQuadtree();

            List <ClientPhysicsTestSphere> spheres = new List <ClientPhysicsTestSphere>();

            spheres.Add(new ClientPhysicsTestSphere(new Vector3(3, 0, 3), 1));
            spheres.Add(new ClientPhysicsTestSphere(new Vector3(33, 0, -83), 1));
            spheres.Add(new ClientPhysicsTestSphere(new Vector3(-25, 0, 40), 1));
            spheres.Add(new ClientPhysicsTestSphere(new Vector3(-25, 0, -35), 1));
            for (int i = 0; i < spheres.Count; i++)
            {
                root.OrdenObject(spheres[i]);
            }


            ClientPhysicsTestSphere movingSphere = new ClientPhysicsTestSphere(Vector3.Zero, 2);
            Curve3D curve = CreateTestObject1MovementCurve();

            float time = 0;

            QuadTreeVisualizerXNA visualizer = new QuadTreeVisualizerXNA();

            XNAGame game = new XNAGame();

            game.UpdateEvent +=
                delegate
            {
                time += game.Elapsed;
                movingSphere.Center = curve.Evaluate(time * (1 / 4f));
                root.OrdenObject(movingSphere);
            };

            game.DrawEvent +=
                delegate
            {
                for (int i = 0; i < spheres.Count; i++)
                {
                    game.LineManager3D.AddCenteredBox(spheres[i].Center, spheres[i].Radius, Color.Red);
                }
                game.LineManager3D.AddCenteredBox(movingSphere.Center, movingSphere.Radius, Color.Red);
                visualizer.RenderNodeGroundBoundig(game, root,
                                                   delegate(ClientPhysicsQuadTreeNode node, out Color col)
                {
                    col = Color.Green;

                    return(node.PhysicsObjects.Count == 0);
                });

                visualizer.RenderNodeGroundBoundig(game, root,
                                                   delegate(ClientPhysicsQuadTreeNode node, out Color col)
                {
                    col = Color.Orange;

                    return(node.PhysicsObjects.Count > 0);
                });
            };



            game.Run();
        }
Пример #7
0
        public void TestPhysicsEnableDisable()
        {
            List <ClientPhysicsTestSphere> spheres = new List <ClientPhysicsTestSphere>();

            ClientPhysicsQuadTreeNode root = CreateTestClientPhysicsQuadtree();



            spheres.Add(new ClientPhysicsTestSphere(new Vector3(0, 0, 0), 2));


            Curve3D curve1 = CreateTestObject1MovementCurve();



            float time         = 0;
            bool  progressTime = true;
            QuadTreeVisualizerXNA visualizer = new QuadTreeVisualizerXNA();

            XNAGame game = new XNAGame();

            game.UpdateEvent +=
                delegate
            {
                if (game.Keyboard.IsKeyPressed(Microsoft.Xna.Framework.Input.Keys.P))
                {
                    progressTime = !progressTime;
                }

                if (progressTime)
                {
                    time += game.Elapsed;
                }


                spheres[0].Move(root, curve1.Evaluate(time * (1 / 4f)));
            };

            game.DrawEvent +=
                delegate
            {
                for (int i = 0; i < spheres.Count; i++)
                {
                    game.LineManager3D.AddCenteredBox(spheres[i].Center, spheres[i].Radius, Color.Red);
                }
                visualizer.RenderNodeGroundBoundig(game, root,
                                                   delegate(ClientPhysicsQuadTreeNode node, out Color col)
                {
                    col = Color.Green;

                    return(node.DynamicObjectsCount == 0 && !node.PhysicsEnabled);
                });
                visualizer.RenderNodeGroundBoundig(game, root,
                                                   delegate(ClientPhysicsQuadTreeNode node, out Color col)
                {
                    col = Color.Red;

                    return(node.DynamicObjectsCount == 0 && node.PhysicsEnabled);
                });

                visualizer.RenderNodeGroundBoundig(game, root,
                                                   delegate(ClientPhysicsQuadTreeNode node, out Color col)
                {
                    col = Color.Yellow;
                    if (node.DynamicObjectsCount == 1)
                    {
                        col = Color.Orange;
                    }

                    return(node.DynamicObjectsCount > 0);
                });

                visualizer.RenderNodeGroundBoundig(game, root,
                                                   delegate(ClientPhysicsQuadTreeNode node, out Color col)
                {
                    col = Color.Purple;

                    return(node.DynamicObjectsCount < 0);
                });
            };



            game.Run();
        }
Пример #8
0
        public void TestOBJToRAMMeshConverterPerObjectVisualCool()
        {
            var textureFactory = new RAMTextureFactory();
            var c = new OBJToRAMMeshConverter(textureFactory);


            var importer = new ObjImporter();

            importer.AddMaterialFileStream("Town001.mtl", new FileStream("../GameData/Town/OBJ03/Town001.mtl", FileMode.Open));
            importer.ImportObjFile("../GameData/Town/OBJ03/Town001.obj");

            var meshes = c.CreateMeshesFromObjects(importer);

            var texturePool           = new TexturePool();
            var meshpartPool          = new MeshPartPool();
            var vertexDeclarationPool = new VertexDeclarationPool();

            var renderer = new SimpleMeshRenderer(texturePool, meshpartPool, vertexDeclarationPool);

            vertexDeclarationPool.SetVertexElements <TangentVertex>(TangentVertex.VertexElements);



            var spheres = new List <ClientPhysicsTestSphere>();
            var engine  = new PhysicsEngine();
            PhysicsDebugRendererXNA debugRenderer = null;

            var root = CreatePhysicsQuadtree(20, 5);

            var physicsElementFactoryXNA = new MeshPhysicsFactoryXNA(engine, root);
            var physicsElementFactory    = physicsElementFactoryXNA.Factory;

            var physicsElements = new List <MeshStaticPhysicsElement>();

            for (int i = 0; i < 0 * 100 + 1 * meshes.Count; i++)
            {
                var mesh = meshes[i];
                var el   = renderer.AddMesh(mesh);
                el.WorldMatrix = Matrix.CreateTranslation(Vector3.Right * 0 * 2 + Vector3.UnitZ * 0 * 2);

                var pEl = physicsElementFactory.CreateStaticElement(mesh, Matrix.Identity);
                physicsElements.Add(pEl);
            }
            var gameMeshes = new List <OBJParserTest.TestGameMesh>();

            var game = new XNAGame();

            game.IsFixedTimeStep                    = false;
            game.DrawFps                            = true;
            game.SpectaterCamera.FarClip            = 5000;
            game.Graphics1.PreparingDeviceSettings += delegate(object sender, PreparingDeviceSettingsEventArgs e)
            {
                DisplayMode displayMode = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode;
                e.GraphicsDeviceInformation.PresentationParameters.BackBufferFormat = displayMode.Format;
                e.GraphicsDeviceInformation.PresentationParameters.BackBufferWidth  = displayMode.Width;
                e.GraphicsDeviceInformation.PresentationParameters.BackBufferHeight = displayMode.Height;
                game.SpectaterCamera.AspectRatio = displayMode.Width / (float)displayMode.Height;
            };
            game.Graphics1.ToggleFullScreen();

            var barrelMesh = OBJParserTest.GetBarrelMesh(c);
            var crateMesh  = OBJParserTest.GetCrateMesh(c);

            var sphereMesh = new SphereMesh(0.3f, 20, Color.Green);
            var visualizer = new QuadTreeVisualizerXNA();

            game.AddXNAObject(physicsElementFactoryXNA);

            game.AddXNAObject(texturePool);
            game.AddXNAObject(meshpartPool);
            game.AddXNAObject(vertexDeclarationPool);
            game.AddXNAObject(renderer);


            game.InitializeEvent += delegate
            {
                engine.Initialize();
                debugRenderer = new PhysicsDebugRendererXNA(game, engine.Scene);
                debugRenderer.Initialize(game);
                sphereMesh.Initialize(game);

                for (int i = 0; i < meshes.Count; i++)
                {
                    var mesh = meshes[i];
                    var data = mesh.GetCollisionData();

                    /*if (data.TriangleMesh != null)
                     *  physicsElementFactory.MeshPhysicsPool.PreloadTriangleMesh(engine.Scene, data.TriangleMesh);*/
                }
            };

            bool showPhysics = true;

            game.DrawEvent += delegate
            {
                if (game.Keyboard.IsKeyPressed(Keys.P))
                {
                    showPhysics = !showPhysics;
                }
                if (showPhysics)
                {
                    debugRenderer.Render(game);
                }

                /*visualizer.RenderNodeGroundBoundig(game, root,
                 *  delegate(ClientPhysicsQuadTreeNode node, out Color col)
                 *  {
                 *      col = Color.Green;
                 *
                 *      return node.PhysicsObjects.Count == 0;
                 *  });
                 *
                 * visualizer.RenderNodeGroundBoundig(game, root,
                 * delegate(ClientPhysicsQuadTreeNode node, out Color col)
                 * {
                 *     col = Color.Orange;
                 *
                 *     return node.PhysicsObjects.Count > 0;
                 * });*/

                for (int i = 0; i < physicsElements.Count; i++)
                {
                    var el = physicsElements[i];
                    //game.LineManager3D.AddBox(BoundingBox.CreateFromSphere( el.BoundingSphere), Color.Orange);
                }
                for (int i = 0; i < spheres.Count; i++)
                {
                    sphereMesh.WorldMatrix = Matrix.CreateTranslation(spheres[i].Center);
                    sphereMesh.Render(game);
                }
            };
            game.UpdateEvent += delegate
            {
                engine.Update(game.Elapsed);
                sphereMesh.Update(game);
                if (game.Keyboard.IsKeyPressed(Microsoft.Xna.Framework.Input.Keys.F))
                {
                    var pEl = physicsElementFactory.CreateDynamicElement(crateMesh,
                                                                         Matrix.CreateTranslation(
                                                                             game.SpectaterCamera.CameraPosition +
                                                                             game.SpectaterCamera.CameraDirection));
                    pEl.Actor.LinearVelocity = game.SpectaterCamera.CameraDirection * 30;

                    var rEl = renderer.AddMesh(crateMesh);


                    gameMeshes.Add(new OBJParserTest.TestGameMesh(rEl, pEl));
                }
                if (game.Keyboard.IsKeyPressed(Microsoft.Xna.Framework.Input.Keys.E))
                {
                    var pEl = physicsElementFactory.CreateDynamicElement(barrelMesh,
                                                                         Matrix.CreateTranslation(
                                                                             game.SpectaterCamera.CameraPosition +
                                                                             game.SpectaterCamera.CameraDirection));
                    pEl.Actor.LinearVelocity = game.SpectaterCamera.CameraDirection * 30;

                    var rEl = renderer.AddMesh(barrelMesh);


                    gameMeshes.Add(new OBJParserTest.TestGameMesh(rEl, pEl));
                }


                for (int i = 0; i < gameMeshes.Count; i++)
                {
                    var m = gameMeshes[i];
                    m.RenderElement.WorldMatrix = m.PhysicsElement.World;
                }
            };

            game.Run();
        }
Пример #9
0
        public void TestThrowBarrels()
        {
            var texFactory = new RAMTextureFactory();
            var c          = new OBJToRAMMeshConverter(texFactory);


            RAMMesh mesh = OBJParserTest.GetBarrelMesh(c);

            var texturePool           = new TexturePool();
            var meshpartPool          = new MeshPartPool();
            var vertexDeclarationPool = new VertexDeclarationPool();

            var renderer = new SimpleMeshRenderer(texturePool, meshpartPool, vertexDeclarationPool);

            vertexDeclarationPool.SetVertexElements <TangentVertex>(TangentVertex.VertexElements);



            var gameMeshes = new List <OBJParserTest.TestGameMesh>();
            var engine     = new PhysicsEngine();
            PhysicsDebugRendererXNA debugRenderer = null;

            var root = CreatePhysicsQuadtree(16, 4);

            var physicsElementFactoryXNA = new MeshPhysicsFactoryXNA(engine, root);
            var physicsElementFactory    = physicsElementFactoryXNA.Factory;

            var game = new XNAGame();

            game.IsFixedTimeStep = false;
            game.DrawFps         = true;

            var visualizer = new QuadTreeVisualizerXNA();

            game.AddXNAObject(physicsElementFactoryXNA);

            game.AddXNAObject(texturePool);
            game.AddXNAObject(meshpartPool);
            game.AddXNAObject(vertexDeclarationPool);
            game.AddXNAObject(renderer);


            game.InitializeEvent += delegate
            {
                engine.Initialize();
                debugRenderer = new PhysicsDebugRendererXNA(game, engine.Scene);
                debugRenderer.Initialize(game);
            };

            bool showPhysics = true;

            game.DrawEvent += delegate
            {
                if (game.Keyboard.IsKeyPressed(Keys.P))
                {
                    showPhysics = !showPhysics;
                }
                if (showPhysics)
                {
                    debugRenderer.Render(game);
                }
                visualizer.RenderNodeGroundBoundig(game, root,
                                                   delegate(ClientPhysicsQuadTreeNode node, out Color col)
                {
                    col = Color.Green;

                    return(node.PhysicsObjects.Count == 0);
                });

                visualizer.RenderNodeGroundBoundig(game, root,
                                                   delegate(ClientPhysicsQuadTreeNode node, out Color col)
                {
                    col = Color.Orange;

                    return(node.PhysicsObjects.Count > 0);
                });
            };
            game.UpdateEvent += delegate
            {
                engine.Update(game.Elapsed);
                if (game.Keyboard.IsKeyPressed(Microsoft.Xna.Framework.Input.Keys.F))
                {
                    var pEl = physicsElementFactory.CreateDynamicElement(mesh,
                                                                         Matrix.CreateTranslation(
                                                                             game.SpectaterCamera.CameraPosition +
                                                                             game.SpectaterCamera.CameraDirection));
                    pEl.Actor.LinearVelocity = game.SpectaterCamera.CameraDirection * 30;

                    var rEl = renderer.AddMesh(mesh);


                    gameMeshes.Add(new OBJParserTest.TestGameMesh(rEl, pEl));
                }

                for (int i = 0; i < gameMeshes.Count; i++)
                {
                    var m = gameMeshes[i];
                    m.RenderElement.WorldMatrix = m.PhysicsElement.World;
                }
            };

            game.Run();
        }
Пример #10
0
        public void TestEntityClientPhysis()
        {
            XNAGame game = new XNAGame();

            Database.Database database = loadDatabaseServices();

            EntityManagerService ems = new EntityManagerService(database);

            BoundingBox bb = new BoundingBox();



            PhysicsEngine           engine        = new PhysicsEngine();
            PhysicsDebugRendererXNA debugRenderer = null;

            TheWizards.Client.ClientPhysicsQuadTreeNode root;
            root = new ClientPhysicsQuadTreeNode(
                new BoundingBox(
                    new Vector3(-16 * 16 / 2f, -100, -16 * 16 / 2f),
                    new Vector3(16 * 16 / 2f, 100, 16 * 16 / 2f)));

            QuadTree.Split(root, 4);


            ClientPhysicsTestSphere sphere = new ClientPhysicsTestSphere(Vector3.Zero, 2);

            Curve3D curve1 = ClientTest.CreateTestObject1MovementCurve();


            QuadTreeVisualizerXNA visualizer = new QuadTreeVisualizerXNA();
            float time = 0;

            List <ClientPhysicsTestSphere> spheres  = new List <ClientPhysicsTestSphere>();
            List <EntityClientPhysics>     entities = new List <EntityClientPhysics>();


            game.InitializeEvent += delegate
            {
                engine.Initialize();
                debugRenderer = new PhysicsDebugRendererXNA(game, engine.Scene);
                debugRenderer.Initialize(game);

                EntityFullData      entityData;
                EntityClientPhysics entPhysics;



                entityData           = CreatePyramidEntity(ems, 5);
                entityData.Transform = new Transformation(
                    Vector3.One, Quaternion.Identity,
                    new Vector3(10, 2, 20));

                entPhysics = new EntityClientPhysics(entityData);
                entPhysics.LoadInClientPhysics(engine.Scene, root);
                entities.Add(entPhysics);

                entityData           = CreatePyramidEntity(ems, 20);
                entityData.Transform = new Transformation(
                    Vector3.One, Quaternion.Identity,
                    new Vector3(-32, 0, -40));

                entPhysics = new EntityClientPhysics(entityData);
                entPhysics.LoadInClientPhysics(engine.Scene, root);
                entities.Add(entPhysics);


                entityData = CreateTwoPyramidEntity(ems, 5, 3);
                entityData.ObjectFullData.Models[0].ObjectMatrix *= Matrix.CreateTranslation(new Vector3(-3, 0, 3));
                entityData.ObjectFullData.Models[1].ObjectMatrix *= Matrix.CreateTranslation(new Vector3(3, 1, 2));
                entityData.Transform = new Transformation(
                    Vector3.One * 2, Quaternion.Identity,
                    new Vector3(80, 0, -45));

                entPhysics = new EntityClientPhysics(entityData);
                entPhysics.LoadInClientPhysics(engine.Scene, root);
                entities.Add(entPhysics);
            };

            game.DrawEvent += delegate
            {
                debugRenderer.Render(game);


                visualizer.RenderNodeGroundBoundig(game, root,
                                                   delegate(ClientPhysicsQuadTreeNode node, out Color col)
                {
                    col = Color.Green;

                    return(node.PhysicsObjects.Count == 0);
                });

                visualizer.RenderNodeGroundBoundig(game, root,
                                                   delegate(ClientPhysicsQuadTreeNode node, out Color col)
                {
                    col = Color.Orange;

                    return(node.PhysicsObjects.Count > 0);
                });
                game.LineManager3D.AddCenteredBox(sphere.Center, sphere.Radius, Color.Red);

                for (int i = 0; i < entities.Count; i++)
                {
                    game.LineManager3D.AddCenteredBox(entities[i].BoundingSphere.Center,
                                                      entities[i].BoundingSphere.Radius * 2, Color.Black);
                }
            };
            game.UpdateEvent += delegate
            {
                time += game.Elapsed;


                sphere.Move(root, curve1.Evaluate(time * (1 / 4f)));


                if (game.Keyboard.IsKeyPressed(Microsoft.Xna.Framework.Input.Keys.F))
                {
                    ClientPhysicsTestSphere iSphere = new ClientPhysicsTestSphere(engine.Scene,
                                                                                  game.SpectaterCamera.CameraPosition + game.SpectaterCamera.CameraDirection
                                                                                  , 1);

                    iSphere.InitDynamic();
                    iSphere.Actor.LinearVelocity = game.SpectaterCamera.CameraDirection * 10;

                    spheres.Add(iSphere);
                }



                for (int i = 0; i < spheres.Count; i++)
                {
                    spheres[i].Update(root, game);
                }



                engine.Update(game.Elapsed);
            };


            game.Run();
        }
Пример #11
0
        public void TestMeshDynamicPhysicsElement()
        {
            XNAGame game = new XNAGame();

            var mesh = new RAMMesh();
            var data = mesh.GetCollisionData();

            var box = new MeshCollisionData.Box();

            box.Dimensions  = Vector3.One * 2;
            box.Orientation = Matrix.Identity;

            data.Boxes.Add(box);


            box             = new MeshCollisionData.Box();
            box.Dimensions  = Vector3.One * 4;
            box.Orientation = Matrix.CreateTranslation(new Vector3(2, 2, 2));

            data.Boxes.Add(box);


            BoundingBox bb = new BoundingBox();



            PhysicsEngine           engine        = new PhysicsEngine();
            PhysicsDebugRendererXNA debugRenderer = null;

            TheWizards.Client.ClientPhysicsQuadTreeNode root;
            root = new ClientPhysicsQuadTreeNode(
                new BoundingBox(
                    new Vector3(-16 * 16 / 2f, -100, -16 * 16 / 2f),
                    new Vector3(16 * 16 / 2f, 100, 16 * 16 / 2f)));

            QuadTree.Split(root, 4);


            var   builder    = new MeshPhysicsActorBuilder(new MeshPhysicsPool());
            var   visualizer = new QuadTreeVisualizerXNA();
            float time       = 0;

            var spheres = new List <MeshDynamicPhysicsElement>();
            var meshes  = new List <MeshStaticPhysicsElement>();

            var physicsElementFactoryXNA = new MeshPhysicsFactoryXNA(engine, root);
            var factory = physicsElementFactoryXNA.Factory;

            game.AddXNAObject(physicsElementFactoryXNA);


            game.InitializeEvent += delegate
            {
                engine.Initialize();
                debugRenderer = new PhysicsDebugRendererXNA(game, engine.Scene);
                debugRenderer.Initialize(game);
            };

            game.DrawEvent += delegate
            {
                debugRenderer.Render(game);


                visualizer.RenderNodeGroundBoundig(game, root,
                                                   delegate(ClientPhysicsQuadTreeNode node, out Color col)
                {
                    col = Color.Green;

                    return(node.PhysicsObjects.Count == 0);
                });

                visualizer.RenderNodeGroundBoundig(game, root,
                                                   delegate(ClientPhysicsQuadTreeNode node, out Color col)
                {
                    col = Color.Orange;

                    return(node.PhysicsObjects.Count > 0);
                });

                for (int i = 0; i < meshes.Count; i++)
                {
                    game.LineManager3D.AddCenteredBox(meshes[i].BoundingSphere.Center,
                                                      meshes[i].BoundingSphere.Radius * 2, Color.Black);
                }
            };
            game.UpdateEvent += delegate
            {
                time += game.Elapsed;



                if (game.Keyboard.IsKeyPressed(Microsoft.Xna.Framework.Input.Keys.F))
                {
                    var dEl = new MeshDynamicPhysicsElement(mesh,
                                                            Matrix.CreateTranslation(
                                                                game.SpectaterCamera.CameraPosition +
                                                                game.SpectaterCamera.CameraDirection), builder);

                    dEl.InitDynamic(engine.Scene);
                    dEl.Actor.LinearVelocity = game.SpectaterCamera.CameraDirection * 10;

                    spheres.Add(dEl);
                }



                for (int i = 0; i < spheres.Count; i++)
                {
                    spheres[i].Update(root);
                }



                engine.Update(game.Elapsed);
            };


            game.Run();
        }
Пример #12
0
        public void TestMeshPhysicsElementFactoryDynamic()
        {
            XNAGame game = new XNAGame();

            RAMMesh mesh = createTwoBoxMesh();


            BoundingBox bb = new BoundingBox();



            PhysicsEngine           engine        = new PhysicsEngine();
            PhysicsDebugRendererXNA debugRenderer = null;

            ClientPhysicsQuadTreeNode root = CreatePhysicsQuadtree(16, 4);



            var visualizer = new QuadTreeVisualizerXNA();

            var meshes = new List <MeshDynamicPhysicsElement>();

            var physicsElementFactoryXNA = new MeshPhysicsFactoryXNA(engine, root);
            var factory = physicsElementFactoryXNA.Factory;

            game.AddXNAObject(physicsElementFactoryXNA);

            game.InitializeEvent += delegate
            {
                engine.Initialize();
                debugRenderer = new PhysicsDebugRendererXNA(game, engine.Scene);
                debugRenderer.Initialize(game);
            };

            game.DrawEvent += delegate
            {
                debugRenderer.Render(game);


                visualizer.RenderNodeGroundBoundig(game, root,
                                                   delegate(ClientPhysicsQuadTreeNode node, out Color col)
                {
                    col = Color.Green;

                    return(node.PhysicsObjects.Count == 0);
                });

                visualizer.RenderNodeGroundBoundig(game, root,
                                                   delegate(ClientPhysicsQuadTreeNode node, out Color col)
                {
                    col = Color.Orange;

                    return(node.PhysicsObjects.Count > 0);
                });
            };
            game.UpdateEvent += delegate
            {
                if (game.Keyboard.IsKeyPressed(Microsoft.Xna.Framework.Input.Keys.F))
                {
                    var el = factory.CreateDynamicElement(mesh,
                                                          Matrix.CreateTranslation(game.SpectaterCamera.CameraPosition +
                                                                                   game.SpectaterCamera.CameraDirection));
                    el.Actor.LinearVelocity = game.SpectaterCamera.CameraDirection * 10;


                    meshes.Add(el);
                }

                engine.Update(game.Elapsed);
            };


            game.Run();
        }