Debian Codegraph @

thirdparty/jolt_physics/Jolt/Physics/Body/BodyCreationSettings.h

raw symbols SCIP
1 // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2 // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3 // SPDX-License-Identifier: MIT
4
5 #pragma once
6
7 #include <Jolt/Physics/Collision/Shape/Shape.h>
8 #include <Jolt/Physics/Collision/ObjectLayer.h>
9 #include <Jolt/Physics/Collision/CollisionGroup.h>
10 #include <Jolt/Physics/Body/MotionType.h>
11 #include <Jolt/Physics/Body/MotionQuality.h>
12 #include <Jolt/Physics/Body/AllowedDOFs.h>
13 #include <Jolt/ObjectStream/SerializableObject.h>
14 #include <Jolt/Core/StreamUtils.h>
15
16 JPH_NAMESPACE_BEGIN
17
18 class StreamIn;
19 class StreamOut;
20
21 /// Enum used in BodyCreationSettings to indicate how mass and inertia should be calculated
22 enum class EOverrideMassProperties : uint8
23 {
24 CalculateMassAndInertia, ///< Tells the system to calculate the mass and inertia based on density
25 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
26 MassAndInertiaProvided ///< Tells the system to take the mass and inertia from mMassPropertiesOverride
27 };
28
29 /// Settings for constructing a rigid body
30 class JPH_EXPORT BodyCreationSettings
31 {
32 JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(JPH_EXPORT, BodyCreationSettings)
33
34 public:
35 /// Constructor
36 BodyCreationSettings() = default;
37 BodyCreationSettings(const ShapeSettings *inShape, RVec3Arg inPosition, QuatArg inRotation, EMotionType inMotionType, ObjectLayer inObjectLayer) : mPosition(inPosition), mRotation(inRotation), mObjectLayer(inObjectLayer), mMotionType(inMotionType), mShape(inShape) { }
38 BodyCreationSettings(const Shape *inShape, RVec3Arg inPosition, QuatArg inRotation, EMotionType inMotionType, ObjectLayer inObjectLayer) : mPosition(inPosition), mRotation(inRotation), mObjectLayer(inObjectLayer), mMotionType(inMotionType), mShapePtr(inShape) { }
39
40 /// Access to the shape settings object. This contains serializable (non-runtime optimized) information about the Shape.
41 const ShapeSettings * GetShapeSettings() const { return mShape; }
42 void SetShapeSettings(const ShapeSettings *inShape) { mShape = inShape; mShapePtr = nullptr; }
43
44 /// 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.
45 Shape::ShapeResult ConvertShapeSettings();
46
47 /// Access to the run-time shape object. Will convert from ShapeSettings object if needed.
48 const Shape * GetShape() const;
49 void SetShape(const Shape *inShape) { mShapePtr = inShape; mShape = nullptr; }
50
51 /// Check if the mass properties of this body will be calculated (only relevant for kinematic or dynamic objects that need a MotionProperties object)
52 bool HasMassProperties() const { return mAllowDynamicOrKinematic || mMotionType != EMotionType::Static; }
53
54 /// Calculate (or return when overridden) the mass and inertia for this body
55 MassProperties GetMassProperties() const;
56
57 /// Saves the state of this object in binary form to inStream. Doesn't store the shape nor the group filter.
58 void SaveBinaryState(StreamOut &inStream) const;
59
60 /// Restore the state of this object from inStream. Doesn't restore the shape nor the group filter.
61 void RestoreBinaryState(StreamIn &inStream);
62
63 using GroupFilterToIDMap = StreamUtils::ObjectToIDMap<GroupFilter>;
64 using IDToGroupFilterMap = StreamUtils::IDToObjectMap<GroupFilter>;
65 using ShapeToIDMap = Shape::ShapeToIDMap;
66 using IDToShapeMap = Shape::IDToShapeMap;
67 using MaterialToIDMap = StreamUtils::ObjectToIDMap<PhysicsMaterial>;
68 using IDToMaterialMap = StreamUtils::IDToObjectMap<PhysicsMaterial>;
69
70 /// Save body creation settings, its shape, materials and group 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.
71 /// Pass nullptr to ioShapeMap and ioMaterial map to skip saving shapes
72 /// Pass nullptr to ioGroupFilterMap to skip saving group filters
73 void SaveWithChildren(StreamOut &inStream, ShapeToIDMap *ioShapeMap, MaterialToIDMap *ioMaterialMap, GroupFilterToIDMap *ioGroupFilterMap) const;
74
75 using BCSResult = Result<BodyCreationSettings>;
76
77 /// Restore body creation settings, its shape, materials and group filter. 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.
78 static BCSResult sRestoreWithChildren(StreamIn &inStream, IDToShapeMap &ioShapeMap, IDToMaterialMap &ioMaterialMap, IDToGroupFilterMap &ioGroupFilterMap);
79
80 RVec3 mPosition = RVec3::sZero(); ///< Position of the body (not of the center of mass)
81 Quat mRotation = Quat::sIdentity(); ///< Rotation of the body
82 Vec3 mLinearVelocity = Vec3::sZero(); ///< World space linear velocity of the center of mass (m/s)
83 Vec3 mAngularVelocity = Vec3::sZero(); ///< World space angular velocity (rad/s)
84
85 /// User data value (can be used by application)
86 uint64 mUserData = 0;
87
88 ///@name Collision settings
89 ObjectLayer mObjectLayer = 0; ///< The collision layer this body belongs to (determines if two objects can collide)
90 CollisionGroup mCollisionGroup; ///< The collision group this body belongs to (determines if two objects can collide)
91
92 ///@name Simulation properties
93 EMotionType mMotionType = EMotionType::Dynamic; ///< Motion type, determines if the object is static, dynamic or kinematic
94 EAllowedDOFs mAllowedDOFs = EAllowedDOFs::All; ///< Which degrees of freedom this body has (can be used to limit simulation to 2D)
95 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
96 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.
97 bool mCollideKinematicVsNonDynamic = false; ///< If kinematic objects can generate contact points against other kinematic or static objects. See description at Body::SetCollideKinematicVsNonDynamic.
98 bool mUseManifoldReduction = true; ///< If this body should use manifold reduction (see description at Body::SetUseManifoldReduction)
99 bool mApplyGyroscopicForce = false; ///< Set to indicate that the gyroscopic force should be applied to this body (aka Dzhanibekov effect, see https://en.wikipedia.org/wiki/Tennis_racket_theorem)
100 EMotionQuality mMotionQuality = EMotionQuality::Discrete; ///< Motion quality, or how well it detects collisions when it has a high velocity
101 bool mEnhancedInternalEdgeRemoval = false; ///< Set to indicate that extra effort should be made to try to remove ghost contacts (collisions with internal edges of a mesh). This is more expensive but makes bodies move smoother over a mesh with convex edges.
102 bool mAllowSleeping = true; ///< If this body can go to sleep or not
103 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). Note that bodies can have negative friction but the combined friction (see PhysicsSystem::SetCombineFriction) should never go below zero.
104 float mRestitution = 0.0f; ///< Restitution of body (dimensionless number, usually between 0 and 1, 0 = completely inelastic collision response, 1 = completely elastic collision response). Note that bodies can have negative restitution but the combined restitution (see PhysicsSystem::SetCombineRestitution) should never go below zero.
105 float mLinearDamping = 0.05f; ///< Linear damping: dv/dt = -c * v. c must be between 0 and 1 but is usually close to 0.
106 float mAngularDamping = 0.05f; ///< Angular damping: dw/dt = -c * w. c must be between 0 and 1 but is usually close to 0.
107 float mMaxLinearVelocity = 500.0f; ///< Maximum linear velocity that this body can reach (m/s)
108 float mMaxAngularVelocity = 0.25f * JPH_PI * 60.0f; ///< Maximum angular velocity that this body can reach (rad/s)
109 float mGravityFactor = 1.0f; ///< Value to multiply gravity with for this body
110 uint mNumVelocityStepsOverride = 0; ///< Used only when this body is dynamic and colliding. Override for the number of solver velocity iterations to run, 0 means use the default in PhysicsSettings::mNumVelocitySteps. The number of iterations to use is the max of all contacts and constraints in the island.
111 uint mNumPositionStepsOverride = 0; ///< Used only when this body is dynamic and colliding. Override for the number of solver position iterations to run, 0 means use the default in PhysicsSettings::mNumPositionSteps. The number of iterations to use is the max of all contacts and constraints in the island.
112
113 ///@name Mass properties of the body (by default calculated by the shape)
114 EOverrideMassProperties mOverrideMassProperties = EOverrideMassProperties::CalculateMassAndInertia; ///< Determines how mMassPropertiesOverride will be used
115 float mInertiaMultiplier = 1.0f; ///< When calculating the inertia (not when it is provided) the calculated inertia will be multiplied by this value
116 MassProperties mMassPropertiesOverride; ///< Contains replacement mass settings which override the automatically calculated values
117
118 private:
119 /// Collision volume for the body
120 RefConst<ShapeSettings> mShape; ///< Shape settings, can be serialized. Mutually exclusive with mShapePtr
121 RefConst<Shape> mShapePtr; ///< Actual shape, cannot be serialized. Mutually exclusive with mShape
122 };
123
124 JPH_NAMESPACE_END