Search code examples
nanbulletphysics

Bullet Physics and NAN values


Sometimes some objects get NAN values in world simulated with Bullet Physics. (very rare) What is the correct way to handle it? Is there build in Bullet Physics functionality to detect such objects to avoid NAN viral effect. Why some objects are getting NANs. I do not apply NAN forces or impulses. I think it could be coursed by creation of 2 objects at same coords.


Solution

  • Two things:

    1) clamping velocity like this should prevent any body to speed up instantly

    btScalar speed = velocity.length();
    if(speed > speedLimit) {
        velocity *= speedLimit/speed;
        body->setLinearVelocity(velocity);
    }
    

    2) check for NaN values yourself, and use a safest default value or previous value

    if (isnan(someValue))
    {
        someValue = fallbackValue;
    }