PhysicsScene.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Jolt/Core/Reference.h>
  6. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  7. #include <Jolt/Physics/SoftBody/SoftBodyCreationSettings.h>
  8. #include <Jolt/Physics/Constraints/TwoBodyConstraint.h>
  9. JPH_NAMESPACE_BEGIN
  10. class PhysicsSystem;
  11. /// Contains the creation settings of a set of bodies
  12. class JPH_EXPORT PhysicsScene : public RefTarget<PhysicsScene>
  13. {
  14. public:
  15. JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(JPH_EXPORT, PhysicsScene)
  16. /// Add a body to the scene
  17. void AddBody(const BodyCreationSettings &inBody);
  18. /// Body constant to use to indicate that the constraint is attached to the fixed world
  19. static constexpr uint32 cFixedToWorld = 0xffffffff;
  20. /// Add a constraint to the scene
  21. /// @param inConstraint Constraint settings
  22. /// @param inBody1 Index in the bodies list of first body to attach constraint to
  23. /// @param inBody2 Index in the bodies list of the second body to attach constraint to
  24. void AddConstraint(const TwoBodyConstraintSettings *inConstraint, uint32 inBody1, uint32 inBody2);
  25. /// Add a soft body to the scene
  26. void AddSoftBody(const SoftBodyCreationSettings &inSoftBody);
  27. /// Get number of bodies in this scene
  28. size_t GetNumBodies() const { return mBodies.size(); }
  29. /// Access to the body settings for this scene
  30. const Array<BodyCreationSettings> & GetBodies() const { return mBodies; }
  31. Array<BodyCreationSettings> & GetBodies() { return mBodies; }
  32. /// A constraint and how it is connected to the bodies in the scene
  33. class ConnectedConstraint
  34. {
  35. public:
  36. JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(JPH_EXPORT, ConnectedConstraint)
  37. ConnectedConstraint() = default;
  38. ConnectedConstraint(const TwoBodyConstraintSettings *inSettings, uint inBody1, uint inBody2) : mSettings(inSettings), mBody1(inBody1), mBody2(inBody2) { }
  39. RefConst<TwoBodyConstraintSettings> mSettings; ///< Constraint settings
  40. uint32 mBody1; ///< Index of first body (in mBodies)
  41. uint32 mBody2; ///< Index of second body (in mBodies)
  42. };
  43. /// Get number of constraints in this scene
  44. size_t GetNumConstraints() const { return mConstraints.size(); }
  45. /// Access to the constraints for this scene
  46. const Array<ConnectedConstraint> & GetConstraints() const { return mConstraints; }
  47. Array<ConnectedConstraint> & GetConstraints() { return mConstraints; }
  48. /// Get number of bodies in this scene
  49. size_t GetNumSoftBodies() const { return mSoftBodies.size(); }
  50. /// Access to the soft body settings for this scene
  51. const Array<SoftBodyCreationSettings> & GetSoftBodies() const { return mSoftBodies; }
  52. Array<SoftBodyCreationSettings> & GetSoftBodies() { return mSoftBodies; }
  53. /// Instantiate all bodies, returns false if not all bodies could be created
  54. bool CreateBodies(PhysicsSystem *inSystem) const;
  55. /// Go through all body creation settings and fix shapes that are scaled incorrectly (note this will change the scene a bit).
  56. /// @return False when not all scales could be fixed.
  57. bool FixInvalidScales();
  58. /// Saves the state of this object in binary form to inStream.
  59. /// @param inStream The stream to save the state to
  60. /// @param inSaveShapes If the shapes should be saved as well (these could be shared between physics scenes, in which case the calling application may want to write custom code to restore them)
  61. /// @param inSaveGroupFilter If the group filter should be saved as well (these could be shared)
  62. void SaveBinaryState(StreamOut &inStream, bool inSaveShapes, bool inSaveGroupFilter) const;
  63. using PhysicsSceneResult = Result<Ref<PhysicsScene>>;
  64. /// Restore a saved scene from inStream
  65. static PhysicsSceneResult sRestoreFromBinaryState(StreamIn &inStream);
  66. /// For debugging purposes: Construct a scene from the current state of the physics system
  67. void FromPhysicsSystem(const PhysicsSystem *inSystem);
  68. private:
  69. /// The bodies that are part of this scene
  70. Array<BodyCreationSettings> mBodies;
  71. /// Constraints that are part of this scene
  72. Array<ConnectedConstraint> mConstraints;
  73. /// Soft bodies that are part of this scene
  74. Array<SoftBodyCreationSettings> mSoftBodies;
  75. };
  76. JPH_NAMESPACE_END