Ejemplo n.º 1
0
        public void CreatePhysicsObjectBulletSharp()
        {
            BulletSharp.TriangleIndexVertexArray vertexArray = new BulletSharp.TriangleIndexVertexArray();
            BulletSharp.IndexedMesh mesh = new BulletSharp.IndexedMesh();

            mesh.Allocate(verts.Length, System.Runtime.InteropServices.Marshal.SizeOf(Vector3.Zero), (width - 1) * (height - 1) * 2, 3 * sizeof(int));
            BulletSharp.DataStream vData = mesh.LockVerts();

            Color[] md = new Color[verts.Length];
            heightMap.GetData <Color>(md);

            Vector3[] realVerts = new Vector3[verts.Length];

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    int idx = x + y * width;
                    realVerts[idx] = new Vector3(x, (md[idx].R / 256f) * 30f, y);
                }
            }

            for (int v = 0; v < realVerts.Length; v++)
            {
                vData.Write(realVerts[v].X);
                vData.Write(realVerts[v].Y);
                vData.Write(realVerts[v].Z);
            }

            indices = new int[(width - 1) * (height - 1) * 6];
            for (int x = 0; x < width - 1; x++)
            {
                for (int y = 0; y < height - 1; y++)
                {
                    indices[(x + y * (width - 1)) * 6]     = ((x + 1) + (y + 1) * width);
                    indices[(x + y * (width - 1)) * 6 + 1] = ((x + 1) + y * width);
                    indices[(x + y * (width - 1)) * 6 + 2] = (x + y * width);

                    indices[(x + y * (width - 1)) * 6 + 3] = ((x + 1) + (y + 1) * width);
                    indices[(x + y * (width - 1)) * 6 + 4] = (x + y * width);
                    indices[(x + y * (width - 1)) * 6 + 5] = (x + (y + 1) * width);
                }
            }



            BulletSharp.IntArray iData = mesh.TriangleIndices;
            for (int idx = 0; idx < indices.Length; idx++)
            {
                iData[idx] = indices[idx];
            }

            vertexArray.AddIndexedMesh(mesh);
            BulletSharp.CollisionShape            btTerrain = new BulletSharp.BvhTriangleMeshShape(vertexArray, true);
            BulletSharp.RigidBodyConstructionInfo rbInfo    =
                new BulletSharp.RigidBodyConstructionInfo(0, new BulletSharp.DefaultMotionState(Matrix.Identity), btTerrain, Vector3.Zero);

            BulletSharp.RigidBody bulletSharpRigidBody = new BulletSharp.RigidBody(rbInfo);

            bulletSharpRigidBody.Translate(Position);

            RigidBody = bulletSharpRigidBody;
        }
        public void CreatePhysicsObjectBulletSharp()
        {
            BulletSharp.TriangleIndexVertexArray vertexArray = new BulletSharp.TriangleIndexVertexArray();
            BulletSharp.IndexedMesh mesh = new BulletSharp.IndexedMesh();

            mesh.Allocate(verts.Length, System.Runtime.InteropServices.Marshal.SizeOf(Vector3.Zero), (width - 1) * (height - 1) * 2, 3 * sizeof(int));
            BulletSharp.DataStream vData = mesh.LockVerts();

            Color[] md = new Color[verts.Length];
            heightMap.GetData<Color>(md);

            Vector3[] realVerts = new Vector3[verts.Length];

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    int idx = x + y * width;
                    realVerts[idx] = new Vector3(x, (md[idx].R / 256f) * 30f, y);
                }
            }

            for (int v = 0; v < realVerts.Length; v++)
            {
                vData.Write(realVerts[v].X);
                vData.Write(realVerts[v].Y);
                vData.Write(realVerts[v].Z);
            }

            indices = new int[(width - 1) * (height - 1) * 6];
            for (int x = 0; x < width - 1; x++)
            {
                for (int y = 0; y < height - 1; y++)
                {
                    indices[(x + y * (width - 1)) * 6] = ((x + 1) + (y + 1) * width);
                    indices[(x + y * (width - 1)) * 6 + 1] = ((x + 1) + y * width);
                    indices[(x + y * (width - 1)) * 6 + 2] = (x + y * width);

                    indices[(x + y * (width - 1)) * 6 + 3] = ((x + 1) + (y + 1) * width);
                    indices[(x + y * (width - 1)) * 6 + 4] = (x + y * width);
                    indices[(x + y * (width - 1)) * 6 + 5] = (x + (y + 1) * width);
                }
            }



            BulletSharp.IntArray iData = mesh.TriangleIndices;
            for (int idx = 0; idx < indices.Length; idx++)
            {
                iData[idx] = indices[idx];
            }

            vertexArray.AddIndexedMesh(mesh);
            BulletSharp.CollisionShape btTerrain = new BulletSharp.BvhTriangleMeshShape(vertexArray, true);
            BulletSharp.RigidBodyConstructionInfo rbInfo =
                            new BulletSharp.RigidBodyConstructionInfo(0, new BulletSharp.DefaultMotionState(Matrix.Identity), btTerrain, Vector3.Zero);

            BulletSharp.RigidBody bulletSharpRigidBody = new BulletSharp.RigidBody(rbInfo);

            bulletSharpRigidBody.Translate(Position);

            RigidBody = bulletSharpRigidBody;

        }