Skip to content
0.2.0-dev Download ZIP
82 lines ·2.7 KB Raw
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
7#include <Jolt/Physics/SoftBody/SoftBodyMotionProperties.h>
8
9JPH_NAMESPACE_BEGIN
10
11/// An interface to query which vertices of a soft body are colliding with other bodies
12class SoftBodyManifold
13{
14public:
15 /// Get the vertices of the soft body for iterating
16 const Array<SoftBodyVertex> & GetVertices() const { return mVertices; }
17
18 /// Check if a vertex has collided with something in this update
19 JPH_INLINE bool HasContact(const SoftBodyVertex &inVertex) const
20 {
21 return inVertex.mHasContact;
22 }
23
24 /// Get the local space contact point (multiply by GetCenterOfMassTransform() of the soft body to get world space)
25 JPH_INLINE Vec3 GetLocalContactPoint(const SoftBodyVertex &inVertex) const
26 {
27 return inVertex.mPosition - inVertex.mCollisionPlane.SignedDistance(inVertex.mPosition) * inVertex.mCollisionPlane.GetNormal();
28 }
29
30 /// Get the contact normal for the vertex (assumes there is a contact).
31 JPH_INLINE Vec3 GetContactNormal(const SoftBodyVertex &inVertex) const
32 {
33 return -inVertex.mCollisionPlane.GetNormal();
34 }
35
36 /// Get the body with which the vertex has collided in this update
37 JPH_INLINE BodyID GetContactBodyID(const SoftBodyVertex &inVertex) const
38 {
39 if (!inVertex.mHasContact)
40 return BodyID();
41
42 // If this is a CCD contact, the BodyID is encoded in mCollidingShapeIndex
43 if (inVertex.mCollidingShapeIndex & int(BodyID::cBroadPhaseBit))
44 return BodyID(uint32(inVertex.mCollidingShapeIndex) ^ BodyID::cBroadPhaseBit);
45
46 // Otherwise we can get it from mCollidingShapes
47 return mCollidingShapes[inVertex.mCollidingShapeIndex].mBodyID;
48 }
49
50 /// Get the number of sensors that are in contact with the soft body
51 JPH_INLINE uint GetNumSensorContacts() const
52 {
53 return (uint)mCollidingSensors.size();
54 }
55
56 /// Get the i-th sensor that is in contact with the soft body
57 JPH_INLINE BodyID GetSensorContactBodyID(uint inIndex) const
58 {
59 return mCollidingSensors[inIndex].mBodyID;
60 }
61
62private:
63 /// Allow SoftBodyMotionProperties to construct us
64 friend class SoftBodyMotionProperties;
65
66 /// Constructor
67 explicit SoftBodyManifold(const SoftBodyMotionProperties *inMotionProperties) :
68 mVertices(inMotionProperties->mVertices),
69 mCollidingShapes(inMotionProperties->mCollidingShapes),
70 mCollidingSensors(inMotionProperties->mCollidingSensors)
71 {
72 }
73
74 using CollidingShape = SoftBodyMotionProperties::CollidingShape;
75 using CollidingSensor = SoftBodyMotionProperties::CollidingSensor;
76
77 const Array<SoftBodyVertex> & mVertices;
78 const Array<CollidingShape> & mCollidingShapes;
79 const Array<CollidingSensor> & mCollidingSensors;
80};
81
82JPH_NAMESPACE_END