HeightfieldColliderComponent.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. #include <Source/HeightfieldColliderComponent.h>
  9. #include <AzCore/Component/Entity.h>
  10. #include <AzCore/Component/TransformBus.h>
  11. #include <AzCore/Serialization/SerializeContext.h>
  12. #include <AzCore/std/smart_ptr/make_shared.h>
  13. #include <AzFramework/Physics/Collision/CollisionGroups.h>
  14. #include <AzFramework/Physics/Collision/CollisionLayers.h>
  15. #include <AzFramework/Physics/Common/PhysicsSimulatedBody.h>
  16. #include <AzFramework/Physics/Configuration/StaticRigidBodyConfiguration.h>
  17. #include <AzFramework/Physics/Utils.h>
  18. #include <Source/RigidBodyStatic.h>
  19. #include <Source/SystemComponent.h>
  20. #include <Source/Utils.h>
  21. #include <PhysX/MathConversion.h>
  22. #include <PhysX/PhysXLocks.h>
  23. #include <Scene/PhysXScene.h>
  24. namespace PhysX
  25. {
  26. void HeightfieldColliderComponent::Reflect(AZ::ReflectContext* context)
  27. {
  28. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  29. {
  30. serializeContext->Class<HeightfieldColliderComponent, AZ::Component>()
  31. ->Version(2)
  32. ->Field("ColliderConfiguration", &HeightfieldColliderComponent::m_colliderConfig)
  33. ->Field("BakedHeightfieldAsset", &HeightfieldColliderComponent::m_bakedHeightfieldAsset)
  34. ;
  35. }
  36. }
  37. void HeightfieldColliderComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  38. {
  39. provided.push_back(AZ_CRC_CE("PhysicsWorldBodyService"));
  40. provided.push_back(AZ_CRC_CE("PhysicsColliderService"));
  41. provided.push_back(AZ_CRC_CE("PhysicsHeightfieldColliderService"));
  42. }
  43. void HeightfieldColliderComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  44. {
  45. required.push_back(AZ_CRC_CE("PhysicsHeightfieldProviderService"));
  46. }
  47. void HeightfieldColliderComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  48. {
  49. incompatible.push_back(AZ_CRC_CE("PhysicsColliderService"));
  50. // Incompatible with other rigid bodies because it handles its own rigid body
  51. // internally and it would conflict if another rigid body is added to the entity.
  52. incompatible.push_back(AZ_CRC_CE("PhysicsRigidBodyService"));
  53. }
  54. HeightfieldColliderComponent::~HeightfieldColliderComponent()
  55. {
  56. }
  57. void HeightfieldColliderComponent::OnAssetReady(AZ::Data::Asset<AZ::Data::AssetData> asset)
  58. {
  59. if (asset == m_bakedHeightfieldAsset)
  60. {
  61. m_bakedHeightfieldAsset = asset;
  62. Pipeline::HeightFieldAsset* heightfieldAsset = m_bakedHeightfieldAsset.Get();
  63. bool minMaxHeightsMatch = AZ::IsClose(m_shapeConfig->GetMinHeightBounds(), heightfieldAsset->GetMinHeight()) &&
  64. AZ::IsClose(m_shapeConfig->GetMaxHeightBounds(), heightfieldAsset->GetMaxHeight());
  65. if (!minMaxHeightsMatch)
  66. {
  67. AZ_Warning(
  68. "PhysX",
  69. false,
  70. "MinMax heights mismatch between baked heightfield and heightfield provider. Entity [%s]. "
  71. "Terrain [%0.2f, %0.2f], Asset [%0.2f, %0.2f]",
  72. GetEntity()->GetName().c_str(),
  73. m_shapeConfig->GetMinHeightBounds(),
  74. m_shapeConfig->GetMaxHeightBounds(),
  75. heightfieldAsset->GetMinHeight(),
  76. heightfieldAsset->GetMaxHeight());
  77. }
  78. physx::PxHeightField* pxHeightfield = heightfieldAsset->GetHeightField();
  79. // Since PxHeightfield will have shared ownership in both HeightfieldAsset and HeightfieldShapeConfiguration,
  80. // we need to increment the reference counter here. Both of these places call release() in destructors,
  81. // so we need to avoid double deletion this way.
  82. pxHeightfield->acquireReference();
  83. m_shapeConfig->SetCachedNativeHeightfield(pxHeightfield);
  84. InitHeightfieldCollider(HeightfieldCollider::DataSource::UseCachedHeightfield);
  85. }
  86. }
  87. void HeightfieldColliderComponent::OnAssetReload(AZ::Data::Asset<AZ::Data::AssetData> asset)
  88. {
  89. if (asset == m_bakedHeightfieldAsset)
  90. {
  91. m_heightfieldCollider.reset();
  92. OnAssetReady(asset);
  93. }
  94. }
  95. void HeightfieldColliderComponent::OnAssetError([[maybe_unused]] AZ::Data::Asset<AZ::Data::AssetData> asset)
  96. {
  97. InitHeightfieldCollider(HeightfieldCollider::DataSource::GenerateNewHeightfield);
  98. }
  99. void HeightfieldColliderComponent::Activate()
  100. {
  101. *m_shapeConfig = Utils::CreateBaseHeightfieldShapeConfiguration(GetEntityId());
  102. AZ::Data::AssetId assetId = m_bakedHeightfieldAsset.GetId();
  103. AZ::Data::AssetData::AssetStatus assetStatus = m_bakedHeightfieldAsset.GetStatus();
  104. if (assetId.IsValid() && assetStatus != AZ::Data::AssetData::AssetStatus::Error)
  105. {
  106. if (m_bakedHeightfieldAsset.GetStatus() == AZ::Data::AssetData::AssetStatus::NotLoaded)
  107. {
  108. m_bakedHeightfieldAsset.QueueLoad();
  109. }
  110. AZ::Data::AssetBus::Handler::BusConnect(assetId);
  111. }
  112. else
  113. {
  114. InitHeightfieldCollider(HeightfieldCollider::DataSource::GenerateNewHeightfield);
  115. }
  116. }
  117. void HeightfieldColliderComponent::Deactivate()
  118. {
  119. AZ::Data::AssetBus::Handler::BusDisconnect();
  120. Physics::CollisionFilteringRequestBus::Handler::BusDisconnect();
  121. ColliderComponentRequestBus::Handler::BusDisconnect();
  122. m_heightfieldCollider.reset();
  123. }
  124. void HeightfieldColliderComponent::BlockOnPendingJobs()
  125. {
  126. if (m_heightfieldCollider)
  127. {
  128. m_heightfieldCollider->BlockOnPendingJobs();
  129. }
  130. }
  131. void HeightfieldColliderComponent::SetColliderConfiguration(const Physics::ColliderConfiguration& colliderConfig)
  132. {
  133. if (GetEntity()->GetState() == AZ::Entity::State::Active)
  134. {
  135. AZ_Warning(
  136. "PhysX", false, "Trying to call SetShapeConfiguration for entity \"%s\" while entity is active.",
  137. GetEntity()->GetName().c_str());
  138. return;
  139. }
  140. *m_colliderConfig = colliderConfig;
  141. }
  142. void HeightfieldColliderComponent::SetBakedHeightfieldAsset(const AZ::Data::Asset<Pipeline::HeightFieldAsset>& heightfieldAsset)
  143. {
  144. m_bakedHeightfieldAsset = heightfieldAsset;
  145. }
  146. // ColliderComponentRequestBus
  147. AzPhysics::ShapeColliderPairList HeightfieldColliderComponent::GetShapeConfigurations()
  148. {
  149. AzPhysics::ShapeColliderPairList shapeConfigurationList({ AzPhysics::ShapeColliderPair(m_colliderConfig, m_shapeConfig) });
  150. return shapeConfigurationList;
  151. }
  152. AZStd::shared_ptr<Physics::Shape> HeightfieldColliderComponent::GetHeightfieldShape()
  153. {
  154. return m_heightfieldCollider->GetHeightfieldShape();
  155. }
  156. // ColliderComponentRequestBus
  157. AZStd::vector<AZStd::shared_ptr<Physics::Shape>> HeightfieldColliderComponent::GetShapes()
  158. {
  159. return { GetHeightfieldShape() };
  160. }
  161. // CollisionFilteringRequestBus
  162. void HeightfieldColliderComponent::SetCollisionLayer(const AZStd::string& layerName, AZ::Crc32 colliderTag)
  163. {
  164. if (auto heightfield = GetHeightfieldShape())
  165. {
  166. if (Physics::Utils::FilterTag(heightfield->GetTag(), colliderTag))
  167. {
  168. bool success = false;
  169. AzPhysics::CollisionLayer layer;
  170. Physics::CollisionRequestBus::BroadcastResult(
  171. success, &Physics::CollisionRequests::TryGetCollisionLayerByName, layerName, layer);
  172. if (success)
  173. {
  174. heightfield->SetCollisionLayer(layer);
  175. }
  176. }
  177. }
  178. }
  179. // CollisionFilteringRequestBus
  180. AZStd::string HeightfieldColliderComponent::GetCollisionLayerName()
  181. {
  182. AZStd::string layerName;
  183. if (auto heightfield = GetHeightfieldShape())
  184. {
  185. Physics::CollisionRequestBus::BroadcastResult(
  186. layerName, &Physics::CollisionRequests::GetCollisionLayerName, heightfield->GetCollisionLayer());
  187. }
  188. return layerName;
  189. }
  190. // CollisionFilteringRequestBus
  191. void HeightfieldColliderComponent::SetCollisionGroup(const AZStd::string& groupName, AZ::Crc32 colliderTag)
  192. {
  193. if (auto heightfield = GetHeightfieldShape())
  194. {
  195. if (Physics::Utils::FilterTag(heightfield->GetTag(), colliderTag))
  196. {
  197. bool success = false;
  198. AzPhysics::CollisionGroup group;
  199. Physics::CollisionRequestBus::BroadcastResult(
  200. success, &Physics::CollisionRequests::TryGetCollisionGroupByName, groupName, group);
  201. if (success)
  202. {
  203. heightfield->SetCollisionGroup(group);
  204. }
  205. }
  206. }
  207. }
  208. // CollisionFilteringRequestBus
  209. AZStd::string HeightfieldColliderComponent::GetCollisionGroupName()
  210. {
  211. AZStd::string groupName;
  212. if (auto heightfield = GetHeightfieldShape())
  213. {
  214. Physics::CollisionRequestBus::BroadcastResult(
  215. groupName, &Physics::CollisionRequests::GetCollisionGroupName, heightfield->GetCollisionGroup());
  216. }
  217. return groupName;
  218. }
  219. // CollisionFilteringRequestBus
  220. void HeightfieldColliderComponent::ToggleCollisionLayer(const AZStd::string& layerName, AZ::Crc32 colliderTag, bool enabled)
  221. {
  222. if (auto heightfield = GetHeightfieldShape())
  223. {
  224. if (Physics::Utils::FilterTag(heightfield->GetTag(), colliderTag))
  225. {
  226. bool success = false;
  227. AzPhysics::CollisionLayer layer;
  228. Physics::CollisionRequestBus::BroadcastResult(
  229. success, &Physics::CollisionRequests::TryGetCollisionLayerByName, layerName, layer);
  230. if (success)
  231. {
  232. auto group = heightfield->GetCollisionGroup();
  233. group.SetLayer(layer, enabled);
  234. heightfield->SetCollisionGroup(group);
  235. }
  236. }
  237. }
  238. }
  239. void HeightfieldColliderComponent::InitHeightfieldCollider(HeightfieldCollider::DataSource heightfieldDataSource)
  240. {
  241. const AZ::EntityId entityId = GetEntityId();
  242. AzPhysics::SceneHandle sceneHandle = AzPhysics::InvalidSceneHandle;
  243. Physics::DefaultWorldBus::BroadcastResult(sceneHandle, &Physics::DefaultWorldRequests::GetDefaultSceneHandle);
  244. m_heightfieldCollider = AZStd::make_unique<HeightfieldCollider>(
  245. entityId, GetEntity()->GetName(), sceneHandle, m_colliderConfig, m_shapeConfig, heightfieldDataSource);
  246. ColliderComponentRequestBus::Handler::BusConnect(entityId);
  247. Physics::CollisionFilteringRequestBus::Handler::BusConnect(entityId);
  248. }
  249. } // namespace PhysX