Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Collision/CastConvexVsTriangles.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/CastConvexVsTriangles.h>
8
#include <Jolt/Physics/Collision/TransformedShape.h>
9
#include <Jolt/Physics/Collision/Shape/ScaleHelpers.h>
10
#include <Jolt/Physics/Collision/ActiveEdges.h>
11
#include <Jolt/Physics/Collision/NarrowPhaseStats.h>
12
#include <Jolt/Geometry/EPAPenetrationDepth.h>
13
14
JPH_NAMESPACE_BEGIN
15
16
CastConvexVsTriangles::CastConvexVsTriangles(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, Vec3Arg inScale, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, CastShapeCollector &ioCollector) :
17
mShapeCast(inShapeCast),
18
mShapeCastSettings(inShapeCastSettings),
19
mCenterOfMassTransform2(inCenterOfMassTransform2),
20
mScale(inScale),
21
mSubShapeIDCreator1(inSubShapeIDCreator1),
22
mCollector(ioCollector)
23
{
24
JPH_ASSERT(inShapeCast.mShape->GetType() == EShapeType::Convex);
25
26
// Determine if shape is inside out or not
27
mScaleSign = ScaleHelpers::IsInsideOut(inScale)? -1.0f : 1.0f;
28
}
29
30
void CastConvexVsTriangles::Cast(Vec3Arg inV0, Vec3Arg inV1, Vec3Arg inV2, uint8 inActiveEdges, const SubShapeID &inSubShapeID2)
31
{
32
JPH_PROFILE_FUNCTION();
33
34
// Scale triangle
35
Vec3 v0 = mScale * inV0;
36
Vec3 v1 = mScale * inV1;
37
Vec3 v2 = mScale * inV2;
38
39
// Calculate triangle normal
40
Vec3 triangle_normal = mScaleSign * (v1 - v0).Cross(v2 - v0);
41
42
// Backface check
43
bool back_facing = triangle_normal.Dot(mShapeCast.mDirection) > 0.0f;
44
if (mShapeCastSettings.mBackFaceModeTriangles == EBackFaceMode::IgnoreBackFaces && back_facing)
45
return;
46
47
// Create triangle support function
48
TriangleConvexSupport triangle { v0, v1, v2 };
49
50
// Check if we already created the cast shape support function
51
if (mSupport == nullptr)
52
{
53
// Determine if we want to use the actual shape or a shrunken shape with convex radius
54
ConvexShape::ESupportMode support_mode = mShapeCastSettings.mUseShrunkenShapeAndConvexRadius? ConvexShape::ESupportMode::ExcludeConvexRadius : ConvexShape::ESupportMode::Default;
55
56
// Create support function
57
mSupport = static_cast<const ConvexShape *>(mShapeCast.mShape)->GetSupportFunction(support_mode, mSupportBuffer, mShapeCast.mScale);
58
}
59
60
EPAPenetrationDepth epa;
61
float fraction = mCollector.GetEarlyOutFraction();
62
Vec3 contact_point_a, contact_point_b, contact_normal;
63
if (epa.CastShape(mShapeCast.mCenterOfMassStart, mShapeCast.mDirection, mShapeCastSettings.mCollisionTolerance, mShapeCastSettings.mPenetrationTolerance, *mSupport, triangle, mSupport->GetConvexRadius(), 0.0f, mShapeCastSettings.mReturnDeepestPoint, fraction, contact_point_a, contact_point_b, contact_normal))
64
{
65
// Check if we have enabled active edge detection
66
if (mShapeCastSettings.mActiveEdgeMode == EActiveEdgeMode::CollideOnlyWithActive && inActiveEdges != 0b111)
67
{
68
// Convert the active edge velocity hint to local space
69
Vec3 active_edge_movement_direction = mCenterOfMassTransform2.Multiply3x3Transposed(mShapeCastSettings.mActiveEdgeMovementDirection);
70
71
// Update the contact normal to account for active edges
72
// Note that we flip the triangle normal as the penetration axis is pointing towards the triangle instead of away
73
contact_normal = ActiveEdges::FixNormal(v0, v1, v2, back_facing? triangle_normal : -triangle_normal, inActiveEdges, contact_point_b, contact_normal, active_edge_movement_direction);
74
}
75
76
// Convert to world space
77
contact_point_a = mCenterOfMassTransform2 * contact_point_a;
78
contact_point_b = mCenterOfMassTransform2 * contact_point_b;
79
Vec3 contact_normal_world = mCenterOfMassTransform2.Multiply3x3(contact_normal);
80
81
// Its a hit, store the sub shape id's
82
ShapeCastResult result(fraction, contact_point_a, contact_point_b, contact_normal_world, back_facing, mSubShapeIDCreator1.GetID(), inSubShapeID2, TransformedShape::sGetBodyID(mCollector.GetContext()));
83
84
// Early out if this hit is deeper than the collector's early out value
85
if (fraction == 0.0f && -result.mPenetrationDepth >= mCollector.GetEarlyOutFraction())
86
return;
87
88
// Gather faces
89
if (mShapeCastSettings.mCollectFacesMode == ECollectFacesMode::CollectFaces)
90
{
91
// Get supporting face of shape 1
92
Mat44 transform_1_to_2 = mShapeCast.mCenterOfMassStart;
93
transform_1_to_2.SetTranslation(transform_1_to_2.GetTranslation() + fraction * mShapeCast.mDirection);
94
static_cast<const ConvexShape *>(mShapeCast.mShape)->GetSupportingFace(SubShapeID(), transform_1_to_2.Multiply3x3Transposed(-contact_normal), mShapeCast.mScale, mCenterOfMassTransform2 * transform_1_to_2, result.mShape1Face);
95
96
// Get face of the triangle
97
triangle.GetSupportingFace(contact_normal, result.mShape2Face);
98
99
// Convert to world space
100
for (Vec3 &p : result.mShape2Face)
101
p = mCenterOfMassTransform2 * p;
102
}
103
104
JPH_IF_TRACK_NARROWPHASE_STATS(TrackNarrowPhaseCollector track;)
105
mCollector.AddHit(result);
106
}
107
}
108
109
JPH_NAMESPACE_END
110
111