include/extensions/PxJointLimit.h

File members: include/extensions/PxJointLimit.h

// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//  * Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
//  * Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//  * Neither the name of NVIDIA CORPORATION nor the names of its
//    contributors may be used to endorse or promote products derived
//    from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Copyright (c) 2008-2023 NVIDIA Corporation. All rights reserved.
// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.

#ifndef PX_JOINT_LIMIT_H
#define PX_JOINT_LIMIT_H
#include "foundation/PxMath.h"
#include "common/PxTolerancesScale.h"
#include "extensions/PxJoint.h"
#include "PxPhysXConfig.h"

#if !PX_DOXYGEN
namespace physx
{
#endif

class PxJointLimitParameters
{
public:
    PxReal restitution;

    PxReal bounceThreshold;

    PxReal stiffness;

    PxReal damping;

    PxJointLimitParameters() :
        restitution     (0.0f),
        bounceThreshold (0.0f),
        stiffness       (0.0f),
        damping         (0.0f)
    {
    }

    PxJointLimitParameters(const PxJointLimitParameters& p) :
        restitution     (p.restitution),
        bounceThreshold (p.bounceThreshold),
        stiffness       (p.stiffness),
        damping         (p.damping)
    {
    }

    PX_INLINE bool isValid() const
    {
        return  PxIsFinite(restitution) && restitution >= 0 && restitution <= 1 &&
                PxIsFinite(stiffness) && stiffness >= 0 &&
                PxIsFinite(damping) && damping >= 0 &&
                PxIsFinite(bounceThreshold) && bounceThreshold >= 0;
    }

    PX_INLINE bool isSoft() const
    {
        return damping>0 || stiffness>0;
    }

protected:
    ~PxJointLimitParameters() {}
};

class PxJointLinearLimit : public PxJointLimitParameters
{
public:
    PxReal value;

    PxJointLinearLimit(PxReal extent) : value(extent)
    {
    }

    PxJointLinearLimit(PxReal extent, const PxSpring& spring) : value(extent)
    {
        stiffness = spring.stiffness;
        damping = spring.damping;
    }

    PX_INLINE bool isValid() const
    {
        return PxJointLimitParameters::isValid() &&
               PxIsFinite(value) &&
               value > 0.0f;
    }
};

class PxJointLinearLimitPair : public PxJointLimitParameters
{
public:
    PxReal upper, lower;

    PxJointLinearLimitPair(const PxTolerancesScale& scale, PxReal lowerLimit = -PX_MAX_F32/3.0f, PxReal upperLimit = PX_MAX_F32/3.0f) :
        upper(upperLimit),
        lower(lowerLimit)
    {
        bounceThreshold = 2.0f*scale.length;
    }

    PxJointLinearLimitPair(PxReal lowerLimit, PxReal upperLimit, const PxSpring& spring) :
        upper(upperLimit),
        lower(lowerLimit)
    {
        stiffness = spring.stiffness;
        damping = spring.damping;
    }

    PX_INLINE bool isValid() const
    {
        return PxJointLimitParameters::isValid() &&
               PxIsFinite(upper) && PxIsFinite(lower) && upper >= lower &&
               PxIsFinite(upper - lower);
    }
};

class PxJointAngularLimitPair : public PxJointLimitParameters
{
public:
    PxReal upper, lower;

    PxJointAngularLimitPair(PxReal lowerLimit, PxReal upperLimit) :
        upper(upperLimit),
        lower(lowerLimit)
    {
        bounceThreshold = 0.5f;
    }

    PxJointAngularLimitPair(PxReal lowerLimit, PxReal upperLimit, const PxSpring& spring) :
        upper(upperLimit),
        lower(lowerLimit)
    {
        stiffness = spring.stiffness;
        damping = spring.damping;
    }

    PX_INLINE bool isValid() const
    {
        return PxJointLimitParameters::isValid() &&
               PxIsFinite(upper) && PxIsFinite(lower) && upper >= lower;
    }
};

class PxJointLimitCone : public PxJointLimitParameters
{
public:
    PxReal yAngle;

    PxReal zAngle;

    PxJointLimitCone(PxReal yLimitAngle, PxReal zLimitAngle) :
        yAngle(yLimitAngle),
        zAngle(zLimitAngle)
    {
        bounceThreshold = 0.5f;
    }

    PxJointLimitCone(PxReal yLimitAngle, PxReal zLimitAngle, const PxSpring& spring) :
        yAngle(yLimitAngle),
        zAngle(zLimitAngle)
    {
        stiffness = spring.stiffness;
        damping = spring.damping;
    }

    PX_INLINE bool isValid() const
    {
        return PxJointLimitParameters::isValid() &&
               PxIsFinite(yAngle) && yAngle>0 && yAngle<PxPi &&
               PxIsFinite(zAngle) && zAngle>0 && zAngle<PxPi;
    }
};

class PxJointLimitPyramid : public PxJointLimitParameters
{
public:
    PxReal yAngleMin;

    PxReal yAngleMax;

    PxReal zAngleMin;

    PxReal zAngleMax;

    PxJointLimitPyramid(PxReal yLimitAngleMin, PxReal yLimitAngleMax, PxReal zLimitAngleMin, PxReal zLimitAngleMax) :
        yAngleMin(yLimitAngleMin),
        yAngleMax(yLimitAngleMax),
        zAngleMin(zLimitAngleMin),
        zAngleMax(zLimitAngleMax)
    {
        bounceThreshold = 0.5f;
    }

    PxJointLimitPyramid(PxReal yLimitAngleMin, PxReal yLimitAngleMax, PxReal zLimitAngleMin, PxReal zLimitAngleMax, const PxSpring& spring) :
        yAngleMin(yLimitAngleMin),
        yAngleMax(yLimitAngleMax),
        zAngleMin(zLimitAngleMin),
        zAngleMax(zLimitAngleMax)
    {
        stiffness = spring.stiffness;
        damping = spring.damping;
    }

    PX_INLINE bool isValid() const
    {
        return PxJointLimitParameters::isValid() &&
                PxIsFinite(yAngleMin) && yAngleMin>-PxPi && yAngleMin<PxPi &&
                PxIsFinite(yAngleMax) && yAngleMax>-PxPi && yAngleMax<PxPi &&
                PxIsFinite(zAngleMin) && zAngleMin>-PxPi && zAngleMin<PxPi &&
                PxIsFinite(zAngleMax) && zAngleMax>-PxPi && zAngleMax<PxPi &&
                yAngleMax>=yAngleMin && zAngleMax>=zAngleMin;
    }
};

#if !PX_DOXYGEN
} // namespace physx
#endif

#endif