MADEAPPS / newton-dynamics

Newton Dynamics is an integrated solution for real time simulation of physics environments.
http://www.newtondynamics.com
Other
945 stars 182 forks source link

Rigid Body Position is NaN #175

Closed TrevorCash closed 5 years ago

TrevorCash commented 5 years ago

Hi Julio,

Sometimes I get a rigid body that has a position where the elements are NaN. I try to sense when this happens and manually correct the issue by setting the body's position back to what it should be. this works in the inline code (after I set the body's position I can re-read back the position and it seems corrected - I even invalidate the cache) But for some reason the body always reverts back to NaN in the next update.

Here is the code that tried to correct the problem:

        //applies the newtons body's body frame to the node.
    void NewtonRigidBody::ApplyTransformToNode()
    {
        if (!newtonBody_) {
            return;
        }

        dVector pos;
        dQuaternion quat;
        NewtonBodyGetPosition(newtonBody_, &pos[0]);
        NewtonBodyGetRotation(newtonBody_, &quat.m_x);

        Vector3 scenePos = NewtonToUrhoVec3(pos);

        if (scenePos.IsInf() || scenePos.IsNaN()) {
            URHO3D_LOGWARNING("Newton Body Position is Inf or Nan (trying to revert newtonbody matrix to the node's matrix");

            NewtonInvalidateCache(physicsWorld_->GetNewtonWorld());

            //reset the body's state
            NewtonBodySetMatrix(newtonBody_, &UrhoToNewton(node_->GetWorldTransform())[0][0]);
            dVector v = dVector(0, 0, 0);
            NewtonBodySetVelocity(newtonBody_, &v[0]);
            NewtonBodySetOmega(newtonBody_, &v[0]);

            //verify the state has changed back to valid position
            NewtonBodyGetPosition(newtonBody_, &pos[0]);
            NewtonBodyGetRotation(newtonBody_, &quat.m_x);
            URHO3D_LOGINFO(NewtonToUrhoVec3(pos).ToString()); //does indeed print out a valid position

            return;
        }

        node_->SetWorldPosition(scenePos);
        node_->SetWorldRotation(NewtonToUrhoQuat(quat).Normalized());

        lastSetNodeWorldTransform_ = node_->GetWorldTransform();
    }

I have also traced all the other places where the my code could be setting the body matrix to NaN - and I do not hit any of those breakpoints.

I hope this makes sense what I'm trying to do. I am still using and older commit: 2a50b8236f131ee9fd6cb906b2f0a8b5c3f2ad04.

image To be clear - in the above image - the piece just hangs in midair because the actual rigid body position is NaN and the code (purposefully) does not move the Urho3D node to a NaN position.

TrevorCash commented 5 years ago

Managed to find the root cause of this one - I was setting the world transform to a NaN position in some transformation code accidentally. Closing.

JulioJerez commented 5 years ago

oh glad you found it. I have taking a hiatus for the engine. But I am going to resume this month, to complete some of teh pending feature and bug fixes.

I liek to have the at a stable point before the year is out.

TrevorCash commented 5 years ago

Sounds Great! Yeah I am ironing out bugs in my game code - Its really starting to come together bit by bit I think.