PhysicsScene.h 3.6 KB

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