| 1 | // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics) |
| 2 | // SPDX-FileCopyrightText: 2023 Jorrit Rouwe |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | |
| 5 | #pragma once |
| 6 | |
| 7 | #include <Jolt/Physics/SoftBody/SoftBodySharedSettings.h> |
| 8 | #include <Jolt/Physics/Collision/ObjectLayer.h> |
| 9 | #include <Jolt/Physics/Collision/CollisionGroup.h> |
| 10 | #include <Jolt/ObjectStream/SerializableObject.h> |
| 11 | #include <Jolt/Core/StreamUtils.h> |
| 12 | |
| 13 | JPH_NAMESPACE_BEGIN |
| 14 | |
| 15 | /// This class contains the information needed to create a soft body object |
| 16 | /// Note: Soft bodies are still in development and come with several caveats. Read the Architecture and API documentation for more information! |
| 17 | class JPH_EXPORT SoftBodyCreationSettings |
| 18 | { |
| 19 | JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(JPH_EXPORT, SoftBodyCreationSettings) |
| 20 | |
| 21 | public: |
| 22 | /// Constructor |
| 23 | SoftBodyCreationSettings() = default; |
| 24 | SoftBodyCreationSettings(const SoftBodySharedSettings *inSettings, RVec3Arg inPosition, QuatArg inRotation, ObjectLayer inObjectLayer) : mSettings(inSettings), mPosition(inPosition), mRotation(inRotation), mObjectLayer(inObjectLayer) { } |
| 25 | |
| 26 | /// Saves the state of this object in binary form to inStream. Doesn't store the shared settings nor the group filter. |
| 27 | void SaveBinaryState(StreamOut &inStream) const; |
| 28 | |
| 29 | /// Restore the state of this object from inStream. Doesn't restore the shared settings nor the group filter. |
| 30 | void RestoreBinaryState(StreamIn &inStream); |
| 31 | |
| 32 | using GroupFilterToIDMap = StreamUtils::ObjectToIDMap<GroupFilter>; |
| 33 | using IDToGroupFilterMap = StreamUtils::IDToObjectMap<GroupFilter>; |
| 34 | using SharedSettingsToIDMap = SoftBodySharedSettings::SharedSettingsToIDMap; |
| 35 | using IDToSharedSettingsMap = SoftBodySharedSettings::IDToSharedSettingsMap; |
| 36 | using MaterialToIDMap = StreamUtils::ObjectToIDMap<PhysicsMaterial>; |
| 37 | using IDToMaterialMap = StreamUtils::IDToObjectMap<PhysicsMaterial>; |
| 38 | |
| 39 | /// Save this body creation settings, its shared settings and group filter. Pass in an empty map in ioSharedSettingsMap / ioMaterialMap / ioGroupFilterMap or reuse the same map while saving multiple shapes to the same stream in order to avoid writing duplicates. |
| 40 | /// Pass nullptr to ioSharedSettingsMap and ioMaterial map to skip saving shared settings and materials |
| 41 | /// Pass nullptr to ioGroupFilterMap to skip saving group filters |
| 42 | void SaveWithChildren(StreamOut &inStream, SharedSettingsToIDMap *ioSharedSettingsMap, MaterialToIDMap *ioMaterialMap, GroupFilterToIDMap *ioGroupFilterMap) const; |
| 43 | |
| 44 | using SBCSResult = Result<SoftBodyCreationSettings>; |
| 45 | |
| 46 | /// Restore a shape, all its children and materials. Pass in an empty map in ioSharedSettingsMap / ioMaterialMap / ioGroupFilterMap or reuse the same map while reading multiple shapes from the same stream in order to restore duplicates. |
| 47 | static SBCSResult sRestoreWithChildren(StreamIn &inStream, IDToSharedSettingsMap &ioSharedSettingsMap, IDToMaterialMap &ioMaterialMap, IDToGroupFilterMap &ioGroupFilterMap); |
| 48 | |
| 49 | RefConst<SoftBodySharedSettings> mSettings; ///< Defines the configuration of this soft body |
| 50 | |
| 51 | RVec3 mPosition { RVec3::sZero() }; ///< Initial position of the soft body |
| 52 | Quat mRotation { Quat::sIdentity() }; ///< Initial rotation of the soft body |
| 53 | |
| 54 | /// User data value (can be used by application) |
| 55 | uint64 mUserData = 0; |
| 56 | |
| 57 | ///@name Collision settings |
| 58 | ObjectLayer mObjectLayer = 0; ///< The collision layer this body belongs to (determines if two objects can collide) |
| 59 | CollisionGroup mCollisionGroup; ///< The collision group this body belongs to (determines if two objects can collide) |
| 60 | |
| 61 | uint32 mNumIterations = 5; ///< Number of solver iterations |
| 62 | float mLinearDamping = 0.1f; ///< Linear damping: dv/dt = -mLinearDamping * v |
| 63 | float mMaxLinearVelocity = 500.0f; ///< Maximum linear velocity that a vertex can reach (m/s) |
| 64 | float mRestitution = 0.0f; ///< Restitution when colliding |
| 65 | float mFriction = 0.2f; ///< Friction coefficient when colliding |
| 66 | float mPressure = 0.0f; ///< n * R * T, amount of substance * ideal gas constant * absolute temperature, see https://en.wikipedia.org/wiki/Pressure |
| 67 | float mGravityFactor = 1.0f; ///< Value to multiply gravity with for this body |
| 68 | float mVertexRadius = 0.0f; ///< How big the particles are, can be used to push the vertices a little bit away from the surface of other bodies to prevent z-fighting |
| 69 | bool mUpdatePosition = true; ///< Update the position of the body while simulating (set to false for something that is attached to the static world) |
| 70 | bool mMakeRotationIdentity = true; ///< Bake specified mRotation in the vertices and set the body rotation to identity (simulation is slightly more accurate if the rotation of a soft body is kept to identity) |
| 71 | bool mAllowSleeping = true; ///< If this body can go to sleep or not |
| 72 | bool mFacesDoubleSided = false; ///< If the faces in this soft body should be treated as double sided for the purpose of collision detection (ray cast / collide shape / cast shape) |
| 73 | }; |
| 74 | |
| 75 | JPH_NAMESPACE_END |