Character.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/Character/CharacterBase.h>
  6. #include <Jolt/Physics/EActivation.h>
  7. JPH_NAMESPACE_BEGIN
  8. /// Contains the configuration of a character
  9. class CharacterSettings : public CharacterBaseSettings
  10. {
  11. public:
  12. JPH_OVERRIDE_NEW_DELETE
  13. /// Layer that this character will be added to
  14. ObjectLayer mLayer = 0;
  15. /// Mass of the character
  16. float mMass = 80.0f;
  17. /// Friction for the character
  18. float mFriction = 0.2f;
  19. /// Value to multiply gravity with for this character
  20. float mGravityFactor = 1.0f;
  21. };
  22. /// Runtime character object.
  23. /// This object usually represents the player or a humanoid AI. It uses a single rigid body,
  24. /// usually with a capsule shape to simulate movement and collision for the character.
  25. /// The character is a keyframed object, the application controls it by setting the velocity.
  26. class Character : public CharacterBase
  27. {
  28. public:
  29. JPH_OVERRIDE_NEW_DELETE
  30. /// Constructor
  31. /// @param inSettings The settings for the character
  32. /// @param inPosition Initial position for the character
  33. /// @param inRotation Initial rotation for the character (usually only around Y)
  34. /// @param inUserData Application specific value
  35. /// @param inSystem Physics system that this character will be added to later
  36. Character(const CharacterSettings *inSettings, RVec3Arg inPosition, QuatArg inRotation, uint64 inUserData, PhysicsSystem *inSystem);
  37. /// Destructor
  38. virtual ~Character() override;
  39. /// Add bodies and constraints to the system and optionally activate the bodies
  40. void AddToPhysicsSystem(EActivation inActivationMode = EActivation::Activate, bool inLockBodies = true);
  41. /// Remove bodies and constraints from the system
  42. void RemoveFromPhysicsSystem(bool inLockBodies = true);
  43. /// Wake up the character
  44. void Activate(bool inLockBodies = true);
  45. /// Needs to be called after every PhysicsSystem::Update
  46. /// @param inMaxSeparationDistance Max distance between the floor and the character to still consider the character standing on the floor
  47. /// @param inLockBodies If the collision query should use the locking body interface (true) or the non locking body interface (false)
  48. void PostSimulation(float inMaxSeparationDistance, bool inLockBodies = true);
  49. /// Control the velocity of the character
  50. void SetLinearAndAngularVelocity(Vec3Arg inLinearVelocity, Vec3Arg inAngularVelocity, bool inLockBodies = true);
  51. /// Get the linear velocity of the character (m / s)
  52. Vec3 GetLinearVelocity(bool inLockBodies = true) const;
  53. /// Set the linear velocity of the character (m / s)
  54. void SetLinearVelocity(Vec3Arg inLinearVelocity, bool inLockBodies = true);
  55. /// Add world space linear velocity to current velocity (m / s)
  56. void AddLinearVelocity(Vec3Arg inLinearVelocity, bool inLockBodies = true);
  57. /// Add impulse to the center of mass of the character
  58. void AddImpulse(Vec3Arg inImpulse, bool inLockBodies = true);
  59. /// Get the body associated with this character
  60. BodyID GetBodyID() const { return mBodyID; }
  61. /// Get position / rotation of the body
  62. void GetPositionAndRotation(RVec3 &outPosition, Quat &outRotation, bool inLockBodies = true) const;
  63. /// Set the position / rotation of the body, optionally activating it.
  64. void SetPositionAndRotation(RVec3Arg inPosition, QuatArg inRotation, EActivation inActivationMode = EActivation::Activate, bool inLockBodies = true) const;
  65. /// Get the position of the character
  66. RVec3 GetPosition(bool inLockBodies = true) const;
  67. /// Set the position of the character, optionally activating it.
  68. void SetPosition(RVec3Arg inPostion, EActivation inActivationMode = EActivation::Activate, bool inLockBodies = true);
  69. /// Get the rotation of the character
  70. Quat GetRotation(bool inLockBodies = true) const;
  71. /// Set the rotation of the character, optionally activating it.
  72. void SetRotation(QuatArg inRotation, EActivation inActivationMode = EActivation::Activate, bool inLockBodies = true);
  73. /// Position of the center of mass of the underlying rigid body
  74. RVec3 GetCenterOfMassPosition(bool inLockBodies = true) const;
  75. /// Calculate the world transform of the character
  76. RMat44 GetWorldTransform(bool inLockBodies = true) const;
  77. /// Update the layer of the character
  78. void SetLayer(ObjectLayer inLayer, bool inLockBodies = true);
  79. /// Switch the shape of the character (e.g. for stance). When inMaxPenetrationDepth is not FLT_MAX, it checks
  80. /// if the new shape collides before switching shape. Returns true if the switch succeeded.
  81. bool SetShape(const Shape *inShape, float inMaxPenetrationDepth, bool inLockBodies = true);
  82. /// @brief Get all contacts for the character at a particular location
  83. /// @param inPosition Position to test.
  84. /// @param inRotation Rotation at which to test the shape.
  85. /// @param inMovementDirection A hint in which direction the character is moving, will be used to calculate a proper normal.
  86. /// @param inMaxSeparationDistance How much distance around the character you want to report contacts in (can be 0 to match the character exactly).
  87. /// @param inShape Shape to test collision with.
  88. /// @param inBaseOffset All hit results will be returned relative to this offset, can be zero to get results in world position, but when you're testing far from the origin you get better precision by picking a position that's closer e.g. GetPosition() since floats are most accurate near the origin
  89. /// @param ioCollector Collision collector that receives the collision results.
  90. /// @param inLockBodies If the collision query should use the locking body interface (true) or the non locking body interface (false)
  91. void CheckCollision(RVec3Arg inPosition, QuatArg inRotation, Vec3Arg inMovementDirection, float inMaxSeparationDistance, const Shape *inShape, RVec3Arg inBaseOffset, CollideShapeCollector &ioCollector, bool inLockBodies = true) const;
  92. private:
  93. /// Check collisions between inShape and the world using the center of mass transform
  94. void CheckCollision(RMat44Arg inCenterOfMassTransform, Vec3Arg inMovementDirection, float inMaxSeparationDistance, const Shape *inShape, RVec3Arg inBaseOffset, CollideShapeCollector &ioCollector, bool inLockBodies) const;
  95. /// Check collisions between inShape and the world using the current position / rotation of the character
  96. void CheckCollision(const Shape *inShape, float inMaxSeparationDistance, RVec3Arg inBaseOffset, CollideShapeCollector &ioCollector, bool inLockBodies) const;
  97. /// The body of this character
  98. BodyID mBodyID;
  99. /// The layer the body is in
  100. ObjectLayer mLayer;
  101. };
  102. JPH_NAMESPACE_END