BodyCreationSettings.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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/Physics/Collision/Shape/Shape.h>
  6. #include <Jolt/Physics/Collision/ObjectLayer.h>
  7. #include <Jolt/Physics/Collision/CollisionGroup.h>
  8. #include <Jolt/Physics/Body/MotionType.h>
  9. #include <Jolt/Physics/Body/MotionQuality.h>
  10. #include <Jolt/ObjectStream/SerializableObject.h>
  11. JPH_NAMESPACE_BEGIN
  12. class StreamIn;
  13. class StreamOut;
  14. /// Enum used in BodyCreationSettings to indicate how mass and inertia should be calculated
  15. enum class EOverrideMassProperties : uint8
  16. {
  17. CalculateMassAndInertia, ///< Tells the system to calculate the mass and inertia based on density
  18. CalculateInertia, ///< Tells the system to take the mass from mMassPropertiesOverride and to calculate the inertia based on density of the shapes and to scale it to the provided mass
  19. MassAndInertiaProvided ///< Tells the system to take the mass and inertia from mMassPropertiesOverride
  20. };
  21. /// Settings for constructing a rigid body
  22. class BodyCreationSettings
  23. {
  24. public:
  25. JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(BodyCreationSettings)
  26. /// Constructor
  27. BodyCreationSettings() = default;
  28. BodyCreationSettings(const ShapeSettings *inShape, RVec3Arg inPosition, QuatArg inRotation, EMotionType inMotionType, ObjectLayer inObjectLayer) : mPosition(inPosition), mRotation(inRotation), mObjectLayer(inObjectLayer), mMotionType(inMotionType), mShape(inShape) { }
  29. BodyCreationSettings(const Shape *inShape, RVec3Arg inPosition, QuatArg inRotation, EMotionType inMotionType, ObjectLayer inObjectLayer) : mPosition(inPosition), mRotation(inRotation), mObjectLayer(inObjectLayer), mMotionType(inMotionType), mShapePtr(inShape) { }
  30. /// Access to the shape settings object. This contains serializable (non-runtime optimized) information about the Shape.
  31. const ShapeSettings * GetShapeSettings() const { return mShape; }
  32. void SetShapeSettings(const ShapeSettings *inShape) { mShape = inShape; mShapePtr = nullptr; }
  33. /// Convert ShapeSettings object into a Shape object. This will free the ShapeSettings object and make the object ready for runtime. Serialization is no longer possible after this.
  34. Shape::ShapeResult ConvertShapeSettings();
  35. /// Access to the run-time shape object. Will convert from ShapeSettings object if needed.
  36. const Shape * GetShape() const;
  37. void SetShape(const Shape *inShape) { mShapePtr = inShape; mShape = nullptr; }
  38. /// Check if the mass properties of this body will be calculated (only relevant for kinematic or dynamic objects that need a MotionProperties object)
  39. bool HasMassProperties() const { return mAllowDynamicOrKinematic || mMotionType != EMotionType::Static; }
  40. /// Calculate (or return when overridden) the mass and inertia for this body
  41. MassProperties GetMassProperties() const;
  42. /// Saves the state of this object in binary form to inStream. Doesn't store the shape nor the group filter.
  43. void SaveBinaryState(StreamOut &inStream) const;
  44. /// Restore the state of this object from inStream. Doesn't restore the shape nor the group filter.
  45. void RestoreBinaryState(StreamIn &inStream);
  46. using GroupFilterToIDMap = UnorderedMap<const GroupFilter *, uint32>;
  47. using IDToGroupFilterMap = Array<RefConst<GroupFilter>>;
  48. using ShapeToIDMap = Shape::ShapeToIDMap;
  49. using IDToShapeMap = Shape::IDToShapeMap;
  50. using MaterialToIDMap = Shape::MaterialToIDMap;
  51. using IDToMaterialMap = Shape::IDToMaterialMap;
  52. /// Save this body creation settings, its shape and gropu filter. Pass in an empty map in ioShapeMap / ioMaterialMap / ioGroupFilterMap or reuse the same map while saving multiple shapes to the same stream in order to avoid writing duplicates.
  53. /// Pass nullptr to ioShapeMap and ioMaterial map to skip saving shapes
  54. /// Pass nullptr to ioGroupFilterMap to skip saving group filters
  55. void SaveWithChildren(StreamOut &inStream, ShapeToIDMap *ioShapeMap, MaterialToIDMap *ioMaterialMap, GroupFilterToIDMap *ioGroupFilterMap) const;
  56. using BCSResult = Result<BodyCreationSettings>;
  57. /// Restore a shape, all its children and materials. Pass in an empty map in ioShapeMap / ioMaterialMap / ioGroupFilterMap or reuse the same map while reading multiple shapes from the same stream in order to restore duplicates.
  58. static BCSResult sRestoreWithChildren(StreamIn &inStream, IDToShapeMap &ioShapeMap, IDToMaterialMap &ioMaterialMap, IDToGroupFilterMap &ioGroupFilterMap);
  59. RVec3 mPosition = RVec3::sZero(); ///< Position of the body (not of the center of mass)
  60. Quat mRotation = Quat::sIdentity(); ///< Rotation of the body
  61. Vec3 mLinearVelocity = Vec3::sZero(); ///< World space linear velocity of the center of mass (m/s)
  62. Vec3 mAngularVelocity = Vec3::sZero(); ///< World space angular velocity (rad/s)
  63. /// User data value (can be used by application)
  64. uint64 mUserData = 0;
  65. ///@name Collision settings
  66. ObjectLayer mObjectLayer = 0; ///< The collision layer this body belongs to (determines if two objects can collide)
  67. CollisionGroup mCollisionGroup; ///< The collision group this body belongs to (determines if two objects can collide)
  68. ///@name Simulation properties
  69. EMotionType mMotionType = EMotionType::Dynamic; ///< Motion type, determines if the object is static, dynamic or kinematic
  70. bool mAllowDynamicOrKinematic = false; ///< When this body is created as static, this setting tells the system to create a MotionProperties object so that the object can be switched to kinematic or dynamic
  71. bool mIsSensor = false; ///< If this body is a sensor. A sensor will receive collision callbacks, but will not cause any collision responses and can be used as a trigger volume. See description at Body::SetIsSensor.
  72. bool mUseManifoldReduction = true; ///< If this body should use manifold reduction (see description at Body::SetUseManifoldReduction)
  73. EMotionQuality mMotionQuality = EMotionQuality::Discrete; ///< Motion quality, or how well it detects collisions when it has a high velocity
  74. bool mAllowSleeping = true; ///< If this body can go to sleep or not
  75. float mFriction = 0.2f; ///< Friction of the body (dimensionless number, usually between 0 and 1, 0 = no friction, 1 = friction force equals force that presses the two bodies together)
  76. float mRestitution = 0.0f; ///< Restitution of body (dimensionless number, usually between 0 and 1, 0 = completely inelastic collision response, 1 = completely elastic collision response)
  77. float mLinearDamping = 0.05f; ///< Linear damping: dv/dt = -c * v. c must be between 0 and 1 but is usually close to 0.
  78. float mAngularDamping = 0.05f; ///< Angular damping: dw/dt = -c * w. c must be between 0 and 1 but is usually close to 0.
  79. float mMaxLinearVelocity = 500.0f; ///< Maximum linear velocity that this body can reach (m/s)
  80. float mMaxAngularVelocity = 0.25f * JPH_PI * 60.0f; ///< Maximum angular velocity that this body can reach (rad/s)
  81. float mGravityFactor = 1.0f; ///< Value to multiply gravity with for this body
  82. ///@name Mass properties of the body (by default calculated by the shape)
  83. EOverrideMassProperties mOverrideMassProperties = EOverrideMassProperties::CalculateMassAndInertia; ///< Determines how mMassPropertiesOverride will be used
  84. float mInertiaMultiplier = 1.0f; ///< When calculating the inertia (not when it is provided) the calculated inertia will be multiplied by this value
  85. MassProperties mMassPropertiesOverride; ///< Contains replacement mass settings which override the automatically calculated values
  86. private:
  87. /// Collision volume for the body
  88. RefConst<ShapeSettings> mShape; ///< Shape settings, can be serialized. Mutually exclusive with mShapePtr
  89. RefConst<Shape> mShapePtr; ///< Actual shape, cannot be serialized. Mutually exclusive with mShape
  90. };
  91. JPH_NAMESPACE_END