Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Collision/CollisionDispatch.h
9912 views
1
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2
// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3
// SPDX-License-Identifier: MIT
4
5
#pragma once
6
7
#include <Jolt/Physics/Collision/Shape/Shape.h>
8
#include <Jolt/Physics/Collision/Shape/SubShapeID.h>
9
#include <Jolt/Physics/Collision/ShapeCast.h>
10
#include <Jolt/Physics/Collision/ShapeFilter.h>
11
#include <Jolt/Physics/Collision/NarrowPhaseStats.h>
12
13
JPH_NAMESPACE_BEGIN
14
15
class CollideShapeSettings;
16
17
/// Dispatch function, main function to handle collisions between shapes
18
class JPH_EXPORT CollisionDispatch
19
{
20
public:
21
/// Collide 2 shapes and pass any collision on to ioCollector
22
/// @param inShape1 The first shape
23
/// @param inShape2 The second shape
24
/// @param inScale1 Local space scale of shape 1 (scales relative to its center of mass)
25
/// @param inScale2 Local space scale of shape 2 (scales relative to its center of mass)
26
/// @param inCenterOfMassTransform1 Transform to transform center of mass of shape 1 into world space
27
/// @param inCenterOfMassTransform2 Transform to transform center of mass of shape 2 into world space
28
/// @param inSubShapeIDCreator1 Class that tracks the current sub shape ID for shape 1
29
/// @param inSubShapeIDCreator2 Class that tracks the current sub shape ID for shape 2
30
/// @param inCollideShapeSettings Options for the CollideShape test
31
/// @param ioCollector The collector that receives the results.
32
/// @param inShapeFilter allows selectively disabling collisions between pairs of (sub) shapes.
33
static inline 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 = { })
34
{
35
JPH_IF_TRACK_NARROWPHASE_STATS(TrackNarrowPhaseStat track(NarrowPhaseStat::sCollideShape[(int)inShape1->GetSubType()][(int)inShape2->GetSubType()]);)
36
37
// Only test shape if it passes the shape filter
38
if (inShapeFilter.ShouldCollide(inShape1, inSubShapeIDCreator1.GetID(), inShape2, inSubShapeIDCreator2.GetID()))
39
sCollideShape[(int)inShape1->GetSubType()][(int)inShape2->GetSubType()](inShape1, inShape2, inScale1, inScale2, inCenterOfMassTransform1, inCenterOfMassTransform2, inSubShapeIDCreator1, inSubShapeIDCreator2, inCollideShapeSettings, ioCollector, inShapeFilter);
40
}
41
42
/// Cast a shape against this shape, passes any hits found to ioCollector.
43
/// Note: This version takes the shape cast in local space relative to the center of mass of inShape, take a look at sCastShapeVsShapeWorldSpace if you have a shape cast in world space.
44
/// @param inShapeCastLocal The shape to cast against the other shape and its start and direction.
45
/// @param inShapeCastSettings Settings for performing the cast
46
/// @param inShape The shape to cast against.
47
/// @param inScale Local space scale for the shape to cast against (scales relative to its center of mass).
48
/// @param inShapeFilter allows selectively disabling collisions between pairs of (sub) shapes.
49
/// @param inCenterOfMassTransform2 Is the center of mass transform of shape 2 (excluding scale), this is used to provide a transform to the shape cast result so that local hit result quantities can be transformed into world space.
50
/// @param inSubShapeIDCreator1 Class that tracks the current sub shape ID for the casting shape
51
/// @param inSubShapeIDCreator2 Class that tracks the current sub shape ID for the shape we're casting against
52
/// @param ioCollector The collector that receives the results.
53
static inline void sCastShapeVsShapeLocalSpace(const ShapeCast &inShapeCastLocal, const ShapeCastSettings &inShapeCastSettings, const Shape *inShape, Vec3Arg inScale, const ShapeFilter &inShapeFilter, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, CastShapeCollector &ioCollector)
54
{
55
JPH_IF_TRACK_NARROWPHASE_STATS(TrackNarrowPhaseStat track(NarrowPhaseStat::sCastShape[(int)inShapeCastLocal.mShape->GetSubType()][(int)inShape->GetSubType()]);)
56
57
// Only test shape if it passes the shape filter
58
if (inShapeFilter.ShouldCollide(inShapeCastLocal.mShape, inSubShapeIDCreator1.GetID(), inShape, inSubShapeIDCreator2.GetID()))
59
sCastShape[(int)inShapeCastLocal.mShape->GetSubType()][(int)inShape->GetSubType()](inShapeCastLocal, inShapeCastSettings, inShape, inScale, inShapeFilter, inCenterOfMassTransform2, inSubShapeIDCreator1, inSubShapeIDCreator2, ioCollector);
60
}
61
62
/// See: sCastShapeVsShapeLocalSpace.
63
/// The only difference is that the shape cast (inShapeCastWorld) is provided in world space.
64
/// Note: A shape cast contains the center of mass start of the shape, if you have the world transform of the shape you probably want to construct it using ShapeCast::sFromWorldTransform.
65
static inline void sCastShapeVsShapeWorldSpace(const ShapeCast &inShapeCastWorld, const ShapeCastSettings &inShapeCastSettings, const Shape *inShape, Vec3Arg inScale, const ShapeFilter &inShapeFilter, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, CastShapeCollector &ioCollector)
66
{
67
ShapeCast local_shape_cast = inShapeCastWorld.PostTransformed(inCenterOfMassTransform2.InversedRotationTranslation());
68
sCastShapeVsShapeLocalSpace(local_shape_cast, inShapeCastSettings, inShape, inScale, inShapeFilter, inCenterOfMassTransform2, inSubShapeIDCreator1, inSubShapeIDCreator2, ioCollector);
69
}
70
71
/// Function that collides 2 shapes (see sCollideShapeVsShape)
72
using CollideShape = void (*)(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);
73
74
/// Function that casts a shape vs another shape (see sCastShapeVsShapeLocalSpace)
75
using CastShape = void (*)(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, const Shape *inShape, Vec3Arg inScale, const ShapeFilter &inShapeFilter, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, CastShapeCollector &ioCollector);
76
77
/// Initialize all collision functions with a function that asserts and returns no collision
78
static void sInit();
79
80
/// Register a collide shape function in the collision table
81
static void sRegisterCollideShape(EShapeSubType inType1, EShapeSubType inType2, CollideShape inFunction) { sCollideShape[(int)inType1][(int)inType2] = inFunction; }
82
83
/// Register a cast shape function in the collision table
84
static void sRegisterCastShape(EShapeSubType inType1, EShapeSubType inType2, CastShape inFunction) { sCastShape[(int)inType1][(int)inType2] = inFunction; }
85
86
/// An implementation of CollideShape that swaps inShape1 and inShape2 and swaps the result back, can be registered if the collision function only exists the other way around
87
static void 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);
88
89
/// An implementation of CastShape that swaps inShape1 and inShape2 and swaps the result back, can be registered if the collision function only exists the other way around
90
static void 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);
91
92
private:
93
static CollideShape sCollideShape[NumSubShapeTypes][NumSubShapeTypes];
94
static CastShape sCastShape[NumSubShapeTypes][NumSubShapeTypes];
95
};
96
97
JPH_NAMESPACE_END
98
99