Articulation.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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/std/containers/vector.h>
  10. #include <AzFramework/Physics/Character.h>
  11. #include <AzFramework/Physics/Shape.h>
  12. #include <AzFramework/Physics/SimulatedBodies/RigidBody.h>
  13. #include <AzFramework/Physics/RagdollPhysicsBus.h>
  14. #include <AzFramework/Physics/Common/PhysicsSimulatedBody.h>
  15. #include <AzFramework/Physics/Common/PhysicsJoint.h>
  16. #include <AzFramework/Physics/Configuration/RigidBodyConfiguration.h>
  17. #include <AzFramework/Physics/Configuration/SimulatedBodyConfiguration.h>
  18. #include <AzFramework/Physics/Configuration/JointConfiguration.h>
  19. #include <PhysX/ComponentTypeIds.h>
  20. #include <PhysX/Joint/Configuration/PhysXJointConfiguration.h>
  21. #include <PhysX/UserDataTypes.h>
  22. #include <Source/RigidBody.h>
  23. #include <Source/Articulation/ArticulationLinkConfiguration.h>
  24. namespace physx
  25. {
  26. class PxArticulationLink;
  27. class PxArticulationReducedCoordinate;
  28. }
  29. namespace PhysX
  30. {
  31. //! Maximum number of articulation links in a single articulation.
  32. constexpr size_t MaxArticulationLinks = 64;
  33. //! Configuration data for an articulation link. Contains references to child links.
  34. struct ArticulationLinkData
  35. {
  36. AZ_CLASS_ALLOCATOR(ArticulationLinkData, AZ::SystemAllocator);
  37. AZ_TYPE_INFO(PhysX::ArticulationLinkData, "{0FA03CD7-0FD2-4A80-8DB7-45DB944C8B24}");
  38. static void Reflect(AZ::ReflectContext* context);
  39. //! Articulation link specific properties for constructing PxArticulationLink.
  40. //! This data comes from Articulation Link Component in the Editor.
  41. ArticulationLinkConfiguration m_articulationLinkConfiguration;
  42. //! Data related to the collision shapes for this link.
  43. AzPhysics::ShapeColliderPairList m_shapeColliderConfigurationList;
  44. //! Cached local transform of this link relative to its parent.
  45. //! This is needed because at the time of constructing the articulation
  46. //! child entities corresponding to the links won't be active yet,
  47. //! so there's no way to query their local transform.
  48. AZ::Transform m_localTransform = AZ::Transform::CreateIdentity();
  49. //! Extra data for the articulation joint that is not in the link configuration.
  50. AZ::Transform m_jointLeadLocalFrame = AZ::Transform::CreateIdentity();
  51. AZ::Transform m_jointFollowerLocalFrame = AZ::Transform::CreateIdentity();
  52. //! List of child links. Together this forms a tree-like data structure representing the entire articulation.
  53. AZStd::vector<AZStd::shared_ptr<ArticulationLinkData>> m_childLinks;
  54. };
  55. //! Represents a single articulation link.
  56. class ArticulationLink
  57. : public AzPhysics::SimulatedBody
  58. {
  59. public:
  60. AZ_CLASS_ALLOCATOR(ArticulationLink, AZ::SystemAllocator);
  61. AZ_RTTI(PhysX::ArticulationLink, "{48A87D2B-3F12-4411-BE24-6F7534C77287}", AzPhysics::SimulatedBody);
  62. ~ArticulationLink() override = default;
  63. void SetupFromLinkData(const ArticulationLinkData& thisLinkData);
  64. void SetPxArticulationLink(physx::PxArticulationLink* pxLink);
  65. // AzPhysics::SimulatedBody overrides...
  66. AzPhysics::SceneQueryHit RayCast(const AzPhysics::RayCastRequest& request) override;
  67. AZ::Crc32 GetNativeType() const override;
  68. void* GetNativePointer() const override;
  69. AZ::EntityId GetEntityId() const override;
  70. AZ::Transform GetTransform() const override;
  71. void SetTransform(const AZ::Transform& transform) override;
  72. AZ::Vector3 GetPosition() const override;
  73. AZ::Quaternion GetOrientation() const override;
  74. AZ::Aabb GetAabb() const override;
  75. private:
  76. void AddCollisionShape(const ArticulationLinkData& thisLinkData);
  77. // PxArticulationLinks are managed by the articulation,
  78. // so we don't need to worry about calling release() here.
  79. physx::PxArticulationLink* m_pxLink = nullptr;
  80. ActorData m_actorData;
  81. AZStd::vector<AZStd::shared_ptr<Physics::Shape>> m_physicsShapes;
  82. };
  83. ArticulationLink* CreateArticulationLink(const ArticulationLinkConfiguration* articulationConfig);
  84. } // namespace PhysX