| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
#include <Jolt/Physics/Collision/CollideSoftBodyVertexIterator.h> |
| 8 |
#include <Jolt/Geometry/ClosestPoint.h> |
| 9 |
|
| 10 |
JPH_NAMESPACE_BEGIN |
| 11 |
|
| 12 |
|
| 13 |
class JPH_EXPORT CollideSoftBodyVerticesVsTriangles |
| 14 |
{ |
| 15 |
public: |
| 16 |
CollideSoftBodyVerticesVsTriangles(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale) : |
| 17 |
mTransform(inCenterOfMassTransform * Mat44::sScale(inScale)), |
| 18 |
mInvTransform(mTransform.Inversed()), |
| 19 |
mNormalSign(ScaleHelpers::IsInsideOut(inScale)? -1.0f : 1.0f) |
| 20 |
{ |
| 21 |
} |
| 22 |
|
| 23 |
JPH_INLINE void StartVertex(const CollideSoftBodyVertexIterator &inVertex) |
| 24 |
{ |
| 25 |
mLocalPosition = mInvTransform * inVertex.GetPosition(); |
| 26 |
mClosestDistanceSq = FLT_MAX; |
| 27 |
} |
| 28 |
|
| 29 |
JPH_INLINE void ProcessTriangle(Vec3Arg inV0, Vec3Arg inV1, Vec3Arg inV2) |
| 30 |
{ |
| 31 |
|
| 32 |
uint32 set; |
| 33 |
Vec3 closest_point = ClosestPoint::GetClosestPointOnTriangle(inV0 - mLocalPosition, inV1 - mLocalPosition, inV2 - mLocalPosition, set); |
| 34 |
float dist_sq = closest_point.LengthSq(); |
| 35 |
if (dist_sq < mClosestDistanceSq) |
| 36 |
{ |
| 37 |
mV0 = inV0; |
| 38 |
mV1 = inV1; |
| 39 |
mV2 = inV2; |
| 40 |
mClosestPoint = closest_point; |
| 41 |
mClosestDistanceSq = dist_sq; |
| 42 |
mSet = set; |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
JPH_INLINE void FinishVertex(const CollideSoftBodyVertexIterator &ioVertex, int inCollidingShapeIndex) const |
| 47 |
{ |
| 48 |
if (mClosestDistanceSq < FLT_MAX) |
| 49 |
{ |
| 50 |
|
| 51 |
Vec3 v0 = mTransform * mV0; |
| 52 |
Vec3 v1 = mTransform * mV1; |
| 53 |
Vec3 v2 = mTransform * mV2; |
| 54 |
Vec3 triangle_normal = mNormalSign * (v1 - v0).Cross(v2 - v0).NormalizedOr(Vec3::sAxisY()); |
| 55 |
|
| 56 |
if (mSet == 0b111) |
| 57 |
{ |
| 58 |
|
| 59 |
|
| 60 |
float penetration = min(triangle_normal.Dot(v0 - ioVertex.GetPosition()), 0.1f); |
| 61 |
if (ioVertex.UpdatePenetration(penetration)) |
| 62 |
ioVertex.SetCollision(Plane::sFromPointAndNormal(v0, triangle_normal), inCollidingShapeIndex); |
| 63 |
} |
| 64 |
else |
| 65 |
{ |
| 66 |
|
| 67 |
Vec3 closest_point = mTransform * (mLocalPosition + mClosestPoint); |
| 68 |
Vec3 normal = ioVertex.GetPosition() - closest_point; |
| 69 |
if (normal.Dot(triangle_normal) > 0.0f) |
| 70 |
{ |
| 71 |
float normal_length = normal.Length(); |
| 72 |
float penetration = -normal_length; |
| 73 |
if (ioVertex.UpdatePenetration(penetration)) |
| 74 |
ioVertex.SetCollision(Plane::sFromPointAndNormal(closest_point, normal_length > 0.0f? normal / normal_length : triangle_normal), inCollidingShapeIndex); |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
Mat44 mTransform; |
| 81 |
Mat44 mInvTransform; |
| 82 |
Vec3 mLocalPosition; |
| 83 |
Vec3 mV0, mV1, mV2; |
| 84 |
Vec3 mClosestPoint; |
| 85 |
float mNormalSign; |
| 86 |
float mClosestDistanceSq; |
| 87 |
uint32 mSet; |
| 88 |
}; |
| 89 |
|
| 90 |
JPH_NAMESPACE_END |