NaN Exceptions...

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
_untitled_
Posts: 32
Joined: Sat Jan 19, 2013 8:20 pm

NaN Exceptions...

Post by _untitled_ »

I'm frequently getting NaN exceptions with BEPU.

Here is one of the many stack traces I get (sometimes it's ForceUpdater)

Code: Select all

System.NotFiniteNumberException was unhandled
  HResult=-2146233048
  Message=Invalid value.
  Source=BEPUphysics
  OffendingNumber=0.0
  StackTrace:
       at BEPUphysics.MathExtensions.MathChecker.Validate(Vector3 v)
       at BEPUphysics.Entities.Entity.ApplyLinearImpulse(Vector3& impulse)
       at BEPUphysics.Constraints.Collision.ContactPenetrationConstraint.ExclusiveUpdate()
       at BEPUphysics.Constraints.SolverGroups.SolverGroup.ExclusiveUpdateUpdateable(EntitySolverUpdateable item)
       at BEPUphysics.Constraints.Collision.NonConvexContactManifoldConstraint.ExclusiveUpdate()
       at BEPUphysics.SolverSystems.Solver.UnsafePrestep(SolverUpdateable updateable)
       at BEPUphysics.SolverSystems.Solver.UpdateSingleThreaded()
       at BEPUphysics.MultithreadedProcessingStage.Update()
       at BEPUphysics.Space.DoTimeStep()
       at BEPUphysics.Space.Update(Single dt)
What can cause NaN's?
Poorly generated terrain mesh? Objects with low mass? Objects with high mass? Objects that are too fast? Objects that are too fast and that have low mass?

I suspect it could be the terrain, since my terrain mesh can be considered "complex":
Image

As you can see, the game is basically like minecraft except surface blocks can have a heightmap of sorts to allow smooth terrain.
Blocks that do not have heightmaps are traditional in the sense that each of their faces has two triangles.

The render artifact from the debug drawer in that image also worries me a bit.

Heightmapped blocks have a more complex triangle construction:
Image

I was able to catch one of these NaN errors:
Image
The circled boxes were floating in mid-air, presumably because they had NaN values.

For some reason, once the other boxes disappeared (I had them disappear after 10 seconds), the NaN values would "revert" to real values and the last box would start moving again.

Strange...any ideas?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: NaN Exceptions...

Post by Norbo »

The most common causes of NaNs are degenerate geometry (e.g. triangle which is actually a line or a point) and invalid input (e.g. setting an entity's LinearVelocity explicitly to infinity or something).

In this case, based on where the NaN was caught by the MathChecker, it is likely that it's related to degenerate geometry in the mesh.

If you aren't already, try using the latest development version. A recent change may avoid the crash. Even if it fixes the issue, getting rid of the degenerate triangles is a good idea as they're just wasting cycles.

If the latest version does not address it, there may be another way for degenerate triangles to sneak through that I have not yet protected against. In this case, giving me a mesh which reproduces the issue would be very helpful in tracking it down. (Degenerate triangles aren't exactly 'supported geometry,' but I prefer graceful handling over explosive exceptions.)
_untitled_
Posts: 32
Joined: Sat Jan 19, 2013 8:20 pm

Re: NaN Exceptions...

Post by _untitled_ »

I'd love to give you some sample mesh-is there any way I can serialize a mesh?
If so, I could simply dump the mesh of the terrain and hand that to you.

Also, could the Box have degenerate triangles? The fact that the box was floating in midair and not touching anything leads me to suspect that.

I will try the development version.

EDIT: I guess there could be some degenerate geometry that I could be creating. Sometimes, the height of a block can be 0, which results in a triangle where all vertices are at the same height - resulting in essentially a line.

Due to the nature of my noise function, sometimes the following occurs:
Image
And hence, I clip some heights to 0.

Here's a better diagram:
Image

Could that be a cause?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: NaN Exceptions...

Post by Norbo »

I'd love to give you some sample mesh-is there any way I can serialize a mesh?
Not built into the physics engine, no.
Also, could the Box have degenerate triangles? The fact that the box was floating in midair and not touching anything leads me to suspect that.
If the Box was a Box entity, it is not composed of triangles. BoxShapes are sampled directly as solid, convex boxes. Further, degenerate triangles only cause problems when something actually interacts with them. My guess is that the floating boxes are secondary victims of the NaN infection. This is particularly likely if the simulation was forced to continue after a NaN was detected.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: NaN Exceptions...

Post by Norbo »

I guess there could be some degenerate geometry that I could be creating. Sometimes, the height of a block can be 0, which results in a triangle where all vertices are at the same height - resulting in essentially a line.
...
Could that be a cause?
If a triangle has 0 area, it is degenerate and could cause problems (it will cause problems in versions that aren't the latest development version).
_untitled_
Posts: 32
Joined: Sat Jan 19, 2013 8:20 pm

Re: NaN Exceptions...

Post by _untitled_ »

Norbo wrote:
I guess there could be some degenerate geometry that I could be creating. Sometimes, the height of a block can be 0, which results in a triangle where all vertices are at the same height - resulting in essentially a line.
...
Could that be a cause?
If a triangle has 0 area, it is degenerate and could cause problems (it will cause problems in versions that aren't the latest development version).
What about two triangles on top of each other? (that is to say, they occupy the same space)
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: NaN Exceptions...

Post by Norbo »

If both triangles have area, that should be fine- there are no known problems there. But, just because there is no known problem does not mean there does not exist a problem. :)
_untitled_
Posts: 32
Joined: Sat Jan 19, 2013 8:20 pm

Re: NaN Exceptions...

Post by _untitled_ »

It appears that either setting a minimum height or using the development release has fixed the issue.

Thanks!
Post Reply