Exemplo n.º 1
0
        public void TestPhysicsMeshPoolConvexMesh()
        {
            XNAGame game = new XNAGame();

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

            var convex = new MeshCollisionData.Convex();

            convex.Positions = new List <Vector3>();
            Vector3[] pos      = CreatePyramidPositions();
            Vector3[] transpos = new Vector3[pos.Length];
            var       mat      = Matrix.CreateTranslation(20, 5, 20);

            Vector3.Transform(pos, ref mat, transpos);
            convex.Positions.AddRange(transpos);

            data.ConvexMeshes.Add(convex);

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



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

                var pool = new MeshPhysicsPool();

                var tMesh1 = pool.CreateConvexMesh(engine.Scene, mesh.GetCollisionData().ConvexMeshes[0]);
                var tMesh2 = pool.CreateConvexMesh(engine.Scene, mesh.GetCollisionData().ConvexMeshes[0]);
                Assert.AreEqual(tMesh1, tMesh2);


                var actorDesc = new ActorDescription(new ConvexShapeDescription()
                {
                    ConvexMesh = tMesh1
                });
                engine.Scene.CreateActor(actorDesc);
            };

            game.DrawEvent += delegate
            {
                debugRenderer.Render(game);
            };
            int frameNum = 0;

            game.UpdateEvent += delegate
            {
                engine.Update(game.Elapsed);
                //if (frameNum > 2) game.Exit();
                frameNum++;
            };


            game.Run();
        }
Exemplo n.º 2
0
        public void TestPhysicsDebugRendererXNA()
        {
            XNAGame game = new XNAGame();

            game.SpectaterCamera.CameraPosition = new Vector3(0, 0, -40);
            PhysicsEngine engine = new PhysicsEngine();

            //game.AddXNAObject(engine);

            engine.Initialize();

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

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


            /*game.InitializeEvent += delegate
             *  {
             *
             *
             *
             *  };
             *
             * game.UpdateEvent += delegate
             *  {
             *      engine.Update(game.Elapsed);
             *  };*/

            game.Run();

            engine.Dispose();
        }
        public void TestSyncOnlineClient()
        {
            var conn = NetworkingUtilities.ConnectTCP(10045, "5.149.17.16");

            //var conn = NetworkingClientTest.ConnectTCP(10045, "127.0.0.1");
            conn.Receiving = true;
            var client = new ClientPacketManagerNetworked(conn);

            var clientSyncer = new ClientSyncer(client);



            var physicsEngine = new PhysicsEngine();

            StillDesign.PhysX.Scene serverScene = null;

            PhysicsDebugRendererXNA debugRenderer;
            PhysicsDebugRendererXNA debugRendererServer;
            var game = new XNAGame();

            game.InitializeEvent += delegate
            {
                physicsEngine.Initialize();
                serverScene = physicsEngine.CreateScene(physicsEngine.Scene.Gravity, true);


                debugRenderer = new PhysicsDebugRendererXNA(game, physicsEngine.Scene);
                game.AddXNAObject(debugRenderer);
                debugRendererServer = new PhysicsDebugRendererXNA(game, serverScene);
                game.AddXNAObject(debugRendererServer);

                ActorDescription       actorDesc;
                SphereShapeDescription shape;


                shape     = new SphereShapeDescription(1);
                actorDesc = new ActorDescription(shape);
                actorDesc.BodyDescription = new BodyDescription(10);

                var client1 = clientSyncer.CreateActor(physicsEngine.Scene.CreateActor(actorDesc));

                client1.ID = 2; // Identify
            };

            game.UpdateEvent += delegate
            {
                physicsEngine.Update(game);
                physicsEngine.UpdateScene(game.Elapsed, serverScene);

                clientSyncer.Update(game.Elapsed);
            };

            client.SyncronizeRemotePacketIDs();
            client.WaitForUDPConnected();

            game.Run();

            physicsEngine.Dispose();
        }
Exemplo n.º 4
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();
        }
Exemplo n.º 5
0
        public void TestTriangleMesh()
        {
            XNAGame game = new XNAGame();

            game.SpectaterCamera.CameraPosition = new Vector3(0, 0, -40);
            PhysicsEngine engine = new PhysicsEngine();

            //game.AddXNAObject(engine);



            game.InitializeEvent += delegate
            {
                engine.Initialize();

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

                game.AddXNAObject(debugRenderer);

                TangentVertex[] vertices;
                short[]         indices;
                BoxMesh.CreateUnitBoxVerticesAndIndices(out vertices, out indices);

                var positions = new Vector3[vertices.Length];
                for (int i = 0; i < positions.Length; i++)
                {
                    positions[i] = vertices[i].pos;
                }

                int[] intIndices = new int[indices.Length];
                for (int i = 0; i < intIndices.Length; i++)
                {
                    intIndices[i] = indices[i];
                }

                var triangleMesh = CreateTriangleMesh(positions, intIndices, engine.Scene);

                var triangleMeshShapeDesc = new TriangleMeshShapeDescription();
                triangleMeshShapeDesc.TriangleMesh = triangleMesh;

                var actorDesc = new ActorDescription(triangleMeshShapeDesc);

                var actor = engine.Scene.CreateActor(actorDesc);
            };

            game.UpdateEvent += delegate
            {
                engine.Update(game.Elapsed);
            };

            game.Run();

            engine.Dispose();
        }
