include/PxContactModifyCallback.h

File members: include/PxContactModifyCallback.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_CONTACT_MODIFY_CALLBACK_H
#define PX_CONTACT_MODIFY_CALLBACK_H
#include "PxPhysXConfig.h"
#include "PxShape.h"
#include "PxContact.h"
#include "foundation/PxTransform.h"

#if !PX_DOXYGEN
namespace physx
{
#endif

class PxShape;

class PxContactSet
{
public:
    PX_FORCE_INLINE     const PxVec3& getPoint(PxU32 i) const           { return mContacts[i].contact;      }

    PX_FORCE_INLINE     void setPoint(PxU32 i, const PxVec3& p)         { mContacts[i].contact = p; }

    PX_FORCE_INLINE     const PxVec3& getNormal(PxU32 i) const          { return mContacts[i].normal;   }

    PX_FORCE_INLINE     void setNormal(PxU32 i, const PxVec3& n)
    {
        PxContactPatch* patch = getPatch();
        patch->internalFlags |= PxContactPatch::eREGENERATE_PATCHES;
        mContacts[i].normal = n;
    }

    PX_FORCE_INLINE     PxReal getSeparation(PxU32 i) const             { return mContacts[i].separation;   }

    PX_FORCE_INLINE     void setSeparation(PxU32 i, PxReal s)           { mContacts[i].separation = s; }

    PX_FORCE_INLINE     const PxVec3& getTargetVelocity(PxU32 i) const  { return mContacts[i].targetVelocity;   }

    PX_FORCE_INLINE     void setTargetVelocity(PxU32 i, const PxVec3& v)
    {
        PxContactPatch* patch = getPatch();
        patch->internalFlags |= PxContactPatch::eHAS_TARGET_VELOCITY;
        mContacts[i].targetVelocity = v;
    }

    PX_FORCE_INLINE     PxU32 getInternalFaceIndex0(PxU32 i)    const       { PX_UNUSED(i); return PXC_CONTACT_NO_FACE_INDEX; }

    PX_FORCE_INLINE     PxU32 getInternalFaceIndex1(PxU32 i)    const
    {
        PxContactPatch* patch = getPatch();
        if (patch->internalFlags & PxContactPatch::eHAS_FACE_INDICES)
        {
            return reinterpret_cast<PxU32*>(mContacts + mCount)[mCount + i];
        }
        return PXC_CONTACT_NO_FACE_INDEX;
    }

    PX_FORCE_INLINE     PxReal getMaxImpulse(PxU32 i) const             { return mContacts[i].maxImpulse;   }

    PX_FORCE_INLINE     void setMaxImpulse(PxU32 i, PxReal s)
    {
        PxContactPatch* patch = getPatch();
        patch->internalFlags |= PxContactPatch::eHAS_MAX_IMPULSE;
        mContacts[i].maxImpulse = s;
    }

    PX_FORCE_INLINE     PxReal getRestitution(PxU32 i) const            { return mContacts[i].restitution; }

    PX_FORCE_INLINE     void setRestitution(PxU32 i, PxReal r)
    {
        PxContactPatch* patch = getPatch();
        patch->internalFlags |= PxContactPatch::eREGENERATE_PATCHES;
        mContacts[i].restitution = r;
    }

    PX_FORCE_INLINE     PxReal getStaticFriction(PxU32 i) const { return mContacts[i].staticFriction; }

    PX_FORCE_INLINE     void setStaticFriction(PxU32 i, PxReal f)
    {
        PxContactPatch* patch = getPatch();
        patch->internalFlags |= PxContactPatch::eREGENERATE_PATCHES;
        mContacts[i].staticFriction = f;
    }

    PX_FORCE_INLINE     PxReal getDynamicFriction(PxU32 i) const { return mContacts[i].dynamicFriction; }

    PX_FORCE_INLINE     void setDynamicFriction(PxU32 i, PxReal f)
    {
        PxContactPatch* patch = getPatch();
        patch->internalFlags |= PxContactPatch::eREGENERATE_PATCHES;
        mContacts[i].dynamicFriction = f;
    }

    PX_FORCE_INLINE     void ignore(PxU32 i)                            { setMaxImpulse(i, 0.0f);   }

    PX_FORCE_INLINE     PxU32 size() const                              { return mCount; }

    PX_FORCE_INLINE     PxReal getInvMassScale0() const
    {
        PxContactPatch* patch = getPatch();
        return patch->mMassModification.linear0;
    }

    PX_FORCE_INLINE     PxReal getInvMassScale1() const
    {
        PxContactPatch* patch = getPatch();
        return patch->mMassModification.linear1;
    }

    PX_FORCE_INLINE     PxReal getInvInertiaScale0() const
    {
        PxContactPatch* patch = getPatch();
        return patch->mMassModification.angular0;
    }

    PX_FORCE_INLINE     PxReal getInvInertiaScale1() const
    {
        PxContactPatch* patch = getPatch();
        return patch->mMassModification.angular1;
    }

    PX_FORCE_INLINE     void setInvMassScale0(const PxReal scale)
    {
        PxContactPatch* patch = getPatch();
        patch->mMassModification.linear0 = scale;
        patch->internalFlags |= PxContactPatch::eHAS_MODIFIED_MASS_RATIOS;
    }

    PX_FORCE_INLINE     void setInvMassScale1(const PxReal scale)
    {
        PxContactPatch* patch = getPatch();
        patch->mMassModification.linear1 = scale;
        patch->internalFlags |= PxContactPatch::eHAS_MODIFIED_MASS_RATIOS;
    }

    PX_FORCE_INLINE     void setInvInertiaScale0(const PxReal scale)
    {
        PxContactPatch* patch = getPatch();
        patch->mMassModification.angular0 = scale;
        patch->internalFlags |= PxContactPatch::eHAS_MODIFIED_MASS_RATIOS;
    }

    PX_FORCE_INLINE     void setInvInertiaScale1(const PxReal scale)
    {
        PxContactPatch* patch = getPatch();
        patch->mMassModification.angular1 = scale;
        patch->internalFlags |= PxContactPatch::eHAS_MODIFIED_MASS_RATIOS;
    }

protected:

    PX_FORCE_INLINE PxContactPatch* getPatch() const
    {
        const size_t headerOffset = sizeof(PxContactPatch)*mCount;
        return reinterpret_cast<PxContactPatch*>(reinterpret_cast<PxU8*>(mContacts) - headerOffset);
    }

    PxU32                   mCount;
    PxModifiableContact*    mContacts;
};

class PxContactModifyPair
{
public:

    const PxRigidActor*     actor[2];

    const PxShape*          shape[2];

    PxTransform             transform[2];

    PxContactSet            contacts;
};

class PxContactModifyCallback
{
public:

    virtual void onContactModify(PxContactModifyPair* const pairs, PxU32 count) = 0;

protected:
    virtual ~PxContactModifyCallback(){}
};

class PxCCDContactModifyCallback
{
public:

    virtual void onCCDContactModify(PxContactModifyPair* const pairs, PxU32 count) = 0;

protected:
    virtual ~PxCCDContactModifyCallback(){}
};

#if !PX_DOXYGEN
} // namespace physx
#endif

#endif