Inconsistent collision errors with 2D btBvhTriangleMeshShape

Post Reply
Megadanxzero
Posts: 13
Joined: Fri Sep 07, 2012 9:50 pm

Inconsistent collision errors with 2D btBvhTriangleMeshShape

Post by Megadanxzero »

I should probably start by saying I'm very new to Bullet, so I don't really know much about what I'm doing so any help I can get would be greatly appreciated.

With that out the way, I'm using a btBvhTriangleMeshShape for levels in a game I'm currently making, creating it using a btTriangleMesh and just adding triangles to it in a loop. I've noticed recently that certain triangles in the collision mesh seem to sometimes be ignored, but the strangest thing is that sometimes the same triangle will collide fine. I initially thought that maybe the loop was somehow missing certain triangles, but since it only happens some of the time that can't be the case. It also seems to depend somewhat on the direction objects are travelling in.

I made a short video to demonstrate what happens:
http://www.youtube.com/watch?v=4rHZWV4CVaw

The first 10 seconds or so is moving using my own 'kinematic' character controller, but the rest of the video is simply using the standard Bullet physics simulation, and as you can see the same thing happens with both. In this specific case it seems to happen more often when moving right to left, but it varies depending on the terrain.

If anyone has any idea what could be causing this I'd really appreciate the help 'cause I don't know much about Bullet and really can't think of any reason why it would be so inconsistent!

Edit: Realised that video isn't great, so here's another one that's zoomed in closer and shows the mesh in wireframe
http://www.youtube.com/watch?v=swtOV1lTZQk
Last edited by Megadanxzero on Sun Dec 02, 2012 2:12 am, edited 1 time in total.
marios
Posts: 52
Joined: Mon Jul 19, 2010 3:11 am
Contact:

Re: Inconsistent collision errors in btBvhTriangleMeshShape

Post by marios »

If this is 2 dimensional triangle mesh(so all z coordinates are zero) i suggest creating convex hull for each triangle instead btBvhTriangleMeshShape. I was experimenting with 2d terrain some time ago and with mesh this didn't worked good. So I ended with primitives encapsulated with btConvex2dShape and btBox2dShape
Megadanxzero
Posts: 13
Joined: Fri Sep 07, 2012 9:50 pm

Re: Inconsistent collision errors in btBvhTriangleMeshShape

Post by Megadanxzero »

Ah, yeah it is a 2D triangle mesh, I probably should have mentioned that. But yeah that kinda sucks if the issue is the btBvhTriangleMeshShape... I should look into using lots of btConvex2DShapes, but I have a feeling that would end up being incredibly slow because, depending on the complexity of the terrain, the levels are already between 10 and 20000 triangles and could potentially have way more than that if they're very complex. I'm already getting fairly low framerates with my more complex test levels so anything slower might not be usable :(

But yeah, I'll definitely give that a try 'cause I can't really think of any other options if it is just a problem with btBvhTriangleMeshShape... Thanks for the suggestion!
jeff
Posts: 6
Joined: Thu Aug 02, 2012 12:40 am

Re: Inconsistent collision errors with 2D btBvhTriangleMeshS

Post by jeff »

How are you rotating the btBvhTriangleMeshShape? If you're using setWorldTransform() instead of applying torque, that could potentially cause problems.
Megadanxzero
Posts: 13
Joined: Fri Sep 07, 2012 9:50 pm

Re: Inconsistent collision errors with 2D btBvhTriangleMeshS

Post by Megadanxzero »

The btBvhTriangleMeshShape isn't being rotated at all in this example. It's just a static mesh used for terrain.
Megadanxzero
Posts: 13
Joined: Fri Sep 07, 2012 9:50 pm

Re: Inconsistent collision errors with 2D btBvhTriangleMeshS

Post by Megadanxzero »

Right so I've tried this out, putting all the triangles from my triangle mesh into btTriangleShapeEx and then btConvex2dShapes. Then I've added all of those into a btCompoundShape to use for collision. Unfortunately, as I suspected, this is incredibly slow compared to using the btBvhTriangleMeshShape, making it unusable even for my simpler test levels. I could probably improve this by dynamically converting the triangles into much larger convex shapes so there are fewer of them, but as far as I can tell all the algorithms to do that are also incredibly slow, and since I'm using dynamically destroyable terrain I need the terrain regeneration to be as close to instant as possible. :(

I'm wondering if there isn't simply a better way to do this? I thought that maybe btCompoundShape would have similar performance to btBvhTriangleMeshShape, but maybe there's just something wrong with the way I'm creating it, 'cause as I said I'm pretty new to Bullet. I'm also curious about what exactly is done differently in the btConvex2dShape that makes it work better for this, and it seems strange that there's no 2D option for concave shapes, at least as far as I can tell.
marios
Posts: 52
Joined: Mon Jul 19, 2010 3:11 am
Contact:

Re: Inconsistent collision errors with 2D btBvhTriangleMeshS

Post by marios »

You are totally right. I don't think that there is better way of doing that in current bullet version than convex 2d shapes. Actually, I see you are trying to do what I managed to do some time ago.

http://www.youtube.com/watch?v=qKyUggLSw54

But the performance is really bad (that's why I don't use destructible terrain right now). About 2000-3000 triangles and simulation incredibly slows down. So, I think we need some new collision shape, something like bt2dTriangleMesh, but I don't feel that smart enough to code such one.
Megadanxzero
Posts: 13
Joined: Fri Sep 07, 2012 9:50 pm

Re: Inconsistent collision errors with 2D btBvhTriangleMeshS

Post by Megadanxzero »

Right, so I think I may have had a bit of a breakthrough with this. I decided to work off the theory that the issue with the btBvhTriangleMeshShape is that polygons are technically infinitely thin, and so when you use it for a 2D collision mesh, and everything is colliding with the polygons from the side, it's not unreasonable for it to miss them. Think of it like looking at the edge of a piece of paper, it's so thin it could potentially be considered to not even be there, so stuff could just pass right through it. Usually in a 3D physics engine things are going to be colliding with the flat polygon faces, so that's presumably the behaviour which works best in Bullet. I'm guessing the 2D shapes that exist in Bullet do something different to ensure that collisions from the side work properly, since they seem to work fine. Or maybe it's just the way the convex collision algorithm works...

Image

So anyway with that in mind I decided to change my mesh generation to make it a bit more Bullet-friendly. Previously I was triangulating a 2D grid of vertices which represented the edge of my terrain (Using a nice little library called poly2tri), but I've changed it to instead 'extrude' each pair of vertices on the edge into 4 vertices to create a 2-unit-wide quad of two triangles (From -1 to 1, but really it could probably be any width as long as it's greater than 0). So far it seems to work perfectly! I haven't experienced any penetration of the terrain edge, but I'll probably need to test it out a bit more before I can be certain. Here's a little image to show the basic idea:

