ForceRegion.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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/ForceRegion.h>
  9. #include <PhysX/ColliderShapeBus.h>
  10. #include <PhysX/MeshAsset.h>
  11. #include <Source/Utils.h>
  12. #include <AzCore/Math/Color.h>
  13. #include <AzFramework/Physics/ColliderComponentBus.h>
  14. #include <AzFramework/Physics/RigidBodyBus.h>
  15. namespace PhysX
  16. {
  17. /// Aggregates the AABB of all trigger collider components in an entity.
  18. struct TriggerAabbAggregator
  19. {
  20. AZ::Aabb operator()(AZ::Aabb& lhs, const AZ::Aabb& rhs) const
  21. {
  22. if (rhs == AZ::Aabb::CreateNull()) // Ignore non-trigger colliders that may have null AABB.
  23. {
  24. return lhs;
  25. }
  26. else
  27. {
  28. lhs.AddAabb(rhs);
  29. return lhs;
  30. }
  31. }
  32. };
  33. /// Aggregates points on trigger collider components in an entity.
  34. struct TriggerRandomPointsAggregator
  35. {
  36. Utils::Geometry::PointList operator()(Utils::Geometry::PointList& left
  37. , const Utils::Geometry::PointList& right) const
  38. {
  39. Utils::Geometry::PointList combinedPoints;
  40. combinedPoints.reserve(left.size() + right.size());
  41. combinedPoints.insert(combinedPoints.end(), left.begin(), left.end());
  42. combinedPoints.insert(combinedPoints.end(), right.begin(), right.end());
  43. return combinedPoints;
  44. }
  45. };
  46. void ForceRegion::Reflect(AZ::ReflectContext* context)
  47. {
  48. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  49. {
  50. BaseForce::Reflect(*serializeContext);
  51. serializeContext->Class<ForceRegion>()
  52. ->Version(1)
  53. ->Field("Forces", &ForceRegion::m_forces)
  54. ;
  55. if (auto editContext = serializeContext->GetEditContext())
  56. {
  57. editContext->Class<ForceRegion>(
  58. "Force Region", "Applies forces on entities within a region.")
  59. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  60. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  61. ->DataElement(AZ::Edit::UIHandlers::Default, &ForceRegion::m_forces, "Forces", "Forces acting in the region.")
  62. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  63. ;
  64. }
  65. }
  66. }
  67. ForceRegion::ForceRegion(const ForceRegion& forceRegion)
  68. {
  69. // Force region must be deep copied as it contains pointers
  70. AZ::SerializeContext* context = nullptr;
  71. AZ::ComponentApplicationBus::BroadcastResult(context, &AZ::ComponentApplicationBus::Events::GetSerializeContext);
  72. context->CloneObjectInplace<ForceRegion>(*this, &forceRegion);
  73. }
  74. void ForceRegion::Activate(AZ::EntityId entityId)
  75. {
  76. m_entityId = entityId;
  77. m_regionParams = ForceRegionUtil::CreateRegionParams(m_entityId);
  78. AZ::TransformNotificationBus::MultiHandler::BusConnect(m_entityId);
  79. LmbrCentral::SplineComponentNotificationBus::Handler::BusConnect(m_entityId);
  80. ForceRegionRequestBus::Handler::BusConnect(m_entityId);
  81. Physics::ColliderComponentEventBus::Handler::BusConnect(m_entityId);
  82. for (auto& force : m_forces)
  83. {
  84. force->Activate(m_entityId);
  85. }
  86. AZ::TransformBus::EventResult(m_worldTransform, m_entityId, &AZ::TransformBus::Events::GetWorldTM);
  87. }
  88. void ForceRegion::Deactivate()
  89. {
  90. m_entityId.SetInvalid();
  91. for (auto& force : m_forces)
  92. {
  93. force->Deactivate();
  94. }
  95. Physics::ColliderComponentEventBus::Handler::BusDisconnect();
  96. ForceRegionRequestBus::Handler::BusDisconnect();
  97. LmbrCentral::SplineComponentNotificationBus::Handler::BusDisconnect();
  98. AZ::TransformNotificationBus::MultiHandler::BusDisconnect();
  99. }
  100. AZ::Vector3 ForceRegion::CalculateNetForce(const EntityParams& entity) const
  101. {
  102. auto totalForce = AZ::Vector3::CreateZero();
  103. for (auto& force : m_forces)
  104. {
  105. totalForce += force->CalculateForce(entity, m_regionParams);
  106. }
  107. PhysX::ForceRegionNotificationBus::Broadcast(&ForceRegionNotificationBus::Events::OnCalculateNetForce
  108. , m_regionParams.m_id
  109. , entity.m_id
  110. , totalForce.GetNormalized()
  111. , totalForce.GetLength());
  112. return totalForce;
  113. }
  114. void ForceRegion::ClearForces()
  115. {
  116. for (auto& force : m_forces)
  117. {
  118. force->Deactivate();
  119. }
  120. m_forces.clear();
  121. }
  122. PhysX::RegionParams ForceRegion::GetRegionParams() const
  123. {
  124. return m_regionParams;
  125. }
  126. void ForceRegion::OnTransformChanged(const AZ::Transform& /*local*/, const AZ::Transform& world)
  127. {
  128. m_worldTransform = world;
  129. m_regionParams.m_position = world.GetTranslation();
  130. m_regionParams.m_scale = world.GetUniformScale();
  131. m_regionParams.m_rotation = world.GetRotation();
  132. AZ::EBusReduceResult<AZ::Aabb, PhysX::TriggerAabbAggregator> triggerAabb;
  133. triggerAabb.value = AZ::Aabb::CreateNull();
  134. ColliderShapeRequestBus::EventResult(triggerAabb
  135. , m_entityId
  136. , &ColliderShapeRequestBus::Events::GetColliderShapeAabb);
  137. m_regionParams.m_aabb = triggerAabb.value;
  138. }
  139. void ForceRegion::OnColliderChanged()
  140. {
  141. m_regionParams = ForceRegionUtil::CreateRegionParams(m_entityId);
  142. }
  143. void ForceRegion::AddForceWorldSpace(const AZ::Vector3& direction, float magnitude)
  144. {
  145. AddAndActivateForce(AZStd::make_unique<ForceWorldSpace>(direction, magnitude));
  146. }
  147. void ForceRegion::AddForceLocalSpace(const AZ::Vector3& direction, float magnitude)
  148. {
  149. AddAndActivateForce(AZStd::make_unique<ForceLocalSpace>(direction, magnitude));
  150. }
  151. void ForceRegion::AddForcePoint(float magnitude)
  152. {
  153. AddAndActivateForce(AZStd::make_unique<ForcePoint>(magnitude));
  154. }
  155. void ForceRegion::AddForceSplineFollow(float dampingRatio, float frequency, float targetSpeed, float lookAhead)
  156. {
  157. AddAndActivateForce(AZStd::make_unique<ForceSplineFollow>(dampingRatio, frequency, targetSpeed, lookAhead));
  158. }
  159. void ForceRegion::AddForceSimpleDrag(float dragCoefficient, float volumeDensity)
  160. {
  161. AddAndActivateForce(AZStd::make_unique<ForceSimpleDrag>(dragCoefficient, volumeDensity));
  162. }
  163. void ForceRegion::AddForceLinearDamping(float damping)
  164. {
  165. AddAndActivateForce(AZStd::make_unique<ForceLinearDamping>(damping));
  166. }
  167. void ForceRegion::AddAndActivateForce(AZStd::unique_ptr<BaseForce> force)
  168. {
  169. AZ_Assert(force, "Failed to add and activate null force.");
  170. if (nullptr == force)
  171. {
  172. return;
  173. }
  174. m_forces.push_back(AZStd::move(force));
  175. m_forces.back()->Activate(m_entityId);
  176. }
  177. void ForceRegion::OnSplineChanged()
  178. {
  179. LmbrCentral::SplineComponentRequestBus::EventResult(m_regionParams.m_spline
  180. , m_entityId
  181. , &LmbrCentral::SplineComponentRequestBus::Events::GetSpline);
  182. }
  183. RegionParams ForceRegionUtil::CreateRegionParams(AZ::EntityId entityId)
  184. {
  185. RegionParams regionParams;
  186. regionParams.m_id = entityId;
  187. AZ::Transform worldTransform = AZ::Transform::CreateIdentity();
  188. AZ::TransformBus::EventResult(worldTransform
  189. , entityId
  190. , &AZ::TransformBus::Events::GetWorldTM);
  191. regionParams.m_position = worldTransform.GetTranslation();
  192. regionParams.m_scale = worldTransform.GetUniformScale();
  193. regionParams.m_rotation = worldTransform.GetRotation();
  194. LmbrCentral::SplineComponentRequestBus::EventResult(regionParams.m_spline
  195. , entityId
  196. , &LmbrCentral::SplineComponentRequestBus::Events::GetSpline);
  197. AZ::EBusReduceResult<AZ::Aabb, PhysX::TriggerAabbAggregator> triggerAabb;
  198. triggerAabb.value = AZ::Aabb::CreateNull();
  199. ColliderShapeRequestBus::EventResult(triggerAabb
  200. , entityId
  201. , &ColliderShapeRequestBus::Events::GetColliderShapeAabb);
  202. regionParams.m_aabb = triggerAabb.value;
  203. return regionParams;
  204. }
  205. EntityParams ForceRegionUtil::CreateEntityParams(AZ::EntityId entityId)
  206. {
  207. EntityParams entityParams;
  208. entityParams.m_id = entityId;
  209. AzPhysics::RigidBody* rigidBody = nullptr;
  210. if (auto* handler = Physics::RigidBodyRequestBus::FindFirstHandler(entityId))
  211. {
  212. rigidBody = handler->GetRigidBody();
  213. }
  214. if (!rigidBody)
  215. {
  216. AZ_Error("PhysX", false, "ForceRegionUtil::CreateEntityParams: No rigid body for entity [%llu]", entityId);
  217. return entityParams;
  218. }
  219. entityParams.m_position = rigidBody->GetPosition();
  220. entityParams.m_velocity = rigidBody->GetLinearVelocity();
  221. entityParams.m_mass = rigidBody->GetMass();
  222. entityParams.m_aabb = GetForceRegionAabb(entityId);
  223. return entityParams;
  224. }
  225. AZ::Aabb ForceRegionUtil::GetForceRegionAabb(AZ::EntityId entityId)
  226. {
  227. AZ::EBusReduceResult<AZ::Aabb, PhysX::TriggerAabbAggregator> triggerAabb;
  228. triggerAabb.value = AZ::Aabb::CreateNull();
  229. ColliderShapeRequestBus::EventResult(triggerAabb
  230. , entityId
  231. , &ColliderShapeRequestBus::Events::GetColliderShapeAabb);
  232. return triggerAabb.value;
  233. }
  234. }