ActorClothColliders.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <AzCore/Component/EntityId.h>
  10. #include <AzCore/std/containers/vector.h>
  11. #include <AzCore/std/smart_ptr/unique_ptr.h>
  12. #include <AzCore/Math/Transform.h>
  13. namespace NvCloth
  14. {
  15. extern const int InvalidIndex;
  16. //! Base collider class with transform and joint information.
  17. struct Collider
  18. {
  19. //! Offset transform relative to the joint attached.
  20. AZ::Transform m_offsetTransform = AZ::Transform::CreateIdentity();
  21. //! Current transform in model space after animation applied.
  22. AZ::Transform m_currentModelSpaceTransform = AZ::Transform::CreateIdentity();
  23. //! Joint this collider is attached to.
  24. int m_jointIndex = InvalidIndex;
  25. };
  26. //! Describes the shape on an sphere collider.
  27. struct SphereCollider
  28. : public Collider
  29. {
  30. //! Radius of the sphere.
  31. float m_radius = 0.0f;
  32. int m_nvSphereIndex = InvalidIndex; //!< Identifies the sphere within m_spheres in ActorClothColliders.
  33. };
  34. //! Describes the shape on an sphere collider.
  35. struct CapsuleCollider
  36. : public Collider
  37. {
  38. //! Height of the capsule.
  39. float m_height = 0.0f;
  40. //! Radius of the capsule.
  41. float m_radius = 0.0f;
  42. int m_capsuleIndex = InvalidIndex; //!< Identifies first index of the capsule within m_capsuleIndices in ActorClothColliders.
  43. int m_sphereAIndex = InvalidIndex; //!< Identifies the first sphere within m_spheres in ActorClothColliders.
  44. int m_sphereBIndex = InvalidIndex; //!< Identifies the second sphere within m_spheres in ActorClothColliders.
  45. };
  46. //! Class to retrieve cloth colliders information from an actor on the same entity
  47. //! and updates their transform from skinning animation.
  48. //!
  49. //! @note There is a limit of 32 sphere colliders and 32 capsule colliders.
  50. //! In the case that all capsules use unique spheres then the maximum
  51. //! number of capsule would go down to 16, limited by the maximum number of spheres (32).
  52. class ActorClothColliders
  53. {
  54. public:
  55. AZ_TYPE_INFO(ActorClothColliders, "{EA2D9B6A-2493-4B6A-972E-BB639E16798E}");
  56. static AZStd::unique_ptr<ActorClothColliders> Create(AZ::EntityId entityId);
  57. explicit ActorClothColliders(AZ::EntityId entityId);
  58. //! Updates the colliders' transforms with the current pose of the actor.
  59. void Update();
  60. const AZStd::vector<SphereCollider>& GetSphereColliders() const;
  61. const AZStd::vector<CapsuleCollider>& GetCapsuleColliders() const;
  62. const AZStd::vector<AZ::Vector4>& GetSpheres() const;
  63. const AZStd::vector<uint32_t>& GetCapsuleIndices() const;
  64. private:
  65. void UpdateSphere(const SphereCollider& sphere);
  66. void UpdateCapsule(const CapsuleCollider& capsule);
  67. AZ::EntityId m_entityId;
  68. // Configuration data of spheres and capsules, describing their shape and transforms relative to joints.
  69. AZStd::vector<SphereCollider> m_sphereColliders;
  70. AZStd::vector<CapsuleCollider> m_capsuleColliders;
  71. // The current positions and radius of sphere colliders.
  72. // Every update, these positions are computed with the current pose of the actor.
  73. // Note: The spheres used to formed capsules are also part of this list.
  74. AZStd::vector<AZ::Vector4> m_spheres;
  75. // The sphere collider indices associated with capsules.
  76. // Each capsule is 2 indices within the list.
  77. AZStd::vector<uint32_t> m_capsuleIndices;
  78. };
  79. } // namespace NvCloth