Deformable Terrain

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
hahanoob
Posts: 5
Joined: Sun Feb 06, 2011 5:07 am

Deformable Terrain

Post by hahanoob »

I'm using terrain from a heightmap where basically each point on the heightmap is a 4 vertices (I''m doing this so I can get vertical cliffs and sort of a Minecraft look). The terrain object didn't seem applicable in my case so I'm looking at using TriangleMesh and StaticTriangleGroup instead.

Here's roughly what I'm doing:

Code: Select all

// Convert my vertices BEPU vertex format (Is there a better way to do this?)
StaticTriangleGroup.StaticTriangleGroupVertex[] vertices = new StaticTriangleGroup.StaticTriangleGroupVertex[_vertices.Count];
for (int i = 0; i < _vertices.Count; ++i)
{
        vertices[i] = new StaticTriangleGroup.StaticTriangleGroupVertex(_vertices[i].Position);
}

int[] indices = new int[_indices.Count];
for (int i = 0; i < _indices.Count; ++i)
{
        indices[i] = _indices[i];
}

TriangleMesh collision = new TriangleMesh(vertices, indices);

_space.Add(new StaticTriangleGroup(collision));
Anyways, I'd like to be able to have explosions and stuff carve out large areas of the ground. The naive way to do this seems to be to just create a new StaticTriangleGroup every time my vertices change. Unfortunately, with about 3000 vertices the above takes around 20ms. What is the correct way to do this? My intuition says I should be able to have StaticTriangleGroup be directly based on my vertex and index lists (which is what I'll be updating when I modify the terrain).

Thanks for any help!
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Deformable Terrain

Post by Norbo »

One option would be to change the StaticTriangleGroup.TriangleMesh.Vertices positions (indexed as if they were your own vertex list) and then call StaticTriangleGroup.TriangleMesh.Hierachy.Refit. This would prevent any garbage from being created, and would be substantially faster than reconstructing the triangle mesh hierarchy (which is what would happen if a new TriangleMesh/Group get made each time). A refit for ~3000 triangles will still take a few milliseconds, though.

In v0.15.0, the special vertex type is going away too. Instead, the vertices are stored just as a Vector3 array.
hahanoob
Posts: 5
Joined: Sun Feb 06, 2011 5:07 am

Re: Deformable Terrain

Post by hahanoob »

Thanks for the quick answer! I'm not sure if that will work though since potentially I'll need to add or remove vertices from the list entirely. Like if an area is even and a dig a square hole into it that's 4 extra faces.

My other thought is to use boxes but my terrain is actually going to end up being composed of somewhat arbitrary hexahedrons. Perhaps I could just manage a list of triangle entities directly? Or maybe a list of ConvexHull?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Deformable Terrain

Post by Norbo »

I'm not sure if that will work though since potentially I'll need to add or remove vertices from the list entirely. Like if an area is even and a dig a square hole into it that's 4 extra faces.
Yeah, changing the topology of the mesh will break the hierarchy. It might be possible to include the degenerate face triangles in the original mesh so the topology never changes, but I can't say for sure off the top of my head how the collision system would behave with that kind of degenerate geometry.
Perhaps I could just manage a list of triangle entities directly? Or maybe a list of ConvexHull?
It might work. Having a massive number of triangles hanging out in the broadphase isn't ideal, but you could give it a shot and see if it can handle it. A list of ConvexHulls might work too; I'd use whichever feels more natural. ConvexHulls in v0.14.3 and before might have a slow startup time.

However, triangles allow you to force collisions to use their normals which would help prevent bumps when things slide across a flat triangulated plane. ConvexHulls have no such feature, and would likely cause noticeable bumps when trying to slide across borders.

The 'best' solution to this problem would be something currently on the to-do list. In the late stages of v0.15.0 or possibly a little after its release, a new dynamic acceleration structure is planned that can handle changes to mesh topology without having to do a big rebuild process. The initial structure construction phase will be a lot faster too.
hahanoob
Posts: 5
Joined: Sun Feb 06, 2011 5:07 am

Re: Deformable Terrain

Post by hahanoob »

I was finally able to get around to testing this. Using the triangles seems to be working for now though I seem to be getting stuck a lot in the terrain unless I move really slowly. Strange. I'll have to get the debug drawing working and see what is happening.

Thanks!
Post Reply