SoftBodyUpdateContext.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2023 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Jolt/Core/NonCopyable.h>
  6. #include <Jolt/Physics/Body/MotionProperties.h>
  7. JPH_NAMESPACE_BEGIN
  8. class Body;
  9. class SoftBodyMotionProperties;
  10. /// Temporary data used by the update of a soft body
  11. class SoftBodyUpdateContext : public NonCopyable
  12. {
  13. public:
  14. static constexpr uint cVertexCollisionBatch = 64; ///< Number of vertices to process in a batch in DetermineCollisionPlanes
  15. static constexpr uint cEdgeConstraintBatch = 256; ///< Number of edge constraints to process in a batch in ApplyEdgeConstraints
  16. // Input
  17. Body * mBody; ///< Body that is being updated
  18. SoftBodyMotionProperties * mMotionProperties; ///< Motion properties of that body
  19. RMat44 mCenterOfMassTransform; ///< Transform of the body relative to the soft body
  20. Vec3 mGravity; ///< Gravity vector in local space of the soft body
  21. Vec3 mDisplacementDueToGravity; ///< Displacement of the center of mass due to gravity in the current time step
  22. float mDeltaTime; ///< Delta time for the current time step
  23. float mSubStepDeltaTime; ///< Delta time for each sub step
  24. /// Describes progress in the current update
  25. enum class EState
  26. {
  27. DetermineCollisionPlanes, ///< Determine collision planes for vertices in parallel
  28. ApplyEdgeConstraints, ///< Apply edge constraints in parallel
  29. Done ///< Update is finished
  30. };
  31. /// Construct the edge constraint iterator starting at a new group
  32. static inline uint64 sGetEdgeGroupStart(uint32 inGroup)
  33. {
  34. return uint64(inGroup) << 32;
  35. }
  36. /// Get the group and start index from the edge constraint iterator
  37. static inline void sGetEdgeGroupAndStartIdx(uint64 inNextEdgeConstraint, uint32 &outGroup, uint32 &outStartIdx)
  38. {
  39. outGroup = uint32(inNextEdgeConstraint >> 32);
  40. outStartIdx = uint32(inNextEdgeConstraint);
  41. }
  42. // State of the update
  43. atomic<EState> mState { EState::DetermineCollisionPlanes };///< Current state of the update
  44. atomic<uint> mNextCollisionVertex { 0 }; ///< Next vertex to process for DetermineCollisionPlanes
  45. atomic<uint> mNumCollisionVerticesProcessed { 0 }; ///< Number of vertices processed by DetermineCollisionPlanes, used to determine if we can start simulating
  46. atomic<uint> mNextIteration { 0 }; ///< Next simulation iteration to process
  47. atomic<uint64> mNextEdgeConstraint { 0 }; ///< Next edge constraint group and start index to process
  48. atomic<uint> mNumEdgeConstraintsProcessed { 0 }; ///< Number of edge constraints processed by ApplyEdgeConstraints, used to determine if we can go to the next group / iteration
  49. // Output
  50. Vec3 mDeltaPosition; ///< Delta position of the body in the current time step, should be applied after the update
  51. ECanSleep mCanSleep; ///< Can the body sleep? Should be applied after the update
  52. };
  53. JPH_NAMESPACE_END