Skip to content
0.2.0-dev Download ZIP
159 lines ·7.1 KB Raw
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/CollideConvexVsTriangles.h>
8#include <Jolt/Physics/Collision/Shape/ScaleHelpers.h>
9#include <Jolt/Physics/Collision/CollideShape.h>
10#include <Jolt/Physics/Collision/TransformedShape.h>
11#include <Jolt/Physics/Collision/ActiveEdges.h>
12#include <Jolt/Physics/Collision/NarrowPhaseStats.h>
13#include <Jolt/Geometry/EPAPenetrationDepth.h>
14#include <Jolt/Geometry/Plane.h>
15
16JPH_NAMESPACE_BEGIN
17
18CollideConvexVsTriangles::CollideConvexVsTriangles(const ConvexShape *inShape1, Vec3Arg inScale1, Vec3Arg inScale2, Mat44Arg inCenterOfMassTransform1, Mat44Arg inCenterOfMassTransform2, const SubShapeID &inSubShapeID1, const CollideShapeSettings &inCollideShapeSettings, CollideShapeCollector &ioCollector) :
19 mCollideShapeSettings(inCollideShapeSettings),
20 mCollector(ioCollector),
21 mShape1(inShape1),
22 mScale1(inScale1),
23 mScale2(inScale2),
24 mTransform1(inCenterOfMassTransform1),
25 mSubShapeID1(inSubShapeID1)
26{
27 // Get transforms
28 Mat44 inverse_transform2 = inCenterOfMassTransform2.InversedRotationTranslation();
29 Mat44 transform1_to_2 = inverse_transform2 * inCenterOfMassTransform1;
30 mTransform2To1 = transform1_to_2.InversedRotationTranslation();
31
32 // Calculate bounds
33 mBoundsOf1 = inShape1->GetLocalBounds().Scaled(inScale1);
34 mBoundsOf1.ExpandBy(Vec3::sReplicate(inCollideShapeSettings.mMaxSeparationDistance));
35 mBoundsOf1InSpaceOf2 = mBoundsOf1.Transformed(transform1_to_2); // Convert bounding box of 1 into space of 2
36
37 // Determine if shape 2 is inside out or not
38 mScaleSign2 = ScaleHelpers::IsInsideOut(inScale2)? -1.0f : 1.0f;
39}
40
41void CollideConvexVsTriangles::Collide(Vec3Arg inV0, Vec3Arg inV1, Vec3Arg inV2, uint8 inActiveEdges, const SubShapeID &inSubShapeID2)
42{
43 // Scale triangle and transform it to the space of 1
44 Vec3 v0 = mTransform2To1 * (mScale2 * inV0);
45 Vec3 v1 = mTransform2To1 * (mScale2 * inV1);
46 Vec3 v2 = mTransform2To1 * (mScale2 * inV2);
47
48 // Calculate triangle normal
49 Vec3 triangle_normal = mScaleSign2 * (v1 - v0).Cross(v2 - v0);
50
51 // Backface check
52 bool back_facing = triangle_normal.Dot(v0) > 0.0f;
53 if (mCollideShapeSettings.mBackFaceMode == EBackFaceMode::IgnoreBackFaces && back_facing)
54 return;
55
56 // Get bounding box for triangle
57 AABox triangle_bbox = AABox::sFromTwoPoints(v0, v1);
58 triangle_bbox.Encapsulate(v2);
59
60 // Get intersection between triangle and shape box, if there is none, we're done
61 if (!triangle_bbox.Overlaps(mBoundsOf1))
62 return;
63
64 // Create triangle support function
65 TriangleConvexSupport triangle(v0, v1, v2);
66
67 // Perform collision detection
68 // Note: As we don't remember the penetration axis from the last iteration, and it is likely that the shape (A) we're colliding the triangle (B) against is in front of the triangle,
69 // and the penetration axis is the shortest distance along to push B out of collision, we use the inverse of the triangle normal as an initial penetration axis. This has been seen
70 // to improve performance by approx. 5% over using a fixed axis like (1, 0, 0).
71 Vec3 penetration_axis = -triangle_normal, point1, point2;
72 EPAPenetrationDepth pen_depth;
73 EPAPenetrationDepth::EStatus status;
74
75 // Get the support function
76 if (mShape1ExCvxRadius == nullptr)
77 mShape1ExCvxRadius = mShape1->GetSupportFunction(ConvexShape::ESupportMode::ExcludeConvexRadius, mBufferExCvxRadius, mScale1);
78
79 // Perform GJK step
80 float max_separation_distance = mCollideShapeSettings.mMaxSeparationDistance;
81 status = pen_depth.GetPenetrationDepthStepGJK(*mShape1ExCvxRadius, mShape1ExCvxRadius->GetConvexRadius() + max_separation_distance, triangle, 0.0f, mCollideShapeSettings.mCollisionTolerance, penetration_axis, point1, point2);
82
83 // Check result of collision detection
84 if (status == EPAPenetrationDepth::EStatus::NotColliding)
85 return;
86 else if (status == EPAPenetrationDepth::EStatus::Indeterminate)
87 {
88 // Need to run expensive EPA algorithm
89
90 // We know we're overlapping at this point, so we can set the max separation distance to 0.
91 // Numerically it is possible that GJK finds that the shapes are overlapping but EPA finds that they're separated.
92 // In order to avoid this, we clamp the max separation distance to 1 so that we don't excessively inflate the shape,
93 // but we still inflate it enough to avoid the case where EPA misses the collision.
94 max_separation_distance = min(max_separation_distance, 1.0f);
95
96 // Get the support function
97 if (mShape1IncCvxRadius == nullptr)
98 mShape1IncCvxRadius = mShape1->GetSupportFunction(ConvexShape::ESupportMode::IncludeConvexRadius, mBufferIncCvxRadius, mScale1);
99
100 // Add convex radius
101 AddConvexRadius shape1_add_max_separation_distance(*mShape1IncCvxRadius, max_separation_distance);
102
103 // Perform EPA step
104 if (!pen_depth.GetPenetrationDepthStepEPA(shape1_add_max_separation_distance, triangle, mCollideShapeSettings.mPenetrationTolerance, penetration_axis, point1, point2))
105 return;
106 }
107
108 // Check if the penetration is bigger than the early out fraction
109 float penetration_depth = (point2 - point1).Length() - max_separation_distance;
110 if (-penetration_depth >= mCollector.GetEarlyOutFraction())
111 return;
112
113 // Correct point1 for the added separation distance
114 float penetration_axis_len = penetration_axis.Length();
115 if (penetration_axis_len > 0.0f)
116 point1 -= penetration_axis * (max_separation_distance / penetration_axis_len);
117
118 // Check if we have enabled active edge detection
119 if (mCollideShapeSettings.mActiveEdgeMode == EActiveEdgeMode::CollideOnlyWithActive && inActiveEdges != 0b111)
120 {
121 // Convert the active edge velocity hint to local space
122 Vec3 active_edge_movement_direction = mTransform1.Multiply3x3Transposed(mCollideShapeSettings.mActiveEdgeMovementDirection);
123
124 // Update the penetration axis to account for active edges
125 // Note that we flip the triangle normal as the penetration axis is pointing towards the triangle instead of away
126 penetration_axis = ActiveEdges::FixNormal(v0, v1, v2, back_facing? triangle_normal : -triangle_normal, inActiveEdges, point2, penetration_axis, active_edge_movement_direction);
127 }
128
129 // Convert to world space
130 point1 = mTransform1 * point1;
131 point2 = mTransform1 * point2;
132 Vec3 penetration_axis_world = mTransform1.Multiply3x3(penetration_axis);
133
134 // Create collision result
135 CollideShapeResult result(point1, point2, penetration_axis_world, penetration_depth, mSubShapeID1, inSubShapeID2, TransformedShape::sGetBodyID(mCollector.GetContext()));
136
137 // Gather faces
138 if (mCollideShapeSettings.mCollectFacesMode == ECollectFacesMode::CollectFaces)
139 {
140 // Get supporting face of shape 1
141 mShape1->GetSupportingFace(SubShapeID(), -penetration_axis, mScale1, mTransform1, result.mShape1Face);
142
143 // Get face of the triangle
144 result.mShape2Face.resize(3);
145 result.mShape2Face[0] = mTransform1 * v0;
146 result.mShape2Face[1] = mTransform1 * v1;
147 result.mShape2Face[2] = mTransform1 * v2;
148
149 // When inside out, we need to swap the triangle winding
150 if (mScaleSign2 < 0.0f)
151 std::swap(result.mShape2Face[1], result.mShape2Face[2]);
152 }
153
154 // Notify the collector
155 JPH_IF_TRACK_NARROWPHASE_STATS(TrackNarrowPhaseCollector track;)
156 mCollector.AddHit(result);
157}
158
159JPH_NAMESPACE_END