Bepu 2 mesh shape causes crash!

Discuss any questions about BEPUphysics or problems encountered.
wanghongliang
Posts: 18
Joined: Sun May 05, 2019 2:01 pm

Bepu 2 mesh shape causes crash!

Post by wanghongliang »

I am using Bepu 2 as my physics engine and it works great for all convex shapes and compounds! When I add mesh shape to the simulation, it crashes without any clue, hope Norbo helps me by giving a full example of mesh shape collision, thanks
a lot!
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Bepu 2 mesh shape causes crash!

Post by Norbo »

The demos contain a few different examples, mostly relying on the DemoMeshHelper. There isn't much to it- those functions create a list of triangles and give them to a Mesh constructor.

Can you create a reproduction of the crash in the demos so I can take a look?
wanghongliang
Posts: 18
Joined: Sun May 05, 2019 2:01 pm

Re: Bepu 2 mesh shape causes crash!

Post by wanghongliang »

I am using a complicated concave mesh, maybe it's concavity causes instability?



Vector3[] meshVertices = ... // extract points
int[] meshIndices = ... //extract indices

int trianglesCount = meshIndices.Length / 3;

physics.BufferPool.Take<Triangle>(trianglesCount, out var buffer);

for (int i = 0; i < trianglesCount; i++)
{
ref Triangle t = ref buffer [ i ];

t.A = meshVertices[meshIndices[i * 3 + 0]].ToBEPU();
t.B = meshVertices[meshIndices[i * 3 + 1]].ToBEPU();
t.C = meshVertices[meshIndices[i * 3 + 2]].ToBEPU();

}

Mesh mesh = new Mesh(buffer, BepuVector3.One, physics.BufferPool);
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Bepu 2 mesh shape causes crash!

Post by Norbo »

That code looks fine, and concavity isn't an issue.

There may be something about the data which isn't playing nice, like degenerate triangles- but it's hard to say.

In order to know for sure, I would need an example that reproduces the crash that I can run personally.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Bepu 2 mesh shape causes crash!

Post by Norbo »

Oh, one possibility: when you request a buffer from the BufferPool, it does not give you back a buffer of exactly that size. It only guarantees a buffer of *at least* that size. The Mesh uses the input buffer's length, so if there is uninitialized data near the end of the buffer, that could cause all sorts of bad things. If this is the problem, then shrinking the buffer's length to trianglesCount would fix it.

Edit: even if this doesn't turn out to be the true problem in this case, I'll probably be changing the behavior of BufferPool.Take: https://github.com/bepu/bepuphysics2/issues/70
wanghongliang
Posts: 18
Joined: Sun May 05, 2019 2:01 pm

Re: Bepu 2 mesh shape causes crash!

Post by wanghongliang »

Thank you very much for quick response, I am trying to figure out why...
wanghongliang
Posts: 18
Joined: Sun May 05, 2019 2:01 pm

Re: Bepu 2 mesh shape causes crash!

Post by wanghongliang »

The code works fine now! Thanks again!

Mesh mesh = new Mesh(buffer.Slice(0,trianglesCount), BepuVector3.One, physics.BufferPool);

Maybe we can add TakeExact method to the BufferPool class so to eliminate some misunderstandings?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Bepu 2 mesh shape causes crash!

Post by Norbo »

Maybe we can add TakeExact method to the BufferPool class so to eliminate some misunderstandings?
Yup, just added something like that to the todo list: https://github.com/bepu/bepuphysics2/issues/70
wanghongliang
Posts: 18
Joined: Sun May 05, 2019 2:01 pm

Re: Bepu 2 mesh shape causes crash!

Post by wanghongliang »

Great!
tomweiland
Posts: 99
Joined: Wed May 08, 2019 12:17 am

Re: Bepu 2 mesh shape causes crash!

Post by tomweiland »

Hey, I'm in a very similar situation.

I'm also trying to get a mesh to work properly, however it's not causing a crash for me. Instead, nothing seems to collide with it. That, or it's not placing the mesh where I tell it to (I'm using BepuPhysics server side so I don't have a direct visual representation of the collider, so I can't tell if that's an issue, although it really shouldn't be).

