public void CreatePhysicsObjectBulletXNA() { Color[] md = new Color[verts.Length]; heightMap.GetData <Color>(md); BulletXNA.LinearMath.ObjectArray <Vector3> realVerts = new BulletXNA.LinearMath.ObjectArray <Vector3>(verts.Length); BulletXNA.LinearMath.ObjectArray <int> terrainIndices = new BulletXNA.LinearMath.ObjectArray <int>((width - 1) * (height - 1) * 6); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { int idx = x + y * width; verts[idx].Position = new Vector3(y, 0, x); realVerts[idx] = new Vector3(x, (md[idx].R / 256f) * 30f, y); } } for (int x = 0; x < width - 1; x++) { for (int y = 0; y < height - 1; y++) { terrainIndices[(x + y * (width - 1)) * 6] = ((x + 1) + (y + 1) * width); terrainIndices[(x + y * (width - 1)) * 6 + 1] = ((x + 1) + y * width); terrainIndices[(x + y * (width - 1)) * 6 + 2] = (x + y * width); terrainIndices[(x + y * (width - 1)) * 6 + 3] = ((x + 1) + (y + 1) * width); terrainIndices[(x + y * (width - 1)) * 6 + 4] = (x + y * width); terrainIndices[(x + y * (width - 1)) * 6 + 5] = (x + (y + 1) * width); } } // Build the physics stuff BulletXNA.BulletCollision.TriangleIndexVertexArray vertexArray = new BulletXNA.BulletCollision.TriangleIndexVertexArray(((width - 1) * (height - 1) * 2), terrainIndices, 1, realVerts.Count, realVerts, 1); BulletXNA.BulletCollision.CollisionShape btTerrain = new BulletXNA.BulletCollision.BvhTriangleMeshShape(vertexArray, true, true); BulletXNA.BulletDynamics.RigidBodyConstructionInfo rbInfo = new BulletXNA.BulletDynamics.RigidBodyConstructionInfo(0, new BulletXNA.DefaultMotionState(Matrix.CreateTranslation(Vector3.Zero), Matrix.Identity), btTerrain, Vector3.Zero); BulletXNA.BulletDynamics.RigidBody bulletXNARigidBody = new BulletXNA.BulletDynamics.RigidBody(rbInfo); bulletXNARigidBody.Translate(Position); indices = terrainIndices.GetRawArray(); RigidBody = bulletXNARigidBody; }
public void CreatePhysicsObjectBulletXNA() { Color[] md = new Color[verts.Length]; heightMap.GetData<Color>(md); BulletXNA.LinearMath.ObjectArray<Vector3> realVerts = new BulletXNA.LinearMath.ObjectArray<Vector3>(verts.Length); BulletXNA.LinearMath.ObjectArray<int> terrainIndices = new BulletXNA.LinearMath.ObjectArray<int>((width - 1) * (height - 1) * 6); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { int idx = x + y * width; verts[idx].Position = new Vector3(y, 0, x); realVerts[idx] = new Vector3(x, (md[idx].R / 256f) * 30f, y); } } for (int x = 0; x < width - 1; x++) { for (int y = 0; y < height - 1; y++) { terrainIndices[(x + y * (width - 1)) * 6] = ((x + 1) + (y + 1) * width); terrainIndices[(x + y * (width - 1)) * 6 + 1] = ((x + 1) + y * width); terrainIndices[(x + y * (width - 1)) * 6 + 2] = (x + y * width); terrainIndices[(x + y * (width - 1)) * 6 + 3] = ((x + 1) + (y + 1) * width); terrainIndices[(x + y * (width - 1)) * 6 + 4] = (x + y * width); terrainIndices[(x + y * (width - 1)) * 6 + 5] = (x + (y + 1) * width); } } // Build the physics stuff BulletXNA.BulletCollision.TriangleIndexVertexArray vertexArray = new BulletXNA.BulletCollision.TriangleIndexVertexArray(((width - 1) * (height - 1) * 2), terrainIndices, 1, realVerts.Count, realVerts, 1); BulletXNA.BulletCollision.CollisionShape btTerrain = new BulletXNA.BulletCollision.BvhTriangleMeshShape(vertexArray, true, true); BulletXNA.BulletDynamics.RigidBodyConstructionInfo rbInfo = new BulletXNA.BulletDynamics.RigidBodyConstructionInfo(0, new BulletXNA.DefaultMotionState(Matrix.CreateTranslation(Vector3.Zero), Matrix.Identity), btTerrain, Vector3.Zero); BulletXNA.BulletDynamics.RigidBody bulletXNARigidBody = new BulletXNA.BulletDynamics.RigidBody(rbInfo); bulletXNARigidBody.Translate(Position); indices = terrainIndices.GetRawArray(); RigidBody = bulletXNARigidBody; }