AnimatedHitVolumesComponent.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project. For complete copyright and license terms please see the LICENSE at the root of this distribution.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0 OR MIT
  5. *
  6. */
  7. #include <Source/Components/AnimatedHitVolumesComponent.h>
  8. #include <AzFramework/Physics/Common/PhysicsTypes.h>
  9. #include <AzFramework/Physics/CharacterBus.h>
  10. #include <AzFramework/Physics/Character.h>
  11. #include <AzFramework/Physics/SystemBus.h>
  12. #include <AzFramework/Physics/Material.h>
  13. #include <MCore/Source/AzCoreConversions.h>
  14. #include <Integration/ActorComponentBus.h>
  15. #include <Integration/AnimGraphNetworkingBus.h>
  16. namespace MultiplayerSample
  17. {
  18. AZ_CVAR(bool, bg_DrawArticulatedHitVolumes, false, nullptr, AZ::ConsoleFunctorFlags::Null, "Enables debug draw of articulated hit volumes");
  19. AZ_CVAR(float, bg_DrawDebugHitVolumeLifetime, 0.0f, nullptr, AZ::ConsoleFunctorFlags::Null, "The lifetime for hit volume draw-debug shapes");
  20. AZ_CVAR(float, bg_RewindPositionTolerance, 0.0001f, nullptr, AZ::ConsoleFunctorFlags::Null, "Don't sync the physx entity if the square of delta position is less than this value");
  21. AZ_CVAR(float, bg_RewindOrientationTolerance, 0.001f, nullptr, AZ::ConsoleFunctorFlags::Null, "Don't sync the physx entity if the square of delta orientation is less than this value");
  22. AnimatedHitVolumesComponent::AnimatedHitVolume::AnimatedHitVolume
  23. (
  24. AzNetworking::ConnectionId connectionId,
  25. Physics::CharacterRequests* character,
  26. const char* hitVolumeName,
  27. const Physics::ColliderConfiguration* colliderConfig,
  28. const Physics::ShapeConfiguration* shapeConfig,
  29. const uint32_t jointIndex
  30. )
  31. : m_colliderConfig(colliderConfig)
  32. , m_shapeConfig(shapeConfig)
  33. , m_jointIndex(jointIndex)
  34. {
  35. m_transform.SetOwningConnectionId(connectionId);
  36. m_colliderOffSetTransform = AZ::Transform::CreateFromQuaternionAndTranslation(m_colliderConfig->m_rotation, m_colliderConfig->m_position);
  37. if (m_colliderConfig->m_isExclusive)
  38. {
  39. Physics::SystemRequestBus::BroadcastResult(m_physicsShape, &Physics::SystemRequests::CreateShape, *m_colliderConfig, *m_shapeConfig);
  40. }
  41. else
  42. {
  43. Physics::ColliderConfiguration colliderConfiguration = *m_colliderConfig;
  44. colliderConfiguration.m_isExclusive = true;
  45. colliderConfiguration.m_isSimulated = false;
  46. colliderConfiguration.m_isInSceneQueries = true;
  47. Physics::SystemRequestBus::BroadcastResult(m_physicsShape, &Physics::SystemRequests::CreateShape, colliderConfiguration, *m_shapeConfig);
  48. }
  49. if (m_physicsShape)
  50. {
  51. m_physicsShape->SetName(hitVolumeName);
  52. m_physicsShape->SetCollisionLayer(AzPhysics::CollisionLayer::TouchBend);
  53. character->GetCharacter()->AttachShape(m_physicsShape);
  54. }
  55. }
  56. void AnimatedHitVolumesComponent::AnimatedHitVolume::UpdateTransform(const AZ::Transform& transform)
  57. {
  58. m_transform = transform;
  59. m_physicsShape->SetLocalPose(transform.GetTranslation(), transform.GetRotation());
  60. }
  61. void AnimatedHitVolumesComponent::AnimatedHitVolume::SyncToCurrentTransform()
  62. {
  63. const AZ::Transform& rewoundTransform = m_transform.Get();
  64. const AZ::Transform physicsTransform = AZ::Transform::CreateFromQuaternionAndTranslation(m_physicsShape->GetLocalPose().second, m_physicsShape->GetLocalPose().first);
  65. // Don't call SetLocalPose unless the transforms are actually different
  66. const AZ::Vector3 positionDelta = physicsTransform.GetTranslation() - rewoundTransform.GetTranslation();
  67. const AZ::Quaternion orientationDelta = physicsTransform.GetRotation() - rewoundTransform.GetRotation();
  68. if ((positionDelta.GetLengthSq() >= bg_RewindPositionTolerance) || (orientationDelta.GetLengthSq() >= bg_RewindOrientationTolerance))
  69. {
  70. m_physicsShape->SetLocalPose(rewoundTransform.GetTranslation(), rewoundTransform.GetRotation());
  71. }
  72. }
  73. void AnimatedHitVolumesComponent::AnimatedHitVolumesComponent::Reflect(AZ::ReflectContext* context)
  74. {
  75. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  76. if (serializeContext)
  77. {
  78. serializeContext->Class<AnimatedHitVolumesComponent, AnimatedHitVolumesComponentBase>()
  79. ->Version(1);
  80. }
  81. AnimatedHitVolumesComponentBase::Reflect(context);
  82. }
  83. AnimatedHitVolumesComponent::AnimatedHitVolumesComponent()
  84. : m_syncRewindHandler([this]() { OnSyncRewind(); })
  85. , m_preRenderHandler([this](float deltaTime, float blendFactor) { OnPreRender(deltaTime, blendFactor); })
  86. {
  87. ;
  88. }
  89. void AnimatedHitVolumesComponent::OnInit()
  90. {
  91. ;
  92. }
  93. void AnimatedHitVolumesComponent::OnActivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  94. {
  95. EMotionFX::Integration::ActorComponentNotificationBus::Handler::BusConnect(GetEntityId());
  96. GetNetBindComponent()->AddEntitySyncRewindEventHandler(m_syncRewindHandler);
  97. m_physicsCharacter = Physics::CharacterRequestBus::FindFirstHandler(GetEntityId());
  98. }
  99. void AnimatedHitVolumesComponent::OnDeactivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  100. {
  101. DestroyHitVolumes();
  102. EMotionFX::Integration::ActorComponentNotificationBus::Handler::BusDisconnect();
  103. }
  104. void AnimatedHitVolumesComponent::OnPreRender([[maybe_unused]] float deltaTime, [[maybe_unused]] float blendFactor)
  105. {
  106. if (m_animatedHitVolumes.size() <= 0)
  107. {
  108. CreateHitVolumes();
  109. }
  110. AZ::Vector3 position, scale;
  111. AZ::Quaternion rotation;
  112. for (AnimatedHitVolume& hitVolume : m_animatedHitVolumes)
  113. {
  114. m_actorComponent->GetJointTransformComponents(hitVolume.m_jointIndex, EMotionFX::Integration::Space::ModelSpace, position, rotation, scale);
  115. hitVolume.UpdateTransform(AZ::Transform::CreateFromQuaternionAndTranslation(rotation, position) * hitVolume.m_colliderOffSetTransform);
  116. }
  117. }
  118. void AnimatedHitVolumesComponent::OnTransformUpdate([[maybe_unused]] const AZ::Transform& transform)
  119. {
  120. OnSyncRewind();
  121. }
  122. void AnimatedHitVolumesComponent::OnSyncRewind()
  123. {
  124. if (m_physicsCharacter && m_physicsCharacter->GetCharacter())
  125. {
  126. uint32_t frameId = static_cast<uint32_t>(Multiplayer::GetNetworkTime()->GetHostFrameId());
  127. m_physicsCharacter->GetCharacter()->SetFrameId(frameId);
  128. }
  129. for (AnimatedHitVolume& hitVolume : m_animatedHitVolumes)
  130. {
  131. hitVolume.SyncToCurrentTransform();
  132. }
  133. }
  134. void AnimatedHitVolumesComponent::CreateHitVolumes()
  135. {
  136. if (m_physicsCharacter == nullptr || m_actorComponent == nullptr)
  137. {
  138. return;
  139. }
  140. const Physics::AnimationConfiguration* physicsConfig = m_actorComponent->GetPhysicsConfig();
  141. if (physicsConfig == nullptr)
  142. {
  143. return;
  144. }
  145. m_hitDetectionConfig = &physicsConfig->m_hitDetectionConfig;
  146. const AzNetworking::ConnectionId owningConnectionId = GetNetBindComponent()->GetOwningConnectionId();
  147. m_animatedHitVolumes.reserve(m_hitDetectionConfig->m_nodes.size());
  148. for (const Physics::CharacterColliderNodeConfiguration& nodeConfig : m_hitDetectionConfig->m_nodes)
  149. {
  150. const AZStd::size_t jointIndex = m_actorComponent->GetJointIndexByName(nodeConfig.m_name.c_str());
  151. if (jointIndex == EMotionFX::Integration::ActorComponentRequests::s_invalidJointIndex)
  152. {
  153. continue;
  154. }
  155. for (const AzPhysics::ShapeColliderPair& coliderPair : nodeConfig.m_shapes)
  156. {
  157. const Physics::ColliderConfiguration* colliderConfig = coliderPair.first.get();
  158. Physics::ShapeConfiguration* shapeConfig = coliderPair.second.get();
  159. m_animatedHitVolumes.emplace_back(owningConnectionId, m_physicsCharacter, nodeConfig.m_name.c_str(), colliderConfig, shapeConfig, aznumeric_cast<uint32_t>(jointIndex));
  160. }
  161. }
  162. }
  163. void AnimatedHitVolumesComponent::DestroyHitVolumes()
  164. {
  165. m_animatedHitVolumes.clear();
  166. }
  167. void AnimatedHitVolumesComponent::OnActorInstanceCreated([[maybe_unused]] EMotionFX::ActorInstance* actorInstance)
  168. {
  169. m_actorComponent = EMotionFX::Integration::ActorComponentRequestBus::FindFirstHandler(GetEntity()->GetId());
  170. }
  171. void AnimatedHitVolumesComponent::OnActorInstanceDestroyed([[maybe_unused]] EMotionFX::ActorInstance* actorInstance)
  172. {
  173. m_actorComponent = nullptr;
  174. }
  175. }