Exemplo n.º 6
0
        private void testMeshPhysicsActorBuilder(RAMMesh mesh)
        {
            XNAGame     game        = new XNAGame();
            BoundingBox boundingBox = new BoundingBox();

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

            Matrix mirrorMatrix = Matrix.CreateScale(-1, 1, 1);


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


                var builder = new MeshPhysicsActorBuilder(new MeshPhysicsPool());
                builder.CreateActorStatic(engine.Scene, mesh.GetCollisionData(), Matrix.Identity);


                boundingBox = builder.CalculateBoundingBox(mesh.GetCollisionData());

                builder.CreateActorStatic(engine.Scene, mesh.GetCollisionData(), mirrorMatrix);
            };

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

                game.LineManager3D.WorldMatrix = Matrix.Identity;
                game.LineManager3D.AddBox(boundingBox, Color.Orange);
                game.LineManager3D.WorldMatrix = mirrorMatrix;
                game.LineManager3D.AddBox(boundingBox, Color.Yellow);
                game.LineManager3D.WorldMatrix = Matrix.Identity;
            };
            game.UpdateEvent += delegate
            {
                engine.Update(game.Elapsed);
                if (game.Keyboard.IsKeyPressed(Microsoft.Xna.Framework.Input.Keys.F))
                {
                    Actor actor = PhysicsHelper.CreateDynamicSphereActor(engine.Scene, 1, 1);
                    actor.GlobalPosition = game.SpectaterCamera.CameraPosition +
                                           game.SpectaterCamera.CameraDirection * 5;
                    actor.LinearVelocity = game.SpectaterCamera.CameraDirection * 5;
                }
            };


            game.Run();
        }
Exemplo n.º 7
0
        public void TestContactNotify()
        {
            XNAGame game = new XNAGame();

            game.SpectaterCamera.CameraPosition = new Vector3(0, 0, -40);
            PhysicsEngine engine = new PhysicsEngine();


            engine.AddContactNotification(delegate(ContactPair contactInformation, ContactPairFlag events)
            {
                var pos = contactInformation.ActorA.GlobalPosition;
                game.LineManager3D.AddLine(pos, pos + Vector3.Up * 5, Color.Yellow);
                pos = contactInformation.ActorB.GlobalPosition;
                game.LineManager3D.AddLine(pos, pos + Vector3.Up * 5, Color.Orange);
            });



            game.InitializeEvent += delegate
            {
                engine.Initialize();

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

                game.AddXNAObject(debugRenderer);
            };

            game.UpdateEvent += delegate
            {
                if (game.Keyboard.IsKeyPressed(Microsoft.Xna.Framework.Input.Keys.F))
                {
                    Actor actor = PhysicsHelper.CreateDynamicSphereActor(engine.Scene, 1, 1);
                    actor.GlobalPosition = game.SpectaterCamera.CameraPosition +
                                           game.SpectaterCamera.CameraDirection * 5;
                    actor.LinearVelocity     = game.SpectaterCamera.CameraDirection * 5;
                    actor.ContactReportFlags = ContactPairFlag.All;
                    //actor.ContactReportThreshold = 10000;
                }
                engine.Update(game.Elapsed);
            };

            game.Run();

            engine.Dispose();
        }
Exemplo n.º 8
0
        public void Initialize(IXNAGame _game)
        {
            Engine.Initialize();

            PhysicsDebugRendererXNA debugRenderer = new PhysicsDebugRendererXNA(Game, Engine.Scene);

            Game.AddXNAObject(debugRenderer);


            Actor holderActor;

            actor = CreateCogHolder(Engine, out holderActor);


            var bar = new BarCog(Engine);

            bar.Actor.GlobalPosition = Vector3.Up * 2 + Vector3.UnitZ * -10;
        }
