Shape.h 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 <PxPhysicsAPI.h>
  10. #include <AzFramework/Physics/Shape.h>
  11. #include <AzFramework/Physics/ShapeConfiguration.h>
  12. #include <AzCore/std/smart_ptr/enable_shared_from_this.h>
  13. #include <AzFramework/Physics/Collision/CollisionGroups.h>
  14. #include <AzFramework/Physics/Collision/CollisionLayers.h>
  15. namespace Physics
  16. {
  17. class Material;
  18. }
  19. namespace PhysX
  20. {
  21. class Material;
  22. class Shape
  23. : public Physics::Shape
  24. , public AZStd::enable_shared_from_this<Shape>
  25. {
  26. public:
  27. AZ_CLASS_ALLOCATOR(Shape, AZ::SystemAllocator);
  28. AZ_RTTI(Shape, "{A84BCCA2-7F29-4E17-830F-911E7BB3E80C}", Physics::Shape);
  29. Shape(const Physics::ColliderConfiguration& colliderConfiguration, const Physics::ShapeConfiguration& configuration);
  30. Shape(physx::PxShape* nativeShape);
  31. virtual ~Shape();
  32. Shape(Shape&& shape);
  33. Shape& operator=(Shape&& shape);
  34. Shape(const Shape& shape) = delete;
  35. Shape& operator=(const Shape& shape) = delete;
  36. // Physics::Shape overrides...
  37. void SetMaterial(const AZStd::shared_ptr<Physics::Material>& material) override;
  38. AZStd::shared_ptr<Physics::Material> GetMaterial() const override;
  39. Physics::MaterialId GetMaterialId() const override;
  40. void SetCollisionLayer(const AzPhysics::CollisionLayer& layer) override;
  41. AzPhysics::CollisionLayer GetCollisionLayer() const override;
  42. void SetCollisionGroup(const AzPhysics::CollisionGroup& group) override;
  43. AzPhysics::CollisionGroup GetCollisionGroup() const override;
  44. void SetName(const char* name) override;
  45. void SetLocalPose(const AZ::Vector3& offset, const AZ::Quaternion& rotation) override;
  46. AZStd::pair<AZ::Vector3, AZ::Quaternion> GetLocalPose() const override;
  47. float GetRestOffset() const override;
  48. float GetContactOffset() const override;
  49. void SetRestOffset(float restOffset) override;
  50. void SetContactOffset(float contactOffset) override;
  51. void* GetNativePointer() override;
  52. const void* GetNativePointer() const override;
  53. AZ::Crc32 GetTag() const override;
  54. void AttachedToActor(void* actor) override;
  55. void DetachedFromActor() override;
  56. AzPhysics::SceneQueryHit RayCast(const AzPhysics::RayCastRequest& worldSpaceRequest, const AZ::Transform& worldTransform) override;
  57. AzPhysics::SceneQueryHit RayCastLocal(const AzPhysics::RayCastRequest& localSpaceRequest) override;
  58. AZ::Aabb GetAabb(const AZ::Transform& worldTransform) const override;
  59. AZ::Aabb GetAabbLocal() const override;
  60. void GetGeometry(AZStd::vector<AZ::Vector3>& vertices, AZStd::vector<AZ::u32>& indices,
  61. const AZ::Aabb* optionalBounds = nullptr) const override;
  62. physx::PxShape* GetPxShape();
  63. void SetPhysXMaterials(const AZStd::vector<AZStd::shared_ptr<PhysX::Material>>& materials);
  64. const AZStd::vector<AZStd::shared_ptr<PhysX::Material>>& GetPhysXMaterials();
  65. bool IsTrigger() const;
  66. private:
  67. void BindMaterialsWithPxShape();
  68. void ExtractMaterialsFromPxShape();
  69. physx::PxScene* GetScene() const;
  70. void ReleasePxShape(physx::PxShape* shape);
  71. AzPhysics::SceneQueryHit RayCastInternal(const AzPhysics::RayCastRequest& worldSpaceRequest, const physx::PxTransform& pose);
  72. using PxShapeUniquePtr = AZStd::unique_ptr<physx::PxShape, AZStd::function<void(physx::PxShape*)>>;
  73. Shape() = default;
  74. PxShapeUniquePtr m_pxShape;
  75. AZStd::vector<AZStd::shared_ptr<PhysX::Material>> m_materials;
  76. AzPhysics::CollisionLayer m_collisionLayer;
  77. AzPhysics::CollisionGroup m_collisionGroup;
  78. AZ::Crc32 m_tag;
  79. physx::PxActor* m_attachedActor = nullptr;
  80. };
  81. }