PhysicsSettings.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. JPH_NAMESPACE_BEGIN
  6. /// If objects are closer than this distance, they are considered to be colliding (used for GJK) (unit: meter)
  7. constexpr float cDefaultCollisionTolerance = 1.0e-4f;
  8. /// A factor that determines the accuracy of the penetration depth calculation. If the change of the squared distance is less than tolerance * current_penetration_depth^2 the algorithm will terminate. (unit: dimensionless)
  9. constexpr float cDefaultPenetrationTolerance = 1.0e-4f; ///< Stop when there's less than 1% change
  10. /// How much padding to add around objects
  11. constexpr float cDefaultConvexRadius = 0.05f;
  12. /// Used by (Tapered)CapsuleShape to determine when supporting face is an edge rather than a point (unit: meter)
  13. static constexpr float cCapsuleProjectionSlop = 0.02f;
  14. /// Maximum amount of jobs to allow
  15. constexpr int cMaxPhysicsJobs = 2048;
  16. /// Maximum amount of barriers to allow
  17. constexpr int cMaxPhysicsBarriers = 8;
  18. struct PhysicsSettings
  19. {
  20. JPH_OVERRIDE_NEW_DELETE
  21. /// Size of body pairs array, corresponds to the maximum amount of potential body pairs that can be in flight at any time.
  22. /// Setting this to a low value will use less memory but slow down simulation as threads may run out of narrow phase work.
  23. int mMaxInFlightBodyPairs = 16384;
  24. /// How many PhysicsStepListeners to notify in 1 batch
  25. int mStepListenersBatchSize = 8;
  26. /// How many step listener batches are needed before spawning another job (set to INT_MAX if no parallelism is desired)
  27. int mStepListenerBatchesPerJob = 1;
  28. /// Baumgarte stabilization factor (how much of the position error to 'fix' in 1 update) (unit: dimensionless, 0 = nothing, 1 = 100%)
  29. float mBaumgarte = 0.2f;
  30. /// Radius around objects inside which speculative contact points will be detected. Note that if this is too big
  31. /// you will get ghost collisions as speculative contacts are based on the closest points during the collision detection
  32. /// step which may not be the actual closest points by the time the two objects hit (unit: meters)
  33. float mSpeculativeContactDistance = 0.02f;
  34. /// How much bodies are allowed to sink into eachother (unit: meters)
  35. float mPenetrationSlop = 0.02f;
  36. /// Fraction of its inner radius a body must move per step to enable casting for the LinearCast motion quality
  37. float mLinearCastThreshold = 0.75f;
  38. /// Fraction of its inner radius a body may penetrate another body for the LinearCast motion quality
  39. float mLinearCastMaxPenetration = 0.25f;
  40. /// Max squared distance to use to determine if two points are on the same plane for determining the contact manifold between two shape faces (unit: meter^2)
  41. float mManifoldToleranceSq = 1.0e-6f;
  42. /// Maximum distance to correct in a single iteration when solving position constraints (unit: meters)
  43. float mMaxPenetrationDistance = 0.2f;
  44. /// Maximum relative delta position for body pairs to be able to reuse collision results from last frame (units: meter^2)
  45. float mBodyPairCacheMaxDeltaPositionSq = Square(0.001f); ///< 1 mm
  46. /// Maximum relative delta orientation for body pairs to be able to reuse collision results from last frame, stored as cos(max angle / 2)
  47. float mBodyPairCacheCosMaxDeltaRotationDiv2 = 0.99984769515639123915701155881391f; ///< cos(2 degrees / 2)
  48. /// Maximum angle between normals that allows manifolds between different sub shapes of the same body pair to be combined
  49. float mContactNormalCosMaxDeltaRotation = 0.99619469809174553229501040247389f; ///< cos(5 degree)
  50. /// Maximum allowed distance between old and new contact point to preserve contact forces for warm start (units: meter^2)
  51. float mContactPointPreserveLambdaMaxDistSq = Square(0.01f); ///< 1 cm
  52. /// Number of solver velocity iterations to run
  53. /// Note that this needs to be >= 2 in order for friction to work (friction is applied using the non-penetration impulse from the previous iteration)
  54. int mNumVelocitySteps = 10;
  55. /// Number of solver position iterations to run
  56. int mNumPositionSteps = 2;
  57. /// Minimal velocity needed before a collision can be elastic (unit: m)
  58. float mMinVelocityForRestitution = 1.0f;
  59. /// Time before object is allowed to go to sleep (unit: seconds)
  60. float mTimeBeforeSleep = 0.5f;
  61. /// Velocity of points on bounding box of object below which an object can be considered sleeping (unit: m/s)
  62. float mPointVelocitySleepThreshold = 0.03f;
  63. /// By default the simulation is deterministic, it is possible to turn this off by setting this setting to false. This will make the simulation run faster but it will no longer be deterministic.
  64. bool mDeterministicSimulation = true;
  65. ///@name These variables are mainly for debugging purposes, they allow turning on/off certain subsystems. You probably want to leave them alone.
  66. ///@{
  67. /// Whether or not to use warm starting for constraints (initially applying previous frames impulses)
  68. bool mConstraintWarmStart = true;
  69. /// Whether or not to use the body pair cache, which removes the need for narrow phase collision detection when orientation between two bodies didn't change
  70. bool mUseBodyPairContactCache = true;
  71. /// Whether or not to reduce manifolds with similar contact normals into one contact manifold
  72. bool mUseManifoldReduction = true;
  73. /// If we split up large islands into smaller parallel batches of work (to improve performance)
  74. bool mUseLargeIslandSplitter = true;
  75. /// If objects can go to sleep or not
  76. bool mAllowSleeping = true;
  77. /// When false, we prevent collision against non-active (shared) edges. Mainly for debugging the algorithm.
  78. bool mCheckActiveEdges = true;
  79. ///@}
  80. };
  81. JPH_NAMESPACE_END