Пример #1
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();
        }
Пример #2
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();
        }