PhysicsScene.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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/Constraints/TwoBodyConstraint.h>
  8. JPH_NAMESPACE_BEGIN
  9. class PhysicsSystem;
  10. /// Contains the creation settings of a set of bodies
  11. class PhysicsScene : public RefTarget<PhysicsScene>
  12. {
  13. public:
  14. JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(PhysicsScene)
  15. /// Add a body to the scene
  16. void AddBody(const BodyCreationSettings &inBody);
  17. /// Body constant to use to indicate that the constraint is attached to the fixed world
  18. static constexpr uint32 cFixedToWorld = 0xffffffff;
  19. /// Add a constraint to the scene
  20. /// @param inConstraint Constraint settings
  21. /// @param inBody1 Index in the bodies list of first body to attach constraint to
  22. /// @param inBody2 Index in the bodies list of the second body to attach constraint to
  23. void AddConstraint(const TwoBodyConstraintSettings *inConstraint, uint32 inBody1, uint32 inBody2);
  24. /// Get number of bodies in this scene
  25. size_t GetNumBodies() const { return mBodies.size(); }
  26. /// Access to the body settings for this scene
  27. const Array<BodyCreationSettings> & GetBodies() const { return mBodies; }
  28. Array<BodyCreationSettings> & GetBodies() { return mBodies; }
  29. /// A constraint and how it is connected to the bodies in the scene
  30. class ConnectedConstraint
  31. {
  32. public:
  33. JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(ConnectedConstraint)
  34. ConnectedConstraint() = default;
  35. ConnectedConstraint(const TwoBodyConstraintSettings *inSettings, uint inBody1, uint inBody2) : mSettings(inSettings), mBody1(inBody1), mBody2(inBody2) { }
  36. RefConst<TwoBodyConstraintSettings> mSettings; ///< Constraint settings
  37. uint32 mBody1; ///< Index of first body (in mBodies)
  38. uint32 mBody2; ///< Index of second body (in mBodies)
  39. };
  40. /// Get number of constraints in this scene
  41. size_t GetNumConstraints() const { return mConstraints.size(); }
  42. /// Access to the constraints for this scene
  43. const Array<ConnectedConstraint> & GetConstraints() const { return mConstraints; }
  44. Array<ConnectedConstraint> & GetConstraints() { return mConstraints; }
  45. /// Instantiate all bodies, returns false if not all bodies could be created
  46. bool CreateBodies(PhysicsSystem *inSystem) const;
  47. /// Go through all body creation settings and fix shapes that are scaled incorrectly (note this will change the scene a bit).
  48. /// @return False when not all scales could be fixed.
  49. bool FixInvalidScales();
  50. /// Saves the state of this object in binary form to inStream.
  51. /// @param inStream The stream to save the state to
  52. /// @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)
  53. /// @param inSaveGroupFilter If the group filter should be saved as well (these could be shared)
  54. void SaveBinaryState(StreamOut &inStream, bool inSaveShapes, bool inSaveGroupFilter) const;
  55. using PhysicsSceneResult = Result<Ref<PhysicsScene>>;
  56. /// Restore a saved scene from inStream
  57. static PhysicsSceneResult sRestoreFromBinaryState(StreamIn &inStream);
  58. /// For debugging purposes: Construct a scene from the current state of the physics system
  59. void FromPhysicsSystem(const PhysicsSystem *inSystem);
  60. private:
  61. /// The bodies that are part of this scene
  62. Array<BodyCreationSettings> mBodies;
  63. /// Constraints that are part of this scene
  64. Array<ConnectedConstraint> mConstraints;
  65. };
  66. JPH_NAMESPACE_END