First some background, the scene I'm making is using BSP trees (built from Constructive Solid Geometry operations) to supply the geometry. I've implemented a StaticBsp and MobileBsp like so:
Code: Select all
private class MobileBsp<V>
: MobileMesh, IBspMesh<V>
{
public V[] Vertices { get; private set; }
public int[] Indices { get; private set; }
private MobileBsp(V[] vertices, Func<V, Vector3> positionExtractor, int[] indices, BSP bsp)
:base(vertices.Select(v => positionExtractor(v)).ToArray(), indices, AffineTransform.Identity, MobileMeshSolidity.Solid, 1)
{
Vertices = vertices;
Indices = indices;
}
public static MobileBsp<V> Create(BSP bsp, Func<Vector3, Vector3, V> positionNormalToVertex, Func<V, Vector3> vertexToPosition)
{
List<V> vertices = new List<V>();
List<int> indices = new List<int>();
bsp.ToTriangleList<V, int>(positionNormalToVertex,
v =>
{
vertices.Add(v);
return vertices.Count - 1;
},
(x, y, z) =>
{
indices.Add(x);
indices.Add(y);
indices.Add(z);
});
return new MobileBsp<V>(vertices.ToArray(), vertexToPosition, indices.ToArray(), bsp);
}
public new Matrix WorldTransform
{
get { return base.WorldTransform; }
}
}
So using these classes, I create a scene with a single static floor, and a load of mobile boxes. The boxes should fall onto the floor and stack up:
Code: Select all
Space = new BEPUphysics.Space();
Space.ForceUpdater.Gravity = new Vector3(0, -1f, 0f);
var floor = StaticBsp<VertexPositionNormalTexture>.Create(
new Xna.Csg.Primitives.Cube().Transform(Matrix.CreateScale(10, 1, 10)),
(p, n) => new VertexPositionNormalTexture(p, n, Vector2.Zero),
v => v.Position
);
objects.Add(floor);
Space.Add(floor);
int count = 5;
for (int x = 0; x < count; x++)
{
for (int y = 0; y < count; y++)
{
for (int z = 0; z < count; z++)
{
var e = MobileBsp<VertexPositionNormalTexture>.Create(
new Xna.Csg.Primitives.Cube(),
(p, n) => new VertexPositionNormalTexture(p, n, Vector2.Zero),
v => v.Position
);
objects.Add(e);
e.Position = new Vector3(x * 2 - count, y * 2 + 1, z * 2 - count);
Space.Add(e);
}
}
}
So what stupid thing have I done wrong?
