Ok. I think I might've found the issue, though I'm not 100% about it.
So it looks like the issue shows itself when you have a compound object colliding the terrain, instead of a simple single convex shape colliding with it. I've also found what seems to be the fix, but like I said, I'm not 100%.
Here's how I changed the TerrainDemo to get it going:
Code: Select all
public TerrainDemo(DemosGame game)
: base(game)
{
//x and y, in terms of heightmaps, refer to their local x and y coordinates. In world space, they correspond to x and z.
//Setup the heights of the terrain.
//[The size here is limited by the Reach profile the demos use- the drawer draws the terrain as a big block and runs into primitive drawing limits.
//The physics can support far larger terrains!]
int xLength = 180;
int zLength = 180;
float xSpacing = 8f;
float zSpacing = 8f;
var heights = new float[xLength, zLength];
for (int i = 0; i < xLength; i++)
{
for (int j = 0; j < zLength; j++)
{
float x = i - xLength / 2;
float z = j - zLength / 2;
//heights[i,j] = (float)(x * y / 1000f);
heights[i, j] = (float)(10 * (Math.Sin(x / 8) + Math.Sin(z / 8)));
//heights[i,j] = 3 * (float)Math.Sin(x * y / 100f);
//heights[i,j] = (x * x * x * y - y * y * y * x) / 1000f;
}
}
//Create the terrain.
var terrain = new Terrain(heights, new AffineTransform(
new Vector3(xSpacing, 1, zSpacing),
Quaternion.Identity,
new Vector3(-xLength * xSpacing / 2, 0, -zLength * zSpacing / 2)));
terrain.Material.KineticFriction = 0f;
terrain.Material.StaticFriction = 0f;
//terrain.Thickness = 5; //Uncomment this and shoot some things at the bottom of the terrain! They'll be sucked up through the ground.
Space.Add(terrain);
//for (int i = 0; i < 3; i++)
//{
// for (int j = 0; j < 3; j++)
// {
// for (int k = 0; k < 5; k++)
// {
// Space.Add(new Box(
// new Vector3(0 + i * 4, 100 - j * 10, 0 + k * 4),
// 2 + i * j * k,
// 2 + i * j * k,
// 2 + i * j * k,
// 4 + 20 * i * j * k));
// }
// }
//}
//Build the first body
var bodies = new List<CompoundShapeEntry>()
{
new CompoundShapeEntry(new BoxShape(1,1,1), new Vector3(0, 50, 0), 1),
new CompoundShapeEntry(new BoxShape(1,1,1), new Vector3(1, 50.5f, 0), 1),
};
var cb2 = new CompoundBody(bodies, 4);
Space.Add(cb2);
game.ModelDrawer.Add(terrain);
game.Camera.Position = new Vector3(0, 30, 20);
}
Basically, I removed the single boxes, and added a compound object to the simulation. I also tried to kill the friction and in theory the boxes should slide around but they don't (or didn't when I tried it), whereas the single boxes do slide around.
As far as tracking down what I think the bug is, it looks like it might be this function in CompoundTerrainPairHandler.cs:
Code: Select all
protected override void UpdateContainedPairs()
{
//Could go other way; get triangles in mesh that overlap the compound.
//Could be faster sometimes depending on the way it's set up.
var overlappedElements = Resources.GetCompoundChildList();
compoundInfo.hierarchy.Tree.GetOverlaps(terrain.boundingBox, overlappedElements);
for (int i = 0; i < overlappedElements.count; i++)
{
TryToAdd(overlappedElements.Elements[i].CollisionInformation, terrain, overlappedElements.Elements[i].Material);
}
Resources.GiveBack(overlappedElements);
}
The TryToAdd() function doesn't send in the terrain material. It only sends in the other collidable's material. If I change that line to be:
Code: Select all
TryToAdd(overlappedElements.Elements[i].CollisionInformation, terrain, overlappedElements.Elements[i].Material, terrain.Material);
Then it looks like it works as intended.
Let me know what you make of this.
Cheers