Skip to content

Commit

Permalink
Added CollisionCollector::OnBodyEnd and deduplicated some code
Browse files Browse the repository at this point in the history
  • Loading branch information
jrouwe committed Jan 19, 2025
1 parent 6ae6658 commit 005d4c0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 75 deletions.
3 changes: 3 additions & 0 deletions Jolt/Physics/Collision/CollisionCollector.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ class CollisionCollector
/// before AddHit is called (e.g. the user data pointer or the velocity of the body).
virtual void OnBody([[maybe_unused]] const Body &inBody) { /* Collects nothing by default */ }

/// When running a query through the NarrowPhaseQuery class, this will be called after all AddHit calls have been made for a particular body.
virtual void OnBodyEnd() { /* Does nothing by default */ }

/// Set by the collision detection functions to the current TransformedShape that we're colliding against before calling the AddHit function
void SetContext(const TransformedShape *inContext) { mContext = inContext; }
const TransformedShape *GetContext() const { return mContext; }
Expand Down
10 changes: 10 additions & 0 deletions Jolt/Physics/Collision/InternalEdgeRemovingCollector.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ JPH_NAMESPACE_BEGIN

/// Removes internal edges from collision results. Can be used to filter out 'ghost collisions'.
/// Based on: Contact generation for meshes - Pierre Terdiman (https://www.codercorner.com/MeshContacts.pdf)
///
/// Note that this class requires that CollideSettingsBase::mActiveEdgeMode == EActiveEdgeMode::CollideWithAll
/// and CollideSettingsBase::mCollectFacesMode == ECollectFacesMode::CollectFaces.
class InternalEdgeRemovingCollector : public CollideShapeCollector
{
static constexpr uint cMaxDelayedResults = 16;
Expand Down Expand Up @@ -221,6 +224,13 @@ class InternalEdgeRemovingCollector : public CollideShapeCollector
mDelayedResults.clear();
}

// See: CollideShapeCollector::OnBodyEnd
virtual void OnBodyEnd() override
{
Flush();
mChainedCollector.OnBodyEnd();
}