My code is also quite similar to what was posted above, but I've come to a complete loss for ideas. I was initially attempting to load a more complex mesh, but after that failed to work I tried it with a regular cube (so no degenerate triangles) with no luck.

Code: Select all

Vector3[] _vertices = // Get vertices
int[] _indices = // Get indices
int _indexCount = _indices.Length;

Globals.physics.bufferPool.Take<Triangle>(_indexCount / 3, out var _triangleBuffer);
int _triangleIndex = 0;
for (int i = 0; i < _indices.Length; i += 3)
{
    _triangleBuffer[_triangleIndex] = new Triangle(_vertices[_indices[i]], _vertices[_indices[i + 1]], _vertices[_indices[i + 2]]);
    _triangleIndex++;
}
                   
Mesh _mesh = new Mesh(_triangleBuffer.Slice(0, _indexCount / 3), Vector3.One, Globals.physics.bufferPool);
After some debugging, I noticed that the _triangleBuffer always seems to have a length of 14, despite the fact that the cube only has 12 triangles. I'm assuming the Slice() method takes care of the resizing you were talking about above?

But yeah, as I said before...I have no idea what to do, so any help would be greatly appreciated!
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Bepu 2 mesh shape causes crash!

Post by Norbo »

I'm assuming the Slice() method takes care of the resizing you were talking about above?
Yup.
But yeah, as I said before...I have no idea what to do, so any help would be greatly appreciated!
I suspect it's related to winding- triangles are one sided, so only collisions coming from the direction that views the triangle as wound clockwise actually generate contacts.

Notably, I didn't remember which winding it was off the top of my head, and Mesh doesn't have any documentation in it about this. I'll fix that in a minute.
tomweiland
Posts: 99
Joined: Wed May 08, 2019 12:17 am

Re: Bepu 2 mesh shape causes crash!

Post by tomweiland »

Norbo wrote: Wed May 08, 2019 1:43 am I suspect it's related to winding- triangles are one sided, so only collisions coming from the direction that views the triangle as wound clockwise actually generate contacts.
This did occur to me so I tried using

Code: Select all

_triangleBuffer[_triangleIndex] = new Triangle(_vertices[_indices[i + 2]], _vertices[_indices[i + 1]], _vertices[_indices[i]]);
instead of

Code: Select all

_triangleBuffer[_triangleIndex] = new Triangle(_vertices[_indices[i]], _vertices[_indices[i + 1]], _vertices[_indices[i + 2]]);
but that didn't help either.

I would have thought that reversing the vertex order would change the winding direction...is that incorrect?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Bepu 2 mesh shape causes crash!

Post by Norbo »

I would have thought that reversing the vertex order would change the winding direction...is that incorrect?
Nope that's correct- it should have flipped the winding.

Unfortunately I can't think of anything else off the top of my head. Can you reproduce the issue in the Demos project?
tomweiland
Posts: 99
Joined: Wed May 08, 2019 12:17 am

Re: Bepu 2 mesh shape causes crash!

Post by tomweiland »

This is probably a relatively simple process, but I haven't actually ever run any of the demos. When I run the Demos solution, I just get a console window :?

On a side note, when I add the meshes (the ones I was actually trying to use, not the cube) to a compound, I can collide with them (or at least one of them) but it's offset quite a bit and seems to have been scaled up...I can't tell if the other meshes actually exist, but just not where they're supposed to be, or if some of them just aren't working. Is there anything about adding meshes to compounds that could affect the scaling/positioning which I may not be aware of?

Thanks for your help by the way!
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Bepu 2 mesh shape causes crash!

Post by Norbo »

This is probably a relatively simple process, but I haven't actually ever run any of the demos. When I run the Demos solution, I just get a console window :?
That's mysterious! No crash, no other window, nothing printed in the console window itself? If you attach the debugger and break during that, where is it stuck at?
Is there anything about adding meshes to compounds that could affect the scaling/positioning which I may not be aware of?
The overloads of Build* that output a center perform recentering (that is, moving local child offsets such that they are relative to the computed center of mass of the compound) but that's it.

(I'm a bit surprised that works at all, actually- compounds don't technically support nonconvex child shapes like the Mesh. Unless you mean adding the triangles individually.)

Super long shot: are any relevant orientation quaternions not unit length?
Post Reply