NVIDIA-Omniverse / PhysX

NVIDIA PhysX SDK
BSD 3-Clause "New" or "Revised" License
2.61k stars 374 forks source link

PxD6 Joint Limits Exceeding -Pi and Pi #310

Open HerrDerSchoepfung opened 2 months ago

HerrDerSchoepfung commented 2 months ago

Library and Version

PhysX v5.3.1

Operating System

Windows 10

Steps to Trigger Behavior

  1. Create the PxD6 joint with the hinge behaviour
  2. Set Limits equal or higher than Pi

Possible Causes

Incorrect limit values Joint stiffness or damping issues External forces Numerical stability problems Joint configuration errors

Code Snippet to Reproduce Behavior

PxJoint* ConstraintHandler::AddHingeConstraint(V3SHingeConstraint^ constraint, PxRigidActor* actor0, PxRigidActor* actor1)
{
    pxScene->lockWrite();
    PxTransform frame0 = Helper::V3SMatrixToPxTransform(constraint->FrameA);
    PxTransform frame1 = Helper::V3SMatrixToPxTransform(constraint->FrameB);

    PxD6Joint* pxjoint = PxD6JointCreate(*pxPhysics, actor0, frame0, actor1, frame1);

    pxjoint->setMotion(PxD6Axis::eX, PxD6Motion::eLOCKED);
    pxjoint->setMotion(PxD6Axis::eY, PxD6Motion::eLOCKED);
    pxjoint->setMotion(PxD6Axis::eZ, PxD6Motion::eLOCKED);

    pxjoint->setMotion(PxD6Axis::eSWING1, PxD6Motion::eLOCKED);
    pxjoint->setMotion(PxD6Axis::eSWING2, PxD6Motion::eLOCKED);
    pxjoint->setMotion(PxD6Axis::eTWIST, PxD6Motion::eFREE);

    if (pxjoint == nullptr)
    {
        pxScene->unlockWrite();
        throw gcnew PhysicEngineException("Can not create the hinge constraint!");
    }

    if (constraint->AreLimitsEnabled)
    {
        PxJointAngularLimitPair twistLimit(constraint->LowerLimit, constraint->UpperLimit);

        pxjoint->setTwistLimit(twistLimit);
        pxjoint->setMotion(PxD6Axis::eTWIST, PxD6Motion::eLIMITED);
    }
    else
    {
        pxjoint->setMotion(PxD6Axis::eTWIST, PxD6Motion::eFREE);
    }

    PxD6JointDrive drive(10.0f, 100.0f, PX_MAX_F32, false);
    pxjoint->setDrive(PxD6Drive::eTWIST, drive);

#ifdef _DEBUG
    pxjoint->setConstraintFlag(PxConstraintFlag::eVISUALIZATION, true);
#endif

    pxScene->unlockWrite();
    return pxjoint;
}

void ConstraintHandler::UpdateHingeConstraint(V3SConstraint^ constraint)
{
    pxScene->lockWrite();
    auto hingeConstraint = safe_cast<V3SHingeConstraint^>(constraint);
    auto pxjoint = constraint2physx[constraint]->GetPxJoint()->is<PxD6Joint>();

    auto frame0 = Helper::V3SMatrixToPxTransform(hingeConstraint->FrameA);
    auto frame1 = Helper::V3SMatrixToPxTransform(hingeConstraint->FrameB);

    pxjoint->setLocalPose(physx::PxJointActorIndex::eACTOR0, frame0);
    pxjoint->setLocalPose(physx::PxJointActorIndex::eACTOR1, frame1);

    pxjoint->setMotion(PxD6Axis::eX, PxD6Motion::eLOCKED);
    pxjoint->setMotion(PxD6Axis::eY, PxD6Motion::eLOCKED);
    pxjoint->setMotion(PxD6Axis::eZ, PxD6Motion::eLOCKED);

    pxjoint->setMotion(PxD6Axis::eSWING1, PxD6Motion::eLOCKED);
    pxjoint->setMotion(PxD6Axis::eSWING2, PxD6Motion::eLOCKED);
    pxjoint->setMotion(PxD6Axis::eTWIST, PxD6Motion::eFREE);

    if (hingeConstraint->AreLimitsEnabled)
    {
        PxJointAngularLimitPair twistLimit(hingeConstraint->LowerLimit, hingeConstraint->UpperLimit);

        pxjoint->setTwistLimit(twistLimit);
        pxjoint->setMotion(PxD6Axis::eTWIST, PxD6Motion::eLIMITED);
    }
    else
    {
        pxjoint->setMotion(PxD6Axis::eTWIST, PxD6Motion::eFREE);
    }

    PxD6JointDrive drive(10.0f, 100.0f, PX_MAX_F32, false);
    pxjoint->setDrive(PxD6Drive::eTWIST, drive);

    pxjoint->setDrivePosition(PxTransform(PxQuat(hingeConstraint->TargetAngle, PxVec3(1.0f, 0.0f, 0.0f))));

    pxScene->unlockWrite();
}

Expected Behavior

The joint should maintain its limits and behave predictably within the specified range.

The PhysX Doc:

"virtual void setTwistLimit

Set the twist limit for the joint.

The twist limit controls the range of motion around the twist axis.

The limit angle range is (-2Pi, 2Pi).

See also

getTwistLimit() PxJointAngularLimitPair

Parameters limit – [in] the twist limit structure"

Actual Behavior

The box connected with the joint starts spinning without stopping at the assigned limit.