/// Version of CollisionDispatch::sCollideShapeVsShape that removes internal edges
static void sCollideShapeVsShape(const Shape *inShape1, const Shape *inShape2, Vec3Arg inScale1, Vec3Arg inScale2, Mat44Arg inCenterOfMassTransform1, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, const CollideShapeSettings &inCollideShapeSettings, CollideShapeCollector &ioCollector, const ShapeFilter &inShapeFilter = { })
{
Expand Down
96 changes: 21 additions & 75 deletions Jolt/Physics/Collision/NarrowPhaseQuery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ void NarrowPhaseQuery::CastRay(const RRayCast &inRay, const RayCastSettings &inR
// Do narrow phase collision check
ts.CastRay(mRay, mRayCastSettings, mCollector, mShapeFilter);

// Notify collector of the end of this body
mCollector.OnBodyEnd();

// Update early out fraction based on narrow phase collector
UpdateEarlyOutFraction(mCollector.GetEarlyOutFraction());
}
Expand Down Expand Up @@ -189,6 +192,9 @@ void NarrowPhaseQuery::CollidePoint(RVec3Arg inPoint, CollidePointCollector &ioC
// Do narrow phase collision check
ts.CollidePoint(mPoint, mCollector, mShapeFilter);

// Notify collector of the end of this body
mCollector.OnBodyEnd();

// Update early out fraction based on narrow phase collector
UpdateEarlyOutFraction(mCollector.GetEarlyOutFraction());
}
Expand Down Expand Up @@ -255,6 +261,9 @@ void NarrowPhaseQuery::CollideShape(const Shape *inShape, Vec3Arg inShapeScale,
// Do narrow phase collision check
ts.CollideShape(mShape, mShapeScale, mCenterOfMassTransform, mCollideShapeSettings, mBaseOffset, mCollector, mShapeFilter);

// Notify collector of the end of this body
mCollector.OnBodyEnd();

// Update early out fraction based on narrow phase collector
UpdateEarlyOutFraction(mCollector.GetEarlyOutFraction());
}
Expand Down Expand Up @@ -284,82 +293,13 @@ void NarrowPhaseQuery::CollideShape(const Shape *inShape, Vec3Arg inShapeScale,

void NarrowPhaseQuery::CollideShapeWithInternalEdgeRemoval(const Shape *inShape, Vec3Arg inShapeScale, RMat44Arg inCenterOfMassTransform, const CollideShapeSettings &inCollideShapeSettings, RVec3Arg inBaseOffset, CollideShapeCollector &ioCollector, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter, const BodyFilter &inBodyFilter, const ShapeFilter &inShapeFilter) const
{
JPH_PROFILE_FUNCTION();

class MyCollector : public CollideShapeBodyCollector
{
public:
MyCollector(const Shape *inShape, Vec3Arg inShapeScale, RMat44Arg inCenterOfMassTransform, const CollideShapeSettings &inCollideShapeSettings, RVec3Arg inBaseOffset, CollideShapeCollector &ioCollector, const BodyLockInterface &inBodyLockInterface, const BodyFilter &inBodyFilter, const ShapeFilter &inShapeFilter) :
CollideShapeBodyCollector(ioCollector),
mShape(inShape),
mShapeScale(inShapeScale),
mCenterOfMassTransform(inCenterOfMassTransform),
mBaseOffset(inBaseOffset),
mBodyLockInterface(inBodyLockInterface),
mBodyFilter(inBodyFilter),
mShapeFilter(inShapeFilter),
mCollideShapeSettings(inCollideShapeSettings),
mCollector(ioCollector)
{
// We require these settings for internal edge removal to work
mCollideShapeSettings.mActiveEdgeMode = EActiveEdgeMode::CollideWithAll;
mCollideShapeSettings.mCollectFacesMode = ECollectFacesMode::CollectFaces;
}

virtual void AddHit(const ResultType &inResult) override
{
// Only test shape if it passes the body filter
if (mBodyFilter.ShouldCollide(inResult))
{
// Lock the body
BodyLockRead lock(mBodyLockInterface, inResult);
if (lock.SucceededAndIsInBroadPhase()) // Race condition: body could have been removed since it has been found in the broadphase, ensures body is in the broadphase while we call the callbacks
{
const Body &body = lock.GetBody();

// Check body filter again now that we've locked the body
if (mBodyFilter.ShouldCollideLocked(body))
{
// Collect the transformed shape
TransformedShape ts = body.GetTransformedShape();
// We require these settings for internal edge removal to work
CollideShapeSettings settings = inCollideShapeSettings;
settings.mActiveEdgeMode = EActiveEdgeMode::CollideWithAll;
settings.mCollectFacesMode = ECollectFacesMode::CollectFaces;

// Notify collector of new body
mCollector.OnBody(body);

// Release the lock now, we have all the info we need in the transformed shape
lock.ReleaseLock();

// Do narrow phase collision check
ts.CollideShape(mShape, mShapeScale, mCenterOfMassTransform, mCollideShapeSettings, mBaseOffset, mCollector, mShapeFilter);

// After each body, we need to flush the InternalEdgeRemovingCollector because it uses 'ts' as context and it will go out of scope at the end of this block
mCollector.Flush();

// Update early out fraction based on narrow phase collector
UpdateEarlyOutFraction(mCollector.GetEarlyOutFraction());
}
}
}
}

const Shape * mShape;
Vec3 mShapeScale;
RMat44 mCenterOfMassTransform;
RVec3 mBaseOffset;
const BodyLockInterface & mBodyLockInterface;
const BodyFilter & mBodyFilter;
const ShapeFilter & mShapeFilter;
CollideShapeSettings mCollideShapeSettings;
InternalEdgeRemovingCollector mCollector;
};

// Calculate bounds for shape and expand by max separation distance
AABox bounds = inShape->GetWorldSpaceBounds(inCenterOfMassTransform, inShapeScale);
bounds.ExpandBy(Vec3::sReplicate(inCollideShapeSettings.mMaxSeparationDistance));

// Do broadphase test
MyCollector collector(inShape, inShapeScale, inCenterOfMassTransform, inCollideShapeSettings, inBaseOffset, ioCollector, *mBodyLockInterface, inBodyFilter, inShapeFilter);
mBroadPhaseQuery->CollideAABox(bounds, collector, inBroadPhaseLayerFilter, inObjectLayerFilter);
InternalEdgeRemovingCollector wrapper(ioCollector);
CollideShape(inShape, inShapeScale, inCenterOfMassTransform, settings, inBaseOffset, wrapper, inBroadPhaseLayerFilter, inObjectLayerFilter, inBodyFilter, inShapeFilter);
}

void NarrowPhaseQuery::CastShape(const RShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, RVec3Arg inBaseOffset, CastShapeCollector &ioCollector, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter, const BodyFilter &inBodyFilter, const ShapeFilter &inShapeFilter) const
Expand Down Expand Up @@ -409,6 +349,9 @@ void NarrowPhaseQuery::CastShape(const RShapeCast &inShapeCast, const ShapeCastS
// Do narrow phase collision check
ts.CastShape(mShapeCast, mShapeCastSettings, mBaseOffset, mCollector, mShapeFilter);

// Notify collector of the end of this body
mCollector.OnBodyEnd();

// Update early out fraction based on narrow phase collector
UpdateEarlyOutFraction(mCollector.GetEarlyOutFraction());
}
Expand Down Expand Up @@ -471,6 +414,9 @@ void NarrowPhaseQuery::CollectTransformedShapes(const AABox &inBox, TransformedS
// Do narrow phase collision check
ts.CollectTransformedShapes(mBox, mCollector, mShapeFilter);

// Notify collector of the end of this body
mCollector.OnBodyEnd();

// Update early out fraction based on narrow phase collector
UpdateEarlyOutFraction(mCollector.GetEarlyOutFraction());
}
Expand Down

0 comments on commit 005d4c0

Please sign in to comment.