Character.h 6.3 KB

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