Exemplo n.º 9
0
        public void TestTwoCogs()
        {
            XNAGame game = new XNAGame();

            game.SpectaterCamera.CameraPosition = new Vector3(0, 0, -40);
            game.SpectaterCamera.FarClip        = 10000;
            PhysicsEngine engine       = new PhysicsEngine();
            TriangleMesh  triangleMesh = null;
            //game.AddXNAObject(engine);

            Actor actor        = null;
            Actor holderActor  = null;
            Actor holderActor2 = null;

            game.InitializeEvent += delegate
            {
                engine.Initialize();

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

                game.AddXNAObject(debugRenderer);


                actor = CogEngine.CreateCogHolder(engine, out holderActor);
                Joint joint;
                CylindricalJointDescription jointDesc;
                ActorDescription            actorDesc;
                BodyDescription             bodyDesc;
                CapsuleShapeDescription     capsuleShapeDesc;


                Actor mobileCog = CogEngine.CreateCog(engine.Scene);
                mobileCog.GlobalPose = Matrix.CreateRotationX(MathHelper.PiOver2);

                capsuleShapeDesc = new CapsuleShapeDescription(4.7f, 3f);

                actorDesc = new ActorDescription(capsuleShapeDesc);

                bodyDesc            = new BodyDescription(10000);
                bodyDesc.BodyFlags |= BodyFlag.Kinematic;

                actorDesc.GlobalPose      = Matrix.CreateTranslation(Vector3.Up + new Vector3(0, 0, 0));
                actorDesc.BodyDescription = bodyDesc;

                holderActor2 = engine.Scene.CreateActor(actorDesc);


                jointDesc        = new CylindricalJointDescription();
                jointDesc.Actor1 = mobileCog;
                jointDesc.Actor2 = holderActor2;

                jointDesc.SetGlobalAnchor(new Vector3(0, 0, 0));
                jointDesc.SetGlobalAxis(Vector3.Up);


                joint = engine.Scene.CreateJoint(jointDesc);
                joint.AddLimitPlane(new LimitPlane(new Plane(Vector3.Up, 1), 0));
            };

            game.UpdateEvent += delegate
            {
                if (game.Keyboard.IsKeyPressed(Microsoft.Xna.Framework.Input.Keys.F))
                {
                    var bol = PhysicsHelper.CreateDynamicSphereActor(engine.Scene, 1, 1);
                    bol.GlobalPosition = game.SpectaterCamera.CameraPosition +
                                         game.SpectaterCamera.CameraDirection * 5;
                    bol.LinearVelocity = game.SpectaterCamera.CameraDirection * 5;
                }

                Plane p   = new Plane(Vector3.Up, 0);
                var   ray = game.GetWereldViewRay(new Vector2(game.Window.ClientBounds.Width * 0.5f,
                                                              game.Window.ClientBounds.Height * 0.5f));

                float?dist = ray.Intersects(p);
                if (dist.HasValue && game.Keyboard.IsKeyDown(Keys.C))
                {
                    //mobileCog.GlobalPosition = ray.Position + ray.Direction * dist.Value + Vector3.Up;
                    holderActor2.MoveGlobalPositionTo(ray.Position + ray.Direction * dist.Value + Vector3.Up);
                }

                if (game.Keyboard.IsKeyDown(Keys.R))
                {
                    CogEngine.temp(game, actor);

                    /*mobileCog.Dispose();
                     * mobileCog = CreateCog(triangleMesh, engine.Scene);*/
                }



                engine.Update(game.Elapsed);
            };

            game.Run();

            engine.Dispose();
        }
Exemplo n.º 10
0
        public void TestCogsFun()
        {
            XNAGame game = new XNAGame();

            game.SpectaterCamera.CameraPosition = new Vector3(0, 0, -40);
            game.SpectaterCamera.FarClip        = 10000;
            PhysicsEngine engine = new PhysicsEngine();

            Actor actor = null;

            Actor ghostCogHolder = null;

            game.InitializeEvent += delegate
            {
                engine.Initialize();

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

                game.AddXNAObject(debugRenderer);


                Actor holderActor;
                actor = CogEngine.CreateCogHolder(engine, out holderActor);
            };

            game.UpdateEvent += delegate
            {
                if (game.Keyboard.IsKeyPressed(Microsoft.Xna.Framework.Input.Keys.F))
                {
                    var bol = PhysicsHelper.CreateDynamicSphereActor(engine.Scene, 1, 1);
                    bol.GlobalPosition = game.SpectaterCamera.CameraPosition +
                                         game.SpectaterCamera.CameraDirection * 5;
                    bol.LinearVelocity = game.SpectaterCamera.CameraDirection * 5;
                }


                Plane p   = new Plane(Vector3.Up, 0);
                var   ray = game.GetWereldViewRay(new Vector2(game.Window.ClientBounds.Width * 0.5f,
                                                              game.Window.ClientBounds.Height * 0.5f));

                float?dist = ray.Intersects(p);
                if (dist.HasValue && game.Keyboard.IsKeyDown(Keys.C))
                {
                    if (ghostCogHolder != null)
                    {
                        ghostCogHolder.MoveGlobalPositionTo(ray.Position + ray.Direction * dist.Value + Vector3.Up * 7f);
                    }
                }

                if (game.Keyboard.IsKeyDown(Keys.R))
                {
                    CogEngine.temp(game, actor);

                    /*mobileCog.Dispose();
                     * mobileCog = CreateCog(triangleMesh, engine.Scene);*/
                }

                if (game.Keyboard.IsKeyPressed(Keys.Enter))
                {
                    CogEngine.CreateCogHolder(engine, out ghostCogHolder);

                    //ghostCogHolder.MoveGlobalPositionTo(Vector3.UnitX * 30 );
                    ghostCogHolder.MoveGlobalOrientationTo(Matrix.CreateRotationX(MathHelper.PiOver2) *
                                                           Matrix.CreateTranslation(Vector3.UnitX * 30 + Vector3.UnitY * 20));
                }



                engine.Update(game.Elapsed);
            };

            game.Run();

            engine.Dispose();
        }
