public void Update(IXNAGame _game)
        {
            sphereMesh.Update(game);
            if (game.Keyboard.IsKeyPressed(Microsoft.Xna.Framework.Input.Keys.F))
            {
                var pos = shooterCamera.ViewInverse.Translation;
                var dir = Vector3.Transform(Vector3.Forward, shooterCamera.ViewInverse) - pos;

                var iSphere = new ClientPhysicsTestSphere(engine.Scene,
                                                          pos + dir
                                                          , 0.3f);

                iSphere.InitDynamic();
                iSphere.Actor.LinearVelocity = dir * 10;

                spheres.Add(iSphere);
            }



            for (int i = 0; i < spheres.Count; i++)
            {
                spheres[i].Update(root, game);
            }
        }
Пример #2
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();
        }
        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();
        }
Пример #4
0
        public void TestMeshPhysicsElementFactoryStatic()
        {
            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);


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

            Curve3D curve1 = ClientTest.CreateTestObject1MovementCurve();


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

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

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

            game.AddXNAObject(physicsElementFactoryXNA);

            var el = factory.CreateStaticElement(mesh, Matrix.CreateTranslation(new Vector3(20, 0, 20)));

            meshes.Add(el);


            el = factory.CreateStaticElement(mesh, Matrix.CreateTranslation(new Vector3(-40, 0, 8)));
            meshes.Add(el);


            el = factory.CreateStaticElement(mesh, Matrix.CreateTranslation(new Vector3(80, 0, 70)));
            meshes.Add(el);

            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.LineManager3D.AddCenteredBox(sphere.Center, sphere.Radius, Color.Red);

                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;


                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();
        }