Image

There is one fairly big problem with this method though, that you can kinda see in the above image, which is that it increases the amount of triangles in my collision mesh by a lot compared to the old method. It seems to be around twice as many in most cases but could probably be more... On the plus side a btBvhTriangleMeshShape seems to be fine with around 20000 vertices, so even with twice as many triangles you'll be fine up to about 10000 of your old ones, which is better than the btCompoundShape method at least. Unfortunately some of mine were getting up to around 20000 vertices anyway so I'm gonna have to try and think of a way of reducing it a bit more.

But anyway, yay, progress! :3

EDIT: Ok I tried halving the number of triangles by only using 1 triangle instead of 2 for each section. Two vertices on the left with Z values of -1 and 1, and then one vertex on the right with a Z value of 0. That seems to work fine as well provided the things colliding with it are btConvex2dShapes (Which all my dynamic objects are). I'm not sure I trust it with the right side of each triangle being 'infinitely' thin again though, so I'll have to keep an eye out for any errors...
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA
Contact:

Re: Inconsistent collision errors with 2D btBvhTriangleMeshS

Post by Erwin Coumans »

We could add some option to the btBvhTriangleMeshShape to generate 2D triangles, so it will be processed similar to the btConvex2dShapes.

Have you tried creating a single btConvex2dShape for each triangle, and add all of those btConvex2DShape to a single btCompoundShape?
That way, the acceleration structure in btCompoundShape should speed up things, and the performance should be similar to btBvhTriangleMeshShape.

Thanks!
Erwin
Megadanxzero
Posts: 13
Joined: Fri Sep 07, 2012 9:50 pm

Re: Inconsistent collision errors with 2D btBvhTriangleMeshS

Post by Megadanxzero »

Yeah I mentioned doing that in this post (http://bulletphysics.org/Bullet/phpBB3/ ... 640#p29413) 'cause I thought the btCompoundShape would be faster, but for some reason it seems to be a lot slower than btBvhTriangleMeshShape. I guess maybe 'cause it's not designed to be used with such a huge number of separate objects? To be honest I was quite surprised that btBvhTriangleMeshShape handled it so smoothly. :3

My new method of generating it seems to be working perfectly against btConvex2DShapes so far though, so as long as I don't need to collide my terrain against any other concave shapes hopefully it should be fine! (Thankfully I don't expect to ever need to do that)
marios
Posts: 52
Joined: Mon Jul 19, 2010 3:11 am
Contact:

Re: Inconsistent collision errors with 2D btBvhTriangleMeshS

Post by marios »

That's quite nice! So now you have 3 vertices representing two dimensional edge? am I right? So, every 2 vertices in 2d are represented by 3 vertices. It has still some redundancy but this isn't big problem. I will be afraid about something else. I mean that now your terrain is very thin and there maybe some tunneling issues.

There is also one possibility (not sure if better) to create terrain: convexHull encapsulated with convex2dShape with only two vertices. But this will be also very thin edge
Post Reply