Exemplo n.º 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();
        }
        public void TestSyncOnlineServer()
        {
            var server = new ServerPacketManagerNetworked(10045, 10046);


            var serverSyncer = new ServerSyncer(server);



            server.Start();


            var physicsEngine = new PhysicsEngine();

            StillDesign.PhysX.Scene serverScene = null;

            PhysicsDebugRendererXNA debugRenderer;
            PhysicsDebugRendererXNA debugRendererServer;
            var game = new XNAGame();
            ServerSyncedActor server1 = null;

            game.InitializeEvent += delegate
            {
                physicsEngine.Initialize();
                serverScene = physicsEngine.CreateScene(physicsEngine.Scene.Gravity, true);


                debugRenderer = new PhysicsDebugRendererXNA(game, physicsEngine.Scene);
                game.AddXNAObject(debugRenderer);
                debugRendererServer = new PhysicsDebugRendererXNA(game, serverScene);
                game.AddXNAObject(debugRendererServer);

                ActorDescription       actorDesc;
                SphereShapeDescription shape;

                shape     = new SphereShapeDescription(1);
                actorDesc = new ActorDescription(shape);
                actorDesc.BodyDescription = new BodyDescription(10);

                server1 = serverSyncer.CreateActor(serverScene.CreateActor(actorDesc));
                ((WorldPhysxSyncActor)server1.Actor).Actor.AddForce(Vector3.UnitX * 200, ForceMode.Impulse);
                server1.ID = 2; // Identify
            };

            game.UpdateEvent += delegate
            {
                physicsEngine.Update(game);
                physicsEngine.UpdateScene(game.Elapsed, serverScene);

                serverSyncer.Update(game.Elapsed);

                if (game.Keyboard.IsKeyDown(Keys.Up))
                {
                    ((WorldPhysxSyncActor)server1.Actor).Actor.AddForce(Vector3.UnitX * 200, ForceMode.Force);
                }
                if (game.Keyboard.IsKeyDown(Keys.Down))
                {
                    ((WorldPhysxSyncActor)server1.Actor).Actor.AddForce(-Vector3.UnitX * 200, ForceMode.Force);
                }
            };

            game.Run();

            physicsEngine.Dispose();
        }
        public void TestSyncOnline()
        {
            var server  = new ServerPacketManagerNetworked(10045, 10046);
            var success = new AutoResetEvent(false);



            var serverSyncer = new ServerSyncer(server);



            server.Start();

            var conn = NetworkingUtilities.ConnectTCP(10045, "127.0.0.1");

            conn.Receiving = true;
            var client = new ClientPacketManagerNetworked(conn);

            var clientSyncer = new ClientSyncer(client);



            var physicsEngine = new PhysicsEngine();

            StillDesign.PhysX.Scene serverScene = null;

            PhysicsDebugRendererXNA debugRenderer;
            PhysicsDebugRendererXNA debugRendererServer;
            var game = new XNAGame();

            game.InitializeEvent += delegate
            {
                physicsEngine.Initialize();
                serverScene = physicsEngine.CreateScene(physicsEngine.Scene.Gravity, true);


                debugRenderer = new PhysicsDebugRendererXNA(game, physicsEngine.Scene);
                game.AddXNAObject(debugRenderer);
                debugRendererServer = new PhysicsDebugRendererXNA(game, serverScene);
                game.AddXNAObject(debugRendererServer);

                ActorDescription       actorDesc;
                SphereShapeDescription shape;

                shape     = new SphereShapeDescription(1);
                actorDesc = new ActorDescription(shape);
                actorDesc.BodyDescription = new BodyDescription(10);

                var server1 = serverSyncer.CreateActor(serverScene.CreateActor(actorDesc));
                ((WorldPhysxSyncActor)server1.Actor).Actor.AddForce(Vector3.UnitX * 200, ForceMode.Impulse);

                shape     = new SphereShapeDescription(1);
                actorDesc = new ActorDescription(shape);
                actorDesc.BodyDescription = new BodyDescription(10);

                var client1 = clientSyncer.CreateActor(physicsEngine.Scene.CreateActor(actorDesc));

                client1.ID = server1.ID;                         // Identify
            };

            game.UpdateEvent += delegate
            {
                physicsEngine.Update(game);
                physicsEngine.UpdateScene(game.Elapsed, serverScene);

                serverSyncer.Update(game.Elapsed);
                clientSyncer.Update(game.Elapsed);
            };

            client.SyncronizeRemotePacketIDs();
            client.WaitForUDPConnected();

            game.Run();

            physicsEngine.Dispose();
        }
