Page 1 of 1

More precise collision for specific entities

Posted: Thu Jul 28, 2011 9:43 pm
by OddOneOut
So I have in my scene dropped weapons which are represented in physical space as boxes. They are quite thin and tend to sink through ground an cause strange physical glitches. So is there any way I can make the collision more precise like for example make them iterate more times in a frame or something. The physical boxes must be thin to make the weapons look like they are lying on the ground.

By the way, I just love this physics engine. Thanks from making it free and open-source!

Re: More precise collision for specific entities

Posted: Thu Jul 28, 2011 10:15 pm
by Norbo
Here's a few possibilities, ordered roughly from easiest to address/most likely to help to hardest/least likely:
1) This may be related to internal collision margins. This older thread describes how collision margins work and what they are used for: http://www.bepu-games.com/forums/viewto ... ?f=4&t=409. Some convex shapes use internal collision margins and others use external collision margins:
Internal margins:
BoxShape, CylinderShape, SphereShape, CapsuleShape
External margins:
ConeShape, ConvexHullShape, MinkowskiSumShape, TransformableShape, TriangleShape, WrappedShape

Note that some shape pairs, like box-box, will collide with the full, sharp-edged dimensions.

An external margin adds the spherical expansion to the outside of the dimensions of the object. An internal margin shrinks the dimensions of the object and re-adds the spherical expansion. In a box which is smaller than 2 * margin in some dimension, the resulting 'internal' dimension is negative which can cause all sorts of oddities.

So, if you want a box which is super tiny, you'll probably have to reduce the collision margin such that Width - 2 * margin > 0. The margin can be changed in the Entity.CollisionInformation.Shape.CollisionMargin property (if the Entity is a prefab entity type like Box).

2) Setting the entity's PositionUpdateMode to Continuous usually helps stop the most heinous failures like falling through the ground.

3) If you're using an older version (v0.15.2 or before), updating to v0.16.1 will improve triangle mesh collision behavior for small objects quite a bit.

4) Updating the engine more frequently is usually helpful for very tiny objects. This can be done by setting the Space.TimeStepSettings.TimeStepDuration to something lower than 1/60f, perhaps 1/120f. Passing in the elapsed time to the Space.Update method will make it take however many updates are necessary, or you can just call Space.Update() without a time parameter as many times as are necessary to keep up if you're already using a fixed time step in the game.

When using internal time stepping by passing an elapsed time into the Space.Update(dt) method, you may notice very slight jerky motion in some situations. This is because a varying number of updates are occurring to keep pace with the game time. Enabling the interpolation buffers and using interpolated properties for graphics will stop this problem. More information can be found in the asynchronous updating documentation: http://bepuphysics.codeplex.com/wikipag ... umentation. Note that you don't have to use a separate thread to make use of interpolation buffers- that's just a typical usage scenario.

5) There may be more simple sizing issues. If objects are too large, numerical issues can occur. A good rule of thumb is to try to stay in a 0.5 to 10 unit range for object dimensions- including for individual triangles in a mesh. For meshes in particular, these numerical problems will be most noticeable at the edges of triangles. If you notice the problem just as much in the middle of a triangle, then this probably isn't the issue because triangle interiors have a very robust special case.

When going for very small dimensions, you may run afoul of various tuning factors. These can be changed, but it's recommended to stick with the defaults. Here's a thread that describes most of them: http://www.bepu-games.com/forums/viewto ... f=4&t=1139

Re: More precise collision for specific entities

Posted: Thu Jul 28, 2011 11:12 pm
by OddOneOut
Thanks for the response! It was really useful. Setting the PositionUpdateMode to Continious seems to have fixed it almost completely. Now only I have a problem with shooting the weapons that are on the ground (which causes them to have really high velocities), but I think I can fix it with excluding it from the collision group.