SoftBodyManifold.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2024 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Jolt/Physics/SoftBody/SoftBodyMotionProperties.h>
  6. JPH_NAMESPACE_BEGIN
  7. /// An interface to query which vertices of a soft body are colliding with other bodies
  8. class SoftBodyManifold
  9. {
  10. public:
  11. /// Get the vertices of the soft body for iterating
  12. const Array<SoftBodyVertex> & GetVertices() const { return mVertices; }
  13. /// Check if a vertex has collided with something in this update
  14. JPH_INLINE bool HasContact(const SoftBodyVertex &inVertex) const
  15. {
  16. return inVertex.mHasContact;
  17. }
  18. /// Get the local space contact point (multiply by GetCenterOfMassTransform() of the soft body to get world space)
  19. JPH_INLINE Vec3 GetLocalContactPoint(const SoftBodyVertex &inVertex) const
  20. {
  21. return inVertex.mPosition - inVertex.mCollisionPlane.SignedDistance(inVertex.mPosition) * inVertex.mCollisionPlane.GetNormal();
  22. }
  23. /// Get the contact normal for the vertex (assumes there is a contact).
  24. JPH_INLINE Vec3 GetContactNormal(const SoftBodyVertex &inVertex) const
  25. {
  26. return -inVertex.mCollisionPlane.GetNormal();
  27. }
  28. /// Get the body with which the vertex has collided in this update
  29. JPH_INLINE BodyID GetContactBodyID(const SoftBodyVertex &inVertex) const
  30. {
  31. return inVertex.mHasContact? mCollidingShapes[inVertex.mCollidingShapeIndex].mBodyID : BodyID();
  32. }
  33. /// Get the number of sensors that are in contact with the soft body
  34. JPH_INLINE uint GetNumSensorContacts() const
  35. {
  36. return (uint)mCollidingSensors.size();
  37. }
  38. /// Get the i-th sensor that is in contact with the soft body
  39. JPH_INLINE BodyID GetSensorContactBodyID(uint inIndex) const
  40. {
  41. return mCollidingSensors[inIndex].mBodyID;
  42. }
  43. private:
  44. /// Allow SoftBodyMotionProperties to construct us
  45. friend class SoftBodyMotionProperties;
  46. /// Constructor
  47. explicit SoftBodyManifold(const SoftBodyMotionProperties *inMotionProperties) :
  48. mVertices(inMotionProperties->mVertices),
  49. mCollidingShapes(inMotionProperties->mCollidingShapes),
  50. mCollidingSensors(inMotionProperties->mCollidingSensors)
  51. {
  52. }
  53. using CollidingShape = SoftBodyMotionProperties::CollidingShape;
  54. using CollidingSensor = SoftBodyMotionProperties::CollidingSensor;
  55. const Array<SoftBodyVertex> & mVertices;
  56. const Array<CollidingShape> & mCollidingShapes;
  57. const Array<CollidingSensor> & mCollidingSensors;
  58. };
  59. JPH_NAMESPACE_END