Shape.h 4.2 KB

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