Exemplo n.º 14
0
        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();
        }
Exemplo n.º 15
0
        public void TestOBJToRAMMeshConverterVisual()
        {
            PhysicsEngine           engine        = new PhysicsEngine();
            PhysicsDebugRendererXNA debugRenderer = null;



            var texFactory = new RAMTextureFactory();
            var c          = new OBJToRAMMeshConverter(texFactory);


            var importer = new ObjImporter();

            importer.AddMaterialFileStream("CollisionModelBoxes001.mtl",
                                           EmbeddedFile.GetStream("MHGameWork.TheWizards.Tests.Physics.Files.CollisionModelBoxes001.mtl",
                                                                  "CollisionModelBoxes001.mtl"));
            importer.ImportObjFile(EmbeddedFile.GetStream("MHGameWork.TheWizards.Tests.Physics.Files.CollisionModelBoxes001.obj", "CollisionModelBoxes001.obj"));


            var mesh = c.CreateMesh(importer);

            importer = new ObjImporter();
            importer.AddMaterialFileStream("HouseTest.mtl", new FileStream("../GameData/Core/001-House01_BoxTest-OBJ/HouseTest.mtl", FileMode.Open));
            importer.ImportObjFile("../GameData/Core/001-House01_BoxTest-OBJ/HouseTest.obj");

            var mesh2 = c.CreateMesh(importer);

            importer = new ObjImporter();
            var fsMHouseMat = new FileStream(TestFiles.MerchantsHouseMtl, FileMode.Open);

            importer.AddMaterialFileStream("MerchantsHouse.mtl", fsMHouseMat);
            importer.ImportObjFile(TestFiles.MerchantsHouseObj);

            fsMHouseMat.Close();

            var mesh3 = c.CreateMesh(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 el = renderer.AddMesh(mesh);

            el.WorldMatrix = Matrix.CreateTranslation(Vector3.Right * 0 * 2 + Vector3.UnitZ * 0 * 2);

            el             = renderer.AddMesh(mesh2);
            el.WorldMatrix = Matrix.CreateTranslation(new Vector3(0, 0, 80));

            el             = renderer.AddMesh(mesh3);
            el.WorldMatrix = Matrix.CreateTranslation(new Vector3(0, 0, -80));



            var game = new XNAGame();

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

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


                var builder = new MeshPhysicsActorBuilder(new MeshPhysicsPool());
                builder.CreateActorStatic(engine.Scene, mesh.GetCollisionData(), Matrix.Identity);
                builder.CreateActorStatic(engine.Scene, mesh2.GetCollisionData(), Matrix.CreateTranslation(new Vector3(0, 0, 80)));
                builder.CreateActorStatic(engine.Scene, mesh3.GetCollisionData(), Matrix.CreateTranslation(new Vector3(0, 0, -80)));
            };

            game.DrawEvent += delegate
            {
                debugRenderer.Render(game);
            };
            game.UpdateEvent += delegate
            {
                engine.Update(game.Elapsed);
                if (game.Keyboard.IsKeyPressed(Microsoft.Xna.Framework.Input.Keys.F))
                {
                    Actor actor = PhysicsHelper.CreateDynamicSphereActor(engine.Scene, 0.3f, 1);
                    actor.GlobalPosition = game.SpectaterCamera.CameraPosition +
                                           game.SpectaterCamera.CameraDirection * 5;
                    actor.LinearVelocity = game.SpectaterCamera.CameraDirection * 5;
                }
            };



            game.Run();
        }
Exemplo n.º 16
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();
        }
Exemplo n.º 17
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();
        }
Exemplo n.º 18
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();
        }
