Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Collision/CollisionDispatch.cpp
9912 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2021 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#include <Jolt/Jolt.h>56#include <Jolt/Physics/Collision/CollisionDispatch.h>7#include <Jolt/Physics/Collision/CastResult.h>89JPH_NAMESPACE_BEGIN1011CollisionDispatch::CollideShape CollisionDispatch::sCollideShape[NumSubShapeTypes][NumSubShapeTypes];12CollisionDispatch::CastShape CollisionDispatch::sCastShape[NumSubShapeTypes][NumSubShapeTypes];1314void CollisionDispatch::sInit()15{16for (uint i = 0; i < NumSubShapeTypes; ++i)17for (uint j = 0; j < NumSubShapeTypes; ++j)18{19if (sCollideShape[i][j] == nullptr)20sCollideShape[i][j] = [](const Shape *, const Shape *, Vec3Arg, Vec3Arg, Mat44Arg, Mat44Arg, const SubShapeIDCreator &, const SubShapeIDCreator &, const CollideShapeSettings &, CollideShapeCollector &, const ShapeFilter &)21{22JPH_ASSERT(false, "Unsupported shape pair");23};2425if (sCastShape[i][j] == nullptr)26sCastShape[i][j] = [](const ShapeCast &, const ShapeCastSettings &, const Shape *, Vec3Arg, const ShapeFilter &, Mat44Arg, const SubShapeIDCreator &, const SubShapeIDCreator &, CastShapeCollector &)27{28JPH_ASSERT(false, "Unsupported shape pair");29};30}31}3233void CollisionDispatch::sReversedCollideShape(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)34{35// A collision collector that flips the collision results36class ReversedCollector : public CollideShapeCollector37{38public:39explicit ReversedCollector(CollideShapeCollector &ioCollector) :40CollideShapeCollector(ioCollector),41mCollector(ioCollector)42{43}4445virtual void AddHit(const CollideShapeResult &inResult) override46{47// Add the reversed hit48mCollector.AddHit(inResult.Reversed());4950// If our chained collector updated its early out fraction, we need to follow51UpdateEarlyOutFraction(mCollector.GetEarlyOutFraction());52}5354private:55CollideShapeCollector & mCollector;56};5758ReversedShapeFilter shape_filter(inShapeFilter);59ReversedCollector collector(ioCollector);60sCollideShapeVsShape(inShape2, inShape1, inScale2, inScale1, inCenterOfMassTransform2, inCenterOfMassTransform1, inSubShapeIDCreator2, inSubShapeIDCreator1, inCollideShapeSettings, collector, shape_filter);61}6263void CollisionDispatch::sReversedCastShape(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, const Shape *inShape, Vec3Arg inScale, const ShapeFilter &inShapeFilter, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, CastShapeCollector &ioCollector)64{65// A collision collector that flips the collision results66class ReversedCollector : public CastShapeCollector67{68public:69explicit ReversedCollector(CastShapeCollector &ioCollector, Vec3Arg inWorldDirection) :70CastShapeCollector(ioCollector),71mCollector(ioCollector),72mWorldDirection(inWorldDirection)73{74}7576virtual void AddHit(const ShapeCastResult &inResult) override77{78// Add the reversed hit79mCollector.AddHit(inResult.Reversed(mWorldDirection));8081// If our chained collector updated its early out fraction, we need to follow82UpdateEarlyOutFraction(mCollector.GetEarlyOutFraction());83}8485private:86CastShapeCollector & mCollector;87Vec3 mWorldDirection;88};8990// Reverse the shape cast (shape cast is in local space to shape 2)91Mat44 com_start_inv = inShapeCast.mCenterOfMassStart.InversedRotationTranslation();92ShapeCast local_shape_cast(inShape, inScale, com_start_inv, -com_start_inv.Multiply3x3(inShapeCast.mDirection));9394// Calculate the center of mass of shape 1 at start of sweep95Mat44 shape1_com = inCenterOfMassTransform2 * inShapeCast.mCenterOfMassStart;9697// Calculate the world space direction vector of the shape cast98Vec3 world_direction = -inCenterOfMassTransform2.Multiply3x3(inShapeCast.mDirection);99100// Forward the cast101ReversedShapeFilter shape_filter(inShapeFilter);102ReversedCollector collector(ioCollector, world_direction);103sCastShapeVsShapeLocalSpace(local_shape_cast, inShapeCastSettings, inShapeCast.mShape, inShapeCast.mScale, shape_filter, shape1_com, inSubShapeIDCreator2, inSubShapeIDCreator1, collector);104}105106JPH_NAMESPACE_END107108109