NVIDIAGameWorks / FleX

Other
664 stars 100 forks source link

Unexpected Rigid body behavior #12

Open Hurleyworks opened 7 years ago

Hurleyworks commented 7 years ago

Howdy,

I'm having some problems implementing Flex rigid bodies in my application. In the attachment is a couple of simple objects that have the same model bounds, a video capture of the problems I'm seeing and a Flex demo scene that I used to create the video. The simulation was made using Flex 1.1.

The basic problem I'm hitting is that changing the particle size can introduce unexpected motions to the rigid body. In the test scene, a particle radius of .15 works as expected but changing the radius to .1 makes the body travel off in the +x direction. If I replace the concave.obj with the block.obj, which has slightly different geometry but the exact same bounding box, the body travels off in the -x direction.

Can someone tell me if I'm doing something wrong here or if this is an issue with Flex

Thanks!

FlexRigidProblem.zip

mmacklin commented 7 years ago

Hi there,

Thank you for the detailed repro case. I was able to confirm your results here, and I believe this is an issue of loss of numerical precision.

When using a spacing of 0.15 there are ~4k particles in the shape matching constraint, when using spacing of 0.1 there are ~13k particles in the constraint (count scales with the inverse cube of the spacing). Because shape-matching has to compute a sum over all particles for the center of mass and moment matrices, it eventually runs out of numerical precision, and results in numerical drift in some arbitrary direction.

Unfortunately there is no easy solution to this other than to limit the number of particles in an individual constraint. One workaround could be e.g.: overlapping multiple shape-matching constraints and increasing the iteration count to achieve the desired stiffness.

We are looking at supporting triangle-based collision detection in the future, which would avoid some of these problems by decoupling the particle discretization from the shape representation.

Cheers, Miles

Hurleyworks commented 7 years ago

Hi Miles,

Thanks for the quick response! Triangle-based collision detection would be a great feature to have.

One other problem I'm having is Flex seems to be calculating the center of mass differently than my application does. I just add up all the vertex positions and multiply by the inverse of the point count. But the value I get doesn't match Flex. Could you tell me how Flex calculates the center of mass?

int pntCount = triMesh.getVertexCount();
for (int i = 0; i < pntCount; i++)
{
    Vector3d pnt = points.row(i);
    pBody->t.centerOfVertexMass += pnt.cast<float>();
}
pBody->t.centerOfVertexMass *= 1.f /(float)pntCount;
ghost commented 7 years ago

Hi @Hurleyworks , maybe you're looking for function CalculateRigidCentersOfMass() in helpers.h (in the "demo" folder)

Hurleyworks commented 7 years ago

Thanks. Looks like that's new in 1.2 and I've been using Flex 1.1 until 1.2 is out of beta.