Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
CollideSoftBodyVerticesVsTriangles.h
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2024 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
9
11
14{
15public:
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 SoftBodyVertex &inVertex)
24 {
25 mLocalPosition = mInvTransform * inVertex.mPosition;
26 mClosestDistanceSq = FLT_MAX;
27 }
28
29 JPH_INLINE void ProcessTriangle(Vec3Arg inV0, Vec3Arg inV1, Vec3Arg inV2)
30 {
31 // Get the closest point from the vertex to the triangle
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(SoftBodyVertex &ioVertex, int inCollidingShapeIndex) const
47 {
48 if (mClosestDistanceSq < FLT_MAX)
49 {
50 // Convert triangle to world space
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 // Closest is interior to the triangle, use plane as collision plane but don't allow more than 0.1 m penetration
59 // because otherwise a triangle half a level a way will have a huge penetration if it is back facing
60 float penetration = min(triangle_normal.Dot(v0 - ioVertex.mPosition), 0.1f);
61 if (penetration > ioVertex.mLargestPenetration)
62 {
63 ioVertex.mLargestPenetration = penetration;
64 ioVertex.mCollisionPlane = Plane::sFromPointAndNormal(v0, triangle_normal);
65 ioVertex.mCollidingShapeIndex = inCollidingShapeIndex;
66 }
67 }
68 else
69 {
70 // Closest point is on an edge or vertex, use closest point as collision plane
71 Vec3 closest_point = mTransform * (mLocalPosition + mClosestPoint);
72 Vec3 normal = ioVertex.mPosition - closest_point;
73 if (normal.Dot(triangle_normal) > 0.0f) // Ignore back facing edges
74 {
75 float normal_length = normal.Length();
76 float penetration = -normal_length;
77 if (penetration > ioVertex.mLargestPenetration)
78 {
79 ioVertex.mLargestPenetration = penetration;
80 ioVertex.mCollisionPlane = Plane::sFromPointAndNormal(closest_point, normal_length > 0.0f? normal / normal_length : triangle_normal);
81 ioVertex.mCollidingShapeIndex = inCollidingShapeIndex;
82 }
83 }
84 }
85 }
86 }
87
91 Vec3 mV0, mV1, mV2;
96};
97
#define JPH_EXPORT
Definition: Core.h:236
#define JPH_NAMESPACE_END
Definition: Core.h:378
std::uint32_t uint32
Definition: Core.h:455
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:372
Collision detection helper that collides soft body vertices vs triangles.
Definition: CollideSoftBodyVerticesVsTriangles.h:14
JPH_INLINE void ProcessTriangle(Vec3Arg inV0, Vec3Arg inV1, Vec3Arg inV2)
Definition: CollideSoftBodyVerticesVsTriangles.h:29
uint32 mSet
Definition: CollideSoftBodyVerticesVsTriangles.h:95
Mat44 mInvTransform
Definition: CollideSoftBodyVerticesVsTriangles.h:89
Vec3 mLocalPosition
Definition: CollideSoftBodyVerticesVsTriangles.h:90
CollideSoftBodyVerticesVsTriangles(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale)
Definition: CollideSoftBodyVerticesVsTriangles.h:16
float mClosestDistanceSq
Definition: CollideSoftBodyVerticesVsTriangles.h:94
float mNormalSign
Definition: CollideSoftBodyVerticesVsTriangles.h:93
Vec3 mClosestPoint
Definition: CollideSoftBodyVerticesVsTriangles.h:92
JPH_INLINE void FinishVertex(SoftBodyVertex &ioVertex, int inCollidingShapeIndex) const
Definition: CollideSoftBodyVerticesVsTriangles.h:46
Mat44 mTransform
Definition: CollideSoftBodyVerticesVsTriangles.h:88
Vec3 mV0
Definition: CollideSoftBodyVerticesVsTriangles.h:91
JPH_INLINE void StartVertex(const SoftBodyVertex &inVertex)
Definition: CollideSoftBodyVerticesVsTriangles.h:23
Holds a 4x4 matrix of floats, but supports also operations on the 3x3 upper left part of the matrix.
Definition: Mat44.h:13
static Plane sFromPointAndNormal(Vec3Arg inPoint, Vec3Arg inNormal)
Create from point and normal.
Definition: Plane.h:21
Definition: SoftBodyVertex.h:16
float mLargestPenetration
Used while finding the collision plane, stores the largest penetration found so far.
Definition: SoftBodyVertex.h:24
int mCollidingShapeIndex
Index in the colliding shapes list of the body we may collide with.
Definition: SoftBodyVertex.h:22
Plane mCollisionPlane
Nearest collision plane, relative to the center of mass of the soft body.
Definition: SoftBodyVertex.h:21
Vec3 mPosition
Position, relative to the center of mass of the soft body.
Definition: SoftBodyVertex.h:19
Definition: Vec3.h:17
JPH_INLINE float Dot(Vec3Arg inV2) const
Dot product.
Definition: Vec3.inl:645
static JPH_INLINE Vec3 sAxisY()
Definition: Vec3.h:54
JPH_INLINE float Length() const
Length of vector.
Definition: Vec3.inl:677
JPH_INLINE Vec3 NormalizedOr(Vec3Arg inZeroValue) const
Normalize vector or return inZeroValue if the length of the vector is zero.
Definition: Vec3.inl:716
JPH_INLINE float LengthSq() const
Squared length of vector.
Definition: Vec3.inl:661
Vec3 GetClosestPointOnTriangle(Vec3Arg inA, Vec3Arg inB, Vec3Arg inC, uint32 &outSet)
Definition: ClosestPoint.h:160
Helper functions to get properties of a scaling vector.
Definition: ScaleHelpers.h:13