Exemplo n.º 19
0
        public void TestCreateEntityActor()
        {
            XNAGame game = new XNAGame();

            Database.Database database = loadDatabaseServices();

            EntityManagerService ems = new EntityManagerService(database);


            BoundingBox boundingBox = new BoundingBox();

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



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


                EntityFullData 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(10, 10, 10));

                EntityPhysicsActorBuilder builder = new EntityPhysicsActorBuilder();
                builder.CreateActorForEntity(engine.Scene, entityData);

                BoundingBox bb;
                bb     = entityData.ObjectFullData.Models[0].BoundingBox;
                bb.Min = Vector3.Transform(bb.Min, entityData.ObjectFullData.Models[0].ObjectMatrix * entityData.Transform.CreateMatrix());
                bb.Max = Vector3.Transform(bb.Max, entityData.ObjectFullData.Models[0].ObjectMatrix * entityData.Transform.CreateMatrix());

                BoundingBox bb2;
                bb2     = entityData.ObjectFullData.Models[1].BoundingBox;
                bb2.Min = Vector3.Transform(bb2.Min, entityData.ObjectFullData.Models[1].ObjectMatrix * entityData.Transform.CreateMatrix());
                bb2.Max = Vector3.Transform(bb2.Max, entityData.ObjectFullData.Models[1].ObjectMatrix * entityData.Transform.CreateMatrix());


                boundingBox = BoundingBox.CreateMerged(bb, bb2);
            };

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


                game.LineManager3D.AddBox(boundingBox, Color.Orange);
            };
            game.UpdateEvent += delegate
            {
                engine.Update(game.Elapsed);
                if (game.Keyboard.IsKeyPressed(Microsoft.Xna.Framework.Input.Keys.F))
                {
                    Actor actor = PhysicsHelper.CreateDynamicSphereActor(engine.Scene, 1, 1);
                    actor.GlobalPosition = game.SpectaterCamera.CameraPosition +
                                           game.SpectaterCamera.CameraDirection * 5;
                    actor.LinearVelocity = game.SpectaterCamera.CameraDirection * 5;
                }
            };


            game.Run();
        }
        public void TestSyncOffline()
        {
            var serverpm = new SimpleServerPacketManager();
            var clientpm = serverpm.CreateClient();


            var clientSyncer = new ClientSyncer(clientpm);
            var serverSyncer = new ServerSyncer(serverpm);

            var physicsEngine = new PhysicsEngine();

            StillDesign.PhysX.Scene serverScene = null;

            PhysicsDebugRendererXNA debugRenderer;
            PhysicsDebugRendererXNA debugRendererServer;
            var   game          = new XNAGame();
            float totalTime     = 0;
            float timeSinceTick = 0;
            float tickRate      = 1 / 30f;
            int   tickNumber    = 0;
            float packetLoss    = 0.25f;


            var rand = new Random();

            game.InitializeEvent += delegate
            {
                physicsEngine.Initialize();
                serverScene = physicsEngine.CreateScene(physicsEngine.Scene.Gravity, true);


                debugRenderer = new PhysicsDebugRendererXNA(game, physicsEngine.Scene);
                game.AddXNAObject(debugRenderer);
                debugRendererServer = new PhysicsDebugRendererXNA(game, serverScene);
                game.AddXNAObject(debugRendererServer);

                ActorDescription       actorDesc;
                SphereShapeDescription shape;

                shape     = new SphereShapeDescription(1);
                actorDesc = new ActorDescription(shape);
                actorDesc.BodyDescription = new BodyDescription(10);

                var server = serverSyncer.CreateActor(serverScene.CreateActor(actorDesc));
                ((WorldPhysxSyncActor)server.Actor).Actor.AddForce(Vector3.UnitX * 200, ForceMode.Impulse);

                shape     = new SphereShapeDescription(1);
                actorDesc = new ActorDescription(shape);
                actorDesc.BodyDescription = new BodyDescription(10);

                var client = clientSyncer.CreateActor(physicsEngine.Scene.CreateActor(actorDesc));

                client.ID = server.ID;                         // Identify
            };

            game.UpdateEvent += delegate
            {
                physicsEngine.Update(game);
                physicsEngine.UpdateScene(game.Elapsed, serverScene);

                serverSyncer.Update(game.Elapsed);
                clientSyncer.Update(game.Elapsed);
            };

            game.Run();

            physicsEngine.Dispose();
        }
