Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Collision/CollisionDispatch.cpp
9912 views
1
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2
// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3
// SPDX-License-Identifier: MIT
4
5
#include <Jolt/Jolt.h>
6
7
#include <Jolt/Physics/Collision/CollisionDispatch.h>
8
#include <Jolt/Physics/Collision/CastResult.h>
9
10
JPH_NAMESPACE_BEGIN
11
12
CollisionDispatch::CollideShape CollisionDispatch::sCollideShape[NumSubShapeTypes][NumSubShapeTypes];
13
CollisionDispatch::CastShape CollisionDispatch::sCastShape[NumSubShapeTypes][NumSubShapeTypes];
14
15
void CollisionDispatch::sInit()
16
{
17
for (uint i = 0; i < NumSubShapeTypes; ++i)
18
for (uint j = 0; j < NumSubShapeTypes; ++j)
19
{
20
if (sCollideShape[i][j] == nullptr)
21
sCollideShape[i][j] = [](const Shape *, const Shape *, Vec3Arg, Vec3Arg, Mat44Arg, Mat44Arg, const SubShapeIDCreator &, const SubShapeIDCreator &, const CollideShapeSettings &, CollideShapeCollector &, const ShapeFilter &)
22
{
23
JPH_ASSERT(false, "Unsupported shape pair");
24
};
25
26
if (sCastShape[i][j] == nullptr)
27
sCastShape[i][j] = [](const ShapeCast &, const ShapeCastSettings &, const Shape *, Vec3Arg, const ShapeFilter &, Mat44Arg, const SubShapeIDCreator &, const SubShapeIDCreator &, CastShapeCollector &)
28
{
29
JPH_ASSERT(false, "Unsupported shape pair");
30
};
31
}
32
}
33
34
void 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)
35
{
36
// A collision collector that flips the collision results
37
class ReversedCollector : public CollideShapeCollector
38
{
39
public:
40
explicit ReversedCollector(CollideShapeCollector &ioCollector) :
41
CollideShapeCollector(ioCollector),
42
mCollector(ioCollector)
43
{
44
}
45
46
virtual void AddHit(const CollideShapeResult &inResult) override
47
{
48
// Add the reversed hit
49
mCollector.AddHit(inResult.Reversed());
50
51
// If our chained collector updated its early out fraction, we need to follow
52
UpdateEarlyOutFraction(mCollector.GetEarlyOutFraction());
53
}
54
55
private:
56
CollideShapeCollector & mCollector;
57
};
58
59
ReversedShapeFilter shape_filter(inShapeFilter);
60
ReversedCollector collector(ioCollector);
61
sCollideShapeVsShape(inShape2, inShape1, inScale2, inScale1, inCenterOfMassTransform2, inCenterOfMassTransform1, inSubShapeIDCreator2, inSubShapeIDCreator1, inCollideShapeSettings, collector, shape_filter);
62
}
63
64
void 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)
65
{
66
// A collision collector that flips the collision results
67
class ReversedCollector : public CastShapeCollector
68
{
69
public:
70
explicit ReversedCollector(CastShapeCollector &ioCollector, Vec3Arg inWorldDirection) :
71
CastShapeCollector(ioCollector),
72
mCollector(ioCollector),
73
mWorldDirection(inWorldDirection)
74
{
75
}
76
77
virtual void AddHit(const ShapeCastResult &inResult) override
78
{
79
// Add the reversed hit
80
mCollector.AddHit(inResult.Reversed(mWorldDirection));
81
82
// If our chained collector updated its early out fraction, we need to follow
83
UpdateEarlyOutFraction(mCollector.GetEarlyOutFraction());
84
}
85
86
private:
87
CastShapeCollector & mCollector;
88
Vec3 mWorldDirection;
89
};
90
91
// Reverse the shape cast (shape cast is in local space to shape 2)
92
Mat44 com_start_inv = inShapeCast.mCenterOfMassStart.InversedRotationTranslation();
93
ShapeCast local_shape_cast(inShape, inScale, com_start_inv, -com_start_inv.Multiply3x3(inShapeCast.mDirection));
94
95
// Calculate the center of mass of shape 1 at start of sweep
96
Mat44 shape1_com = inCenterOfMassTransform2 * inShapeCast.mCenterOfMassStart;
97
98
// Calculate the world space direction vector of the shape cast
99
Vec3 world_direction = -inCenterOfMassTransform2.Multiply3x3(inShapeCast.mDirection);
100
101
// Forward the cast
102
ReversedShapeFilter shape_filter(inShapeFilter);
103
ReversedCollector collector(ioCollector, world_direction);
104
sCastShapeVsShapeLocalSpace(local_shape_cast, inShapeCastSettings, inShapeCast.mShape, inShapeCast.mScale, shape_filter, shape1_com, inSubShapeIDCreator2, inSubShapeIDCreator1, collector);
105
}
106
107
JPH_NAMESPACE_END
108
109