Esempio n. 1
0
        public void TestMeshPhysicsActorBuilderTriangleMesh()
        {
            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;



            testMeshPhysicsActorBuilder(mesh);
        }
        private MeshCollisionData.TriangleMeshData createTriangleMeshForAllObjects(ObjImporter importer)
        {
            var positions = new List <Vector3>();


            for (int i = 0; i < importer.Groups.Count; i++)
            {
                var group = importer.Groups[i];
                for (int j = 0; j < group.SubObjects.Count; j++)
                {
                    var subObj = group.SubObjects[j];

                    addPositionsFromSubObject(importer, subObj, positions);
                }
            }
            var indices = new int[positions.Count * 3];

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

            var tm = new MeshCollisionData.TriangleMeshData();

            tm.Positions = positions.ToArray();
            tm.Indices   = indices;
            return(tm);
        }
        private void convertSubObjectPhysicsTriangleMesh(ObjImporter importer, OBJGroup.SubObject sub, RAMMesh mesh)
        {
            if (mesh.GetCollisionData().TriangleMesh != null)
            {
                throw new InvalidOperationException("Multiple Physics triangle meshes found in an object!");
            }
            var positions = new List <Vector3>();

            addPositionsFromSubObject(importer, sub, positions);

            var indices = createIndices(positions.Count * 3);

            var tm = new MeshCollisionData.TriangleMeshData();

            tm.Positions = positions.ToArray();
            tm.Indices   = indices;

            mesh.GetCollisionData().TriangleMesh = tm;
        }
        private MeshCollisionData.TriangleMeshData createTriangleMeshForGroup(ObjImporter importer, OBJGroup group)
        {
            var positions = new List <Vector3>();


            for (int j = 0; j < group.SubObjects.Count; j++)
            {
                var subObj = group.SubObjects[j];

                addPositionsFromSubObject(importer, subObj, positions);
            }

            int[] indices = createIndices(positions.Count * 3);

            var tm = new MeshCollisionData.TriangleMeshData();

            tm.Positions = positions.ToArray();
            tm.Indices   = indices;
            return(tm);
        }
Esempio n. 5
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();
        }