| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
#include <Jolt/Core/QuickSort.h> |
| 8 |
#include <Jolt/Core/STLLocalAllocator.h> |
| 9 |
#include <Jolt/Physics/Collision/CollisionDispatch.h> |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
#ifdef JPH_INTERNAL_EDGE_REMOVING_COLLECTOR_DEBUG |
| 14 |
#include <Jolt/Renderer/DebugRenderer.h> |
| 15 |
#endif |
| 16 |
|
| 17 |
JPH_NAMESPACE_BEGIN |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
class InternalEdgeRemovingCollector : public CollideShapeCollector |
| 25 |
{ |
| 26 |
static constexpr uint cMaxLocalDelayedResults = 32; |
| 27 |
static constexpr uint cMaxLocalVoidedFeatures = 128; |
| 28 |
|
| 29 |
|
| 30 |
inline bool IsVoided(const SubShapeID &inSubShapeID, Vec3 inV) const |
| 31 |
{ |
| 32 |
for (const Voided &vf : mVoidedFeatures) |
| 33 |
if (vf.mSubShapeID == inSubShapeID |
| 34 |
&& inV.IsClose(Vec3::sLoadFloat3Unsafe(vf.mFeature), 1.0e-8f)) |
| 35 |
return true; |
| 36 |
return false; |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
inline void VoidFeatures(const CollideShapeResult &inResult) |
| 41 |
{ |
| 42 |
for (const Vec3 &v : inResult.mShape2Face) |
| 43 |
if (!IsVoided(inResult.mSubShapeID1, v)) |
| 44 |
{ |
| 45 |
Voided vf; |
| 46 |
v.StoreFloat3(&vf.mFeature); |
| 47 |
vf.mSubShapeID = inResult.mSubShapeID1; |
| 48 |
mVoidedFeatures.push_back(vf); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
inline void Chain(const CollideShapeResult &inResult) |
| 54 |
{ |
| 55 |
|
| 56 |
mChainedCollector.SetContext(GetContext()); |
| 57 |
|
| 58 |
|
| 59 |
mChainedCollector.AddHit(inResult); |
| 60 |
|
| 61 |
|
| 62 |
UpdateEarlyOutFraction(mChainedCollector.GetEarlyOutFraction()); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
inline void ChainAndVoid(const CollideShapeResult &inResult) |
| 67 |
{ |
| 68 |
Chain(inResult); |
| 69 |
VoidFeatures(inResult); |
| 70 |
|
| 71 |
#ifdef JPH_INTERNAL_EDGE_REMOVING_COLLECTOR_DEBUG |
| 72 |
DebugRenderer::sInstance->DrawWirePolygon(RMat44::sTranslation(mBaseOffset), inResult.mShape2Face, Color::sGreen); |
| 73 |
DebugRenderer::sInstance->DrawArrow(mBaseOffset + inResult.mContactPointOn2, mBaseOffset + inResult.mContactPointOn2 + inResult.mPenetrationAxis.NormalizedOr(Vec3::sZero()), Color::sGreen, 0.1f); |
| 74 |
#endif |
| 75 |
} |
| 76 |
|
| 77 |
public: |
| 78 |
|
| 79 |
explicit InternalEdgeRemovingCollector(CollideShapeCollector &inChainedCollector |
| 80 |
#ifdef JPH_INTERNAL_EDGE_REMOVING_COLLECTOR_DEBUG |
| 81 |
, RVec3Arg inBaseOffset |
| 82 |
#endif |
| 83 |
) : |
| 84 |
CollideShapeCollector(inChainedCollector), |
| 85 |
mChainedCollector(inChainedCollector) |
| 86 |
#ifdef JPH_INTERNAL_EDGE_REMOVING_COLLECTOR_DEBUG |
| 87 |
, mBaseOffset(inBaseOffset) |
| 88 |
#endif |
| 89 |
{ |
| 90 |
|
| 91 |
mVoidedFeatures.reserve(cMaxLocalVoidedFeatures); |
| 92 |
mDelayedResults.reserve(cMaxLocalDelayedResults); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
virtual void Reset() override |
| 97 |
{ |
| 98 |
CollideShapeCollector::Reset(); |
| 99 |
|
| 100 |
mChainedCollector.Reset(); |
| 101 |
|
| 102 |
mVoidedFeatures.clear(); |
| 103 |
mDelayedResults.clear(); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
virtual void OnBody(const Body &inBody) override |
| 108 |
{ |
| 109 |
|
| 110 |
mChainedCollector.OnBody(inBody); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
virtual void AddHit(const CollideShapeResult &inResult) override |
| 115 |
{ |
| 116 |
|
| 117 |
if (inResult.mShape2Face.size() < 3) |
| 118 |
return ChainAndVoid(inResult); |
| 119 |
|
| 120 |
|
| 121 |
Vec3 triangle_normal = (inResult.mShape2Face[1] - inResult.mShape2Face[0]).Cross(inResult.mShape2Face[2] - inResult.mShape2Face[0]); |
| 122 |
float triangle_normal_len = triangle_normal.Length(); |
| 123 |
if (triangle_normal_len < 1e-6f) |
| 124 |
return ChainAndVoid(inResult); |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
Vec3 contact_normal = -inResult.mPenetrationAxis; |
| 129 |
float contact_normal_len = inResult.mPenetrationAxis.Length(); |
| 130 |
if (triangle_normal.Dot(contact_normal) > 0.999848f * contact_normal_len * triangle_normal_len) |
| 131 |
return ChainAndVoid(inResult); |
| 132 |
|
| 133 |
|
| 134 |
mDelayedResults.push_back(inResult); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
void Flush() |
| 139 |
{ |
| 140 |
|
| 141 |
Array<uint, STLLocalAllocator<uint, cMaxLocalDelayedResults>> sorted_indices; |
| 142 |
sorted_indices.resize(mDelayedResults.size()); |
| 143 |
for (uint i = 0; i < uint(mDelayedResults.size()); ++i) |
| 144 |
sorted_indices[i] = i; |
| 145 |
QuickSort(sorted_indices.begin(), sorted_indices.end(), [this](uint inLHS, uint inRHS) { return mDelayedResults[inLHS].mPenetrationDepth > mDelayedResults[inRHS].mPenetrationDepth; }); |
| 146 |
|
| 147 |
|
| 148 |
for (uint i = 0; i < uint(mDelayedResults.size()); ++i) |
| 149 |
{ |
| 150 |
const CollideShapeResult &r = mDelayedResults[sorted_indices[i]]; |
| 151 |
|
| 152 |
|
| 153 |
float best_dist_sq = FLT_MAX; |
| 154 |
uint best_v1_idx = 0; |
| 155 |
uint best_v2_idx = 0; |
| 156 |
uint num_v = uint(r.mShape2Face.size()); |
| 157 |
uint v1_idx = num_v - 1; |
| 158 |
Vec3 v1 = r.mShape2Face[v1_idx] - r.mContactPointOn2; |
| 159 |
for (uint v2_idx = 0; v2_idx < num_v; ++v2_idx) |
| 160 |
{ |
| 161 |
Vec3 v2 = r.mShape2Face[v2_idx] - r.mContactPointOn2; |
| 162 |
Vec3 v1_v2 = v2 - v1; |
| 163 |
float denominator = v1_v2.LengthSq(); |
| 164 |
if (denominator < Square(FLT_EPSILON)) |
| 165 |
{ |
| 166 |
|
| 167 |
float v1_len_sq = v1.LengthSq(); |
| 168 |
if (v1_len_sq < best_dist_sq) |
| 169 |
{ |
| 170 |
best_dist_sq = v1_len_sq; |
| 171 |
best_v1_idx = v1_idx; |
| 172 |
best_v2_idx = v1_idx; |
| 173 |
} |
| 174 |
} |
| 175 |
else |
| 176 |
{ |
| 177 |
|
| 178 |
float fraction = -v1.Dot(v1_v2) / denominator; |
| 179 |
if (fraction < 1.0e-6f) |
| 180 |
{ |
| 181 |
|
| 182 |
float v1_len_sq = v1.LengthSq(); |
| 183 |
if (v1_len_sq < best_dist_sq) |
| 184 |
{ |
| 185 |
best_dist_sq = v1_len_sq; |
| 186 |
best_v1_idx = v1_idx; |
| 187 |
best_v2_idx = v1_idx; |
| 188 |
} |
| 189 |
} |
| 190 |
else if (fraction < 1.0f - 1.0e-6f) |
| 191 |
{ |
| 192 |
|
| 193 |
Vec3 closest = v1 + fraction * v1_v2; |
| 194 |
float closest_len_sq = closest.LengthSq(); |
| 195 |
if (closest_len_sq < best_dist_sq) |
| 196 |
{ |
| 197 |
best_dist_sq = closest_len_sq; |
| 198 |
best_v1_idx = v1_idx; |
| 199 |
best_v2_idx = v2_idx; |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
} |
| 204 |
|
| 205 |
v1_idx = v2_idx; |
| 206 |
v1 = v2; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
bool voided = IsVoided(r.mSubShapeID1, r.mShape2Face[best_v1_idx]) |
| 211 |
&& (best_v1_idx == best_v2_idx || IsVoided(r.mSubShapeID1, r.mShape2Face[best_v2_idx])); |
| 212 |
|
| 213 |
#ifdef JPH_INTERNAL_EDGE_REMOVING_COLLECTOR_DEBUG |
| 214 |
Color color = voided? Color::sRed : Color::sYellow; |
| 215 |
DebugRenderer::sInstance->DrawText3D(mBaseOffset + r.mContactPointOn2, StringFormat("%d: %g", i, r.mPenetrationDepth), color, 0.1f); |
| 216 |
DebugRenderer::sInstance->DrawWirePolygon(RMat44::sTranslation(mBaseOffset), r.mShape2Face, color); |
| 217 |
DebugRenderer::sInstance->DrawArrow(mBaseOffset + r.mContactPointOn2, mBaseOffset + r.mContactPointOn2 + r.mPenetrationAxis.NormalizedOr(Vec3::sZero()), color, 0.1f); |
| 218 |
DebugRenderer::sInstance->DrawMarker(mBaseOffset + r.mShape2Face[best_v1_idx], IsVoided(r.mSubShapeID1, r.mShape2Face[best_v1_idx])? Color::sRed : Color::sYellow, 0.1f); |
| 219 |
DebugRenderer::sInstance->DrawMarker(mBaseOffset + r.mShape2Face[best_v2_idx], IsVoided(r.mSubShapeID1, r.mShape2Face[best_v2_idx])? Color::sRed : Color::sYellow, 0.1f); |
| 220 |
#endif |
| 221 |
|
| 222 |
|
| 223 |
if (!voided) |
| 224 |
Chain(r); |
| 225 |
|
| 226 |
|
| 227 |
VoidFeatures(r); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
mVoidedFeatures.clear(); |
| 232 |
mDelayedResults.clear(); |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
virtual void OnBodyEnd() override |
| 237 |
{ |
| 238 |
Flush(); |
| 239 |
mChainedCollector.OnBodyEnd(); |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
static 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 = { } |
| 244 |
#ifdef JPH_INTERNAL_EDGE_REMOVING_COLLECTOR_DEBUG |
| 245 |
, RVec3Arg inBaseOffset = RVec3::sZero() |
| 246 |
#endif |
| 247 |
) |
| 248 |
{ |
| 249 |
JPH_ASSERT(inCollideShapeSettings.mActiveEdgeMode == EActiveEdgeMode::CollideWithAll); |
| 250 |
JPH_ASSERT(inCollideShapeSettings.mCollectFacesMode == ECollectFacesMode::CollectFaces); |
| 251 |
|
| 252 |
InternalEdgeRemovingCollector wrapper(ioCollector |
| 253 |
#ifdef JPH_INTERNAL_EDGE_REMOVING_COLLECTOR_DEBUG |
| 254 |
, inBaseOffset |
| 255 |
#endif |
| 256 |
); |
| 257 |
CollisionDispatch::sCollideShapeVsShape(inShape1, inShape2, inScale1, inScale2, inCenterOfMassTransform1, inCenterOfMassTransform2, inSubShapeIDCreator1, inSubShapeIDCreator2, inCollideShapeSettings, wrapper, inShapeFilter); |
| 258 |
wrapper.Flush(); |
| 259 |
} |
| 260 |
|
| 261 |
private: |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
struct Voided |
| 266 |
{ |
| 267 |
Float3 mFeature; |
| 268 |
SubShapeID mSubShapeID; |
| 269 |
}; |
| 270 |
|
| 271 |
CollideShapeCollector & mChainedCollector; |
| 272 |
Array<Voided, STLLocalAllocator<Voided, cMaxLocalVoidedFeatures>> mVoidedFeatures; |
| 273 |
Array<CollideShapeResult, STLLocalAllocator<CollideShapeResult, cMaxLocalDelayedResults>> mDelayedResults; |
| 274 |
#ifdef JPH_INTERNAL_EDGE_REMOVING_COLLECTOR_DEBUG |
| 275 |
RVec3 mBaseOffset; |
| 276 |
#endif |
| 277 |
}; |
| 278 |
|
| 279 |
JPH_NAMESPACE_END |