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),
18 mInvTransform(mTransform.InversedRotationTranslation()),
19 mScale(inScale),
20 mNormalSign(ScaleHelpers::IsInsideOut(inScale)? -1.0f : 1.0f)
21 {
22 }
23
24 JPH_INLINE void StartVertex(const CollideSoftBodyVertexIterator &inVertex)
25 {
26 mLocalPosition = mInvTransform * inVertex.GetPosition();
27 mClosestDistanceSq = FLT_MAX;
28 }
29
30 JPH_INLINE void ProcessTriangle(Vec3Arg inV0, Vec3Arg inV1, Vec3Arg inV2)
31 {
32 // Apply the scale to the triangle
33 Vec3 v0 = mScale * inV0;
34 Vec3 v1 = mScale * inV1;
35 Vec3 v2 = mScale * inV2;
36
37 // Get the closest point from the vertex to the triangle
38 uint32 set;
39 Vec3 closest_point = ClosestPoint::GetClosestPointOnTriangle(v0 - mLocalPosition, v1 - mLocalPosition, v2 - mLocalPosition, set);
40 float dist_sq = closest_point.LengthSq();
41 if (dist_sq < mClosestDistanceSq)
42 {
43 mV0 = v0;
44 mV1 = v1;
45 mV2 = v2;
46 mClosestPoint = closest_point;
47 mClosestDistanceSq = dist_sq;
48 mSet = set;
49 }
50 }
51
52 JPH_INLINE void FinishVertex(const CollideSoftBodyVertexIterator &ioVertex, int inCollidingShapeIndex) const
53 {
54 if (mClosestDistanceSq < FLT_MAX)
55 {
56 // Convert triangle to world space
57 Vec3 v0 = mTransform * mV0;
58 Vec3 v1 = mTransform * mV1;
59 Vec3 v2 = mTransform * mV2;
60 Vec3 triangle_normal = mNormalSign * (v1 - v0).Cross(v2 - v0).NormalizedOr(Vec3::sAxisY());
61
62 if (mSet == 0b111)
63 {
64 // Closest is interior to the triangle, use plane as collision plane but don't allow more than sTriangleThickness penetration
65 // because otherwise a triangle half a level a way will have a huge penetration if it is back facing
66 float penetration = triangle_normal.Dot(v0 - ioVertex.GetPosition());
67 if (penetration < sTriangleThickness && ioVertex.UpdatePenetration(penetration))
68 ioVertex.SetCollision(Plane::sFromPointAndNormal(v0, triangle_normal), inCollidingShapeIndex);
69 }
70 else
71 {
72 // Closest point is on an edge or vertex, use closest point as collision plane
73 Vec3 closest_point = mTransform * (mLocalPosition + mClosestPoint);
74 Vec3 normal = ioVertex.GetPosition() - closest_point;
75 if (normal.Dot(triangle_normal) > 0.0f) // Ignore back facing edges
76 {
77 float normal_length = normal.Length();
78 float penetration = -normal_length;
79 if (ioVertex.UpdatePenetration(penetration))
80 ioVertex.SetCollision(Plane::sFromPointAndNormal(closest_point, normal_length > 0.0f? normal / normal_length : triangle_normal), inCollidingShapeIndex);
81 }
82 }
83 }
84 }
85
89 static inline float sTriangleThickness = 0.1f;
90
95 Vec3 mV0, mV1, mV2;
100};
101
#define JPH_EXPORT
Definition Core.h:275
#define JPH_NAMESPACE_END
Definition Core.h:425
std::uint32_t uint32
Definition Core.h:503
#define JPH_NAMESPACE_BEGIN
Definition Core.h:419
Definition CollideSoftBodyVertexIterator.h:15
void SetCollision(const Plane &inCollisionPlane, int inCollidingShapeIndex) const
Update the collision of the current vertex.
Definition CollideSoftBodyVertexIterator.h:93
Vec3 GetPosition() const
Get the position of the current vertex.
Definition CollideSoftBodyVertexIterator.h:70
bool UpdatePenetration(float inLargestPenetration) const
Definition CollideSoftBodyVertexIterator.h:83
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:30
JPH_INLINE void FinishVertex(const CollideSoftBodyVertexIterator &ioVertex, int inCollidingShapeIndex) const
Definition CollideSoftBodyVerticesVsTriangles.h:52
uint32 mSet
Definition CollideSoftBodyVerticesVsTriangles.h:99
Mat44 mInvTransform
Definition CollideSoftBodyVerticesVsTriangles.h:92
Vec3 mLocalPosition
Definition CollideSoftBodyVerticesVsTriangles.h:94
CollideSoftBodyVerticesVsTriangles(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale)
Definition CollideSoftBodyVerticesVsTriangles.h:16
float mClosestDistanceSq
Definition CollideSoftBodyVerticesVsTriangles.h:98
float mNormalSign
Definition CollideSoftBodyVerticesVsTriangles.h:97
Vec3 mScale
Definition CollideSoftBodyVerticesVsTriangles.h:93
Vec3 mClosestPoint
Definition CollideSoftBodyVerticesVsTriangles.h:96
Mat44 mTransform
Definition CollideSoftBodyVerticesVsTriangles.h:91
JPH_INLINE void StartVertex(const CollideSoftBodyVertexIterator &inVertex)
Definition CollideSoftBodyVerticesVsTriangles.h:24
Vec3 mV0
Definition CollideSoftBodyVerticesVsTriangles.h:95
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 Vec3.h:17
JPH_INLINE float Dot(Vec3Arg inV2) const
Dot product.
Definition Vec3.inl:650
static JPH_INLINE Vec3 sAxisY()
Definition Vec3.h:57
JPH_INLINE Vec3 NormalizedOr(Vec3Arg inZeroValue) const
Normalize vector or return inZeroValue if the length of the vector is zero.
Definition Vec3.inl:721
JPH_INLINE float LengthSq() const
Squared length of vector.
Definition Vec3.inl:666
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