Exemplo n.º 21
0
        public void TestPhysicsMeshPoolTriangleMesh()
        {
            XNAGame game = new XNAGame();

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

            var triangleMesh = new MeshCollisionData.TriangleMeshData();

            Vector3[] pos      = CreatePyramidPositions();
            Vector3[] transpos = new Vector3[pos.Length];
            var       mat      = Matrix.CreateTranslation(20, 5, 20);

            Vector3.Transform(pos, ref mat, transpos);
            triangleMesh.Positions = transpos;
            triangleMesh.Indices   = new int[triangleMesh.Positions.Length];
            for (int i = 0; i < triangleMesh.Positions.Length; i++)
            {
                triangleMesh.Indices[i] = i;
            }


            data.TriangleMesh = triangleMesh;

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



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

                var pool = new MeshPhysicsPool();

                var tMesh1 = pool.CreateTriangleMesh(engine.Scene, mesh.GetCollisionData().TriangleMesh);
                var tMesh2 = pool.CreateTriangleMesh(engine.Scene, mesh.GetCollisionData().TriangleMesh);
                Assert.AreEqual(tMesh1, tMesh2);


                var actorDesc = new ActorDescription(new TriangleMeshShapeDescription()
                {
                    TriangleMesh = tMesh1
                });
                engine.Scene.CreateActor(actorDesc);
            };

            game.DrawEvent += delegate
            {
                debugRenderer.Render(game);
            };
            int frameNum = 0;

            game.UpdateEvent += delegate
            {
                engine.Update(game.Elapsed);
                //if (frameNum > 2) game.Exit();
                frameNum++;
            };


            game.Run();
        }
        public void TestSyncDirect()
        {
            var client = new ClientSyncedActor();
            var server = new ServerSyncedActor();

            var physicsEngine = new PhysicsEngine();

            StillDesign.PhysX.Scene serverScene = null;

            PhysicsDebugRendererXNA debugRenderer;
            PhysicsDebugRendererXNA debugRendererServer;
            var   game          = new XNAGame();
            float totalTime     = 0;
            float timeSinceTick = 0;
            float tickRate      = 1 / 30f;
            int   tickNumber    = 0;
            float packetLoss    = 0.25f;

            var rand = new Random();

            game.InitializeEvent += delegate
            {
                physicsEngine.Initialize();
                serverScene = physicsEngine.CreateScene(physicsEngine.Scene.Gravity, true);


                debugRenderer = new PhysicsDebugRendererXNA(game, physicsEngine.Scene);
                game.AddXNAObject(debugRenderer);
                debugRendererServer = new PhysicsDebugRendererXNA(game, serverScene);
                game.AddXNAObject(debugRendererServer);

                ActorDescription       actorDesc;
                SphereShapeDescription shape;

                shape     = new SphereShapeDescription(1);
                actorDesc = new ActorDescription(shape);
                actorDesc.BodyDescription = new BodyDescription(10);

                server.Actor = new WorldPhysxSyncActor(serverScene.CreateActor(actorDesc));
                ((WorldPhysxSyncActor)server.Actor).Actor.AddForce(Vector3.UnitX * 200, ForceMode.Impulse);

                shape     = new SphereShapeDescription(1);
                actorDesc = new ActorDescription(shape);
                actorDesc.BodyDescription = new BodyDescription(10);

                client.Actor =
                    new WorldPhysxSyncActor(physicsEngine.Scene.CreateActor(actorDesc));
            };

            game.UpdateEvent += delegate
            {
                physicsEngine.Update(game);
                physicsEngine.UpdateScene(game.Elapsed, serverScene);
                totalTime     += game.Elapsed;
                timeSinceTick += game.Elapsed;
                int totalMiliseconds = (int)(totalTime * 1000);

                while (timeSinceTick > tickRate)
                {
                    timeSinceTick -= tickRate;
                    //Do a tick
                    tickNumber++;
                    server.Tick();
                    var p = new UpdateEntityPacket();
                    p.Positie     = server.Positie;
                    p.RotatieQuat = server.RotatieQuat;
                    if (rand.NextDouble() < 1 - packetLoss)
                    {
                        client.AddEntityUpdate(tickNumber, p);
                    }
                }

                client.Process(totalMiliseconds, tickRate);
            };

            game.Run();

            physicsEngine.Dispose();
        }
Exemplo n.º 23
0
        public void TestJoint()
        {
            XNAGame game = new XNAGame();

            game.SpectaterCamera.CameraPosition = new Vector3(0, 0, -40);
            PhysicsEngine engine = new PhysicsEngine();

            //game.AddXNAObject(engine);



            game.InitializeEvent += delegate
            {
                engine.Initialize();

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

                game.AddXNAObject(debugRenderer);


                Actor actorA, actorB;
                {
                    BoxShapeDescription boxShapeDesc = new BoxShapeDescription(3, 3, 3);

                    BodyDescription bodyDesc = new BodyDescription(10.0f);
                    bodyDesc.BodyFlags |= BodyFlag.Kinematic;

                    ActorDescription actorDesc = new ActorDescription()
                    {
                        BodyDescription = bodyDesc,
                        GlobalPose      = Matrix.CreateTranslation(70, 25, 65),
                        Shapes          = { boxShapeDesc }
                    };
                    actorA = engine.Scene.CreateActor(actorDesc);
                }
                {
                    BoxShapeDescription boxShapeDesc = new BoxShapeDescription(3, 3, 3);

                    ActorDescription actorDesc = new ActorDescription()
                    {
                        BodyDescription = new BodyDescription(10.0f),
                        GlobalPose      = Matrix.CreateTranslation(70, 15, 65),
                        Shapes          = { boxShapeDesc }
                    };
                    actorB = engine.Scene.CreateActor(actorDesc);
                }

                PrismaticJointDescription prismaticJointDesc = new PrismaticJointDescription()
                {
                    Actor1 = actorA,
                    Actor2 = actorB,
                };
                prismaticJointDesc.SetGlobalAnchor(new Vector3(70, 20, 65));
                prismaticJointDesc.SetGlobalAxis(new Vector3(0, 1, 0));

                PrismaticJoint prismaticJoint = engine.Scene.CreateJoint(prismaticJointDesc) as PrismaticJoint;

                LimitPlane limitPlane = new LimitPlane(new Vector3(0, 1, 0), new Vector3(-30, 8, -30), 0);
                prismaticJoint.AddLimitPlane(limitPlane);
            };

            game.UpdateEvent += delegate
            {
                if (game.Keyboard.IsKeyPressed(Microsoft.Xna.Framework.Input.Keys.F))
                {
                    Actor actor = PhysicsHelper.CreateDynamicSphereActor(engine.Scene, 1, 1);
                    actor.GlobalPosition = game.SpectaterCamera.CameraPosition +
                                           game.SpectaterCamera.CameraDirection * 5;
                    actor.LinearVelocity = game.SpectaterCamera.CameraDirection * 5;
                }
                engine.Update(game.Elapsed);
            };

            game.Run();

            engine.Dispose();
        }
Exemplo n.º 24
0
        public void TestObjImportPhysics()
        {
            XNAGame game = new XNAGame();

            game.SpectaterCamera.CameraPosition = new Vector3(0, 0, -40);
            game.SpectaterCamera.FarClip        = 10000;
            PhysicsEngine engine = new PhysicsEngine();

            //game.AddXNAObject(engine);



            game.InitializeEvent += delegate
            {
                engine.Initialize();

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

                game.AddXNAObject(debugRenderer);


                ObjImporter importer = new ObjImporter();
                //importer.ImportObjFile(EmbeddedFile.GetStream(typeof(ObjImporter).Assembly, "MHGameWork.TheWizards.OBJParser.Files.Crate.obj", "Crate.obj"));
                importer.ImportObjFile(TestFiles.MerchantsHouseObj);
                List <Vector3> positions            = importer.Vertices;
                Vector3[]      transformedPositions = new Vector3[positions.Count];
                Matrix         objectMatrix         = Matrix.CreateScale(5);
                Matrix         transform            = objectMatrix;
                Vector3.Transform(positions.ToArray(), ref transform, transformedPositions);
                var indices = new List <int>();

                for (int i = 0; i < importer.Groups.Count; i++)
                {
                    for (int j = 0; j < importer.Groups[i].SubObjects.Count; j++)
                    {
                        for (int k = 0; k < importer.Groups[i].SubObjects[j].Faces.Count; k++)
                        {
                            indices.Add(importer.Groups[i].SubObjects[j].Faces[k].V1.Position);
                            indices.Add(importer.Groups[i].SubObjects[j].Faces[k].V2.Position);
                            indices.Add(importer.Groups[i].SubObjects[j].Faces[k].V3.Position);
                        }
                    }
                }



                var triangleMesh = CreateTriangleMesh(transformedPositions, indices.ToArray(), engine.Scene);

                var triangleMeshShapeDesc = new TriangleMeshShapeDescription();
                triangleMeshShapeDesc.TriangleMesh = triangleMesh;

                var actorDesc = new ActorDescription(triangleMeshShapeDesc);

                var actor = engine.Scene.CreateActor(actorDesc);
            };

            game.UpdateEvent += delegate
            {
                if (game.Keyboard.IsKeyPressed(Microsoft.Xna.Framework.Input.Keys.F))
                {
                    Actor actor = PhysicsHelper.CreateDynamicSphereActor(engine.Scene, 1, 1);
                    actor.GlobalPosition = game.SpectaterCamera.CameraPosition +
                                           game.SpectaterCamera.CameraDirection * 5;
                    actor.LinearVelocity = game.SpectaterCamera.CameraDirection * 5;
                }
                engine.Update(game.Elapsed);
            };

            game.Run();

            engine.Dispose();
        }
Exemplo n.º 25
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();
        }