NetworkRigidBodyTests.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 <CommonHierarchySetup.h>
  8. #include <MockInterfaces.h>
  9. #include <AzCore/Component/Entity.h>
  10. #include <AzCore/Console/Console.h>
  11. #include <AzCore/Name/Name.h>
  12. #include <AzCore/UnitTest/TestTypes.h>
  13. #include <AzCore/UnitTest/UnitTest.h>
  14. #include <AzFramework/Components/TransformComponent.h>
  15. #include <AzFramework/Physics/PhysicsScene.h>
  16. #include <AzFramework/Physics/SystemBus.h>
  17. #include <AzTest/AzTest.h>
  18. #include <Multiplayer/Components/NetBindComponent.h>
  19. #include <Multiplayer/Components/NetworkRigidBodyComponent.h>
  20. #include <Multiplayer/NetworkEntity/EntityReplication/EntityReplicator.h>
  21. #include <Source/RigidBodyComponent.h>
  22. namespace Multiplayer
  23. {
  24. using namespace testing;
  25. using namespace ::UnitTest;
  26. class MockSceneInterface : public AZ::Interface<AzPhysics::SceneInterface>::Registrar
  27. {
  28. public:
  29. MockSceneInterface() = default;
  30. ~MockSceneInterface() override = default;
  31. MOCK_METHOD4(
  32. AddJoint,
  33. AzPhysics::JointHandle(
  34. AzPhysics::SceneHandle,
  35. const AzPhysics::JointConfiguration*,
  36. AzPhysics::SimulatedBodyHandle,
  37. AzPhysics::SimulatedBodyHandle));
  38. MOCK_METHOD2(
  39. AddSimulatedBodies,
  40. AzPhysics::SimulatedBodyHandleList(AzPhysics::SceneHandle, const AzPhysics::SimulatedBodyConfigurationList&));
  41. MOCK_METHOD2(
  42. AddSimulatedBody, AzPhysics::SimulatedBodyHandle(AzPhysics::SceneHandle, const AzPhysics::SimulatedBodyConfiguration*));
  43. MOCK_METHOD2(DisableSimulationOfBody, void(AzPhysics::SceneHandle, AzPhysics::SimulatedBodyHandle));
  44. MOCK_METHOD2(EnableSimulationOfBody, void(AzPhysics::SceneHandle, AzPhysics::SimulatedBodyHandle));
  45. MOCK_METHOD1(FinishSimulation, void(AzPhysics::SceneHandle));
  46. MOCK_CONST_METHOD1(GetGravity, AZ::Vector3(AzPhysics::SceneHandle));
  47. MOCK_METHOD2(GetJointFromHandle, AzPhysics::Joint*(AzPhysics::SceneHandle, AzPhysics::JointHandle));
  48. MOCK_METHOD1(GetSceneHandle, AzPhysics::SceneHandle(const AZStd::string&));
  49. MOCK_METHOD1(GetScene, AzPhysics::Scene*(AzPhysics::SceneHandle));
  50. MOCK_METHOD2(
  51. GetSimulatedBodiesFromHandle, AzPhysics::SimulatedBodyList(AzPhysics::SceneHandle, const AzPhysics::SimulatedBodyHandleList&));
  52. MOCK_METHOD2(GetSimulatedBodyFromHandle, AzPhysics::SimulatedBody*(AzPhysics::SceneHandle, AzPhysics::SimulatedBodyHandle));
  53. MOCK_CONST_METHOD1(IsEnabled, bool(AzPhysics::SceneHandle));
  54. MOCK_METHOD2(QueryScene, AzPhysics::SceneQueryHits(AzPhysics::SceneHandle, const AzPhysics::SceneQueryRequest*));
  55. MOCK_METHOD3(QueryScene, bool(AzPhysics::SceneHandle sceneHandle, const AzPhysics::SceneQueryRequest* request, AzPhysics::SceneQueryHits&));
  56. MOCK_METHOD4(
  57. QuerySceneAsync,
  58. bool(
  59. AzPhysics::SceneHandle,
  60. AzPhysics::SceneQuery::AsyncRequestId,
  61. const AzPhysics::SceneQueryRequest*,
  62. AzPhysics::SceneQuery::AsyncCallback));
  63. MOCK_METHOD4(
  64. QuerySceneAsyncBatch,
  65. bool(
  66. AzPhysics::SceneHandle,
  67. AzPhysics::SceneQuery::AsyncRequestId,
  68. const AzPhysics::SceneQueryRequests&,
  69. AzPhysics::SceneQuery::AsyncBatchCallback));
  70. MOCK_METHOD2(QuerySceneBatch, AzPhysics::SceneQueryHitsList(AzPhysics::SceneHandle, const AzPhysics::SceneQueryRequests&));
  71. MOCK_METHOD2(
  72. RegisterSceneActiveSimulatedBodiesHandler,
  73. void(
  74. AzPhysics::SceneHandle,
  75. AZ::Event<AZStd::tuple<AZ::Crc32, signed char>, const AZStd::vector<AZStd::tuple<AZ::Crc32, int>>&, float>::Handler&));
  76. MOCK_METHOD2(
  77. RegisterSceneCollisionEventHandler,
  78. void(
  79. AzPhysics::SceneHandle,
  80. AZ::Event<AZStd::tuple<AZ::Crc32, signed char>, const AZStd::vector<AzPhysics::CollisionEvent>&>::Handler&));
  81. MOCK_METHOD2(
  82. RegisterSceneConfigurationChangedEventHandler,
  83. void(AzPhysics::SceneHandle, AZ::Event<AZStd::tuple<AZ::Crc32, signed char>, const AzPhysics::SceneConfiguration&>::Handler&));
  84. MOCK_METHOD2(
  85. RegisterSceneGravityChangedEvent,
  86. void(AzPhysics::SceneHandle, AZ::Event<AZStd::tuple<AZ::Crc32, signed char>, const AZ::Vector3&>::Handler&));
  87. MOCK_METHOD2(
  88. RegisterSceneSimulationFinishHandler, void(AzPhysics::SceneHandle, AzPhysics::SceneEvents::OnSceneSimulationFinishHandler&));
  89. MOCK_METHOD2(
  90. RegisterSceneSimulationStartHandler, void(AzPhysics::SceneHandle, AzPhysics::SceneEvents::OnSceneSimulationStartHandler&));
  91. MOCK_METHOD2(
  92. RegisterSceneTriggersEventHandler,
  93. void(
  94. AzPhysics::SceneHandle,
  95. AZ::Event<AZStd::tuple<AZ::Crc32, signed char>, const AZStd::vector<AzPhysics::TriggerEvent>&>::Handler&));
  96. MOCK_METHOD2(
  97. RegisterSimulationBodyAddedHandler,
  98. void(AzPhysics::SceneHandle, AZ::Event<AZStd::tuple<AZ::Crc32, signed char>, AZStd::tuple<AZ::Crc32, int>>::Handler&));
  99. MOCK_METHOD2(
  100. RegisterSimulationBodyRemovedHandler,
  101. void(AzPhysics::SceneHandle, AZ::Event<AZStd::tuple<AZ::Crc32, signed char>, AZStd::tuple<AZ::Crc32, int>>::Handler&));
  102. MOCK_METHOD2(
  103. RegisterSimulationBodySimulationDisabledHandler,
  104. void(AzPhysics::SceneHandle, AZ::Event<AZStd::tuple<AZ::Crc32, signed char>, AZStd::tuple<AZ::Crc32, int>>::Handler&));
  105. MOCK_METHOD2(
  106. RegisterSimulationBodySimulationEnabledHandler,
  107. void(AzPhysics::SceneHandle, AZ::Event<AZStd::tuple<AZ::Crc32, signed char>, AZStd::tuple<AZ::Crc32, int>>::Handler&));
  108. MOCK_METHOD2(RemoveJoint, void(AzPhysics::SceneHandle, AzPhysics::JointHandle));
  109. MOCK_METHOD2(RemoveSimulatedBodies, void(AzPhysics::SceneHandle, AzPhysics::SimulatedBodyHandleList&));
  110. MOCK_METHOD2(RemoveSimulatedBody, void(AzPhysics::SceneHandle, AzPhysics::SimulatedBodyHandle&));
  111. MOCK_METHOD2(SetEnabled, void(AzPhysics::SceneHandle, bool));
  112. MOCK_METHOD2(SetGravity, void(AzPhysics::SceneHandle, const AZ::Vector3&));
  113. MOCK_METHOD2(StartSimulation, void(AzPhysics::SceneHandle, float));
  114. MOCK_METHOD3(
  115. SuppressCollisionEvents,
  116. void(AzPhysics::SceneHandle, const AzPhysics::SimulatedBodyHandle&, const AzPhysics::SimulatedBodyHandle&));
  117. MOCK_METHOD3(
  118. UnsuppressCollisionEvents,
  119. void(AzPhysics::SceneHandle, const AzPhysics::SimulatedBodyHandle&, const AzPhysics::SimulatedBodyHandle&));
  120. };
  121. class MockPhysicsDefaultWorldRequestsHandler : public Physics::DefaultWorldBus::Handler
  122. {
  123. public:
  124. MockPhysicsDefaultWorldRequestsHandler()
  125. {
  126. Physics::DefaultWorldBus::Handler::BusConnect();
  127. }
  128. ~MockPhysicsDefaultWorldRequestsHandler()
  129. {
  130. Physics::DefaultWorldBus::Handler::BusDisconnect();
  131. }
  132. AzPhysics::SceneHandle GetDefaultSceneHandle() const
  133. {
  134. return { AZ::Crc32(), AzPhysics::SceneIndex(0) };
  135. }
  136. };
  137. class NetworkRigidBodyTests : public HierarchyTests
  138. {
  139. public:
  140. void SetUp() override
  141. {
  142. HierarchyTests::SetUp();
  143. m_rigidBodyDescriptor.reset(PhysX::RigidBodyComponent::CreateDescriptor());
  144. m_rigidBodyDescriptor->Reflect(m_serializeContext.get());
  145. m_netRigidBodyDescriptor.reset(NetworkRigidBodyComponent::CreateDescriptor());
  146. m_netRigidBodyDescriptor->Reflect(m_serializeContext.get());
  147. m_rigidBody = AZStd::make_unique<PhysX::RigidBody>();
  148. m_mockSceneInterface = AZStd::make_unique<Multiplayer::MockSceneInterface>();
  149. ON_CALL(*m_mockSceneInterface, GetSimulatedBodyFromHandle(_, _))
  150. .WillByDefault(Invoke(
  151. [this](AzPhysics::SceneHandle, AzPhysics::SimulatedBodyHandle)
  152. {
  153. return m_rigidBody.get();
  154. }
  155. ));
  156. m_mockDefaultWorld = AZStd::make_unique<Multiplayer::MockPhysicsDefaultWorldRequestsHandler>();
  157. m_root = AZStd::make_unique<EntityInfo>(1, "root", NetEntityId{ 1 }, EntityInfo::Role::Root);
  158. m_child = AZStd::make_unique<EntityInfo>(2, "child", NetEntityId{ 2 }, EntityInfo::Role::Child);
  159. CreateNetworkParentChild(*m_root, *m_child);
  160. AZ::Transform rootTransform = AZ::Transform::CreateIdentity();
  161. rootTransform.SetTranslation(AZ::Vector3::CreateOne());
  162. m_root->m_entity->FindComponent<AzFramework::TransformComponent>()->SetWorldTM(rootTransform);
  163. m_child->m_entity->FindComponent<AzFramework::TransformComponent>()->SetWorldTM(rootTransform);
  164. m_child->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_root->m_entity->GetId());
  165. m_child->m_entity->FindComponent<AzFramework::TransformComponent>()->SetLocalTM(AZ::Transform::CreateIdentity());
  166. AZ::EntityBus::Broadcast(&AZ::EntityBus::Events::OnEntityActivated, m_root->m_entity->GetId());
  167. }
  168. void TearDown() override
  169. {
  170. m_child.reset();
  171. m_root.reset();
  172. m_mockDefaultWorld.reset();
  173. m_mockSceneInterface.reset();
  174. m_rigidBody.reset();
  175. m_netRigidBodyDescriptor.reset();
  176. m_rigidBodyDescriptor.reset();
  177. HierarchyTests::TearDown();
  178. }
  179. void PopulateNetworkEntity(const EntityInfo& entityInfo)
  180. {
  181. entityInfo.m_entity->CreateComponent<AzFramework::TransformComponent>();
  182. entityInfo.m_entity->CreateComponent<NetBindComponent>();
  183. entityInfo.m_entity->CreateComponent<NetworkTransformComponent>();
  184. entityInfo.m_entity->CreateComponent<PhysX::RigidBodyComponent>();
  185. entityInfo.m_entity->CreateComponent<NetworkRigidBodyComponent>();
  186. }
  187. void CreateNetworkParentChild(EntityInfo& root, EntityInfo& child)
  188. {
  189. PopulateNetworkEntity(root);
  190. SetupEntity(root.m_entity, root.m_netId, NetEntityRole::Authority);
  191. PopulateNetworkEntity(child);
  192. SetupEntity(child.m_entity, child.m_netId, NetEntityRole::Authority);
  193. // Create an entity replicator for the child entity
  194. const NetworkEntityHandle childHandle(child.m_entity.get(), m_networkEntityTracker.get());
  195. child.m_replicator = AZStd::make_unique<EntityReplicator>(*m_entityReplicationManager, m_mockConnection.get(), NetEntityRole::Client, childHandle);
  196. child.m_replicator->Initialize(childHandle);
  197. // Create an entity replicator for the root entity
  198. const NetworkEntityHandle rootHandle(root.m_entity.get(), m_networkEntityTracker.get());
  199. root.m_replicator = AZStd::make_unique<EntityReplicator>(*m_entityReplicationManager, m_mockConnection.get(), NetEntityRole::Client, rootHandle);
  200. root.m_replicator->Initialize(rootHandle);
  201. root.m_entity->Activate();
  202. child.m_entity->Activate();
  203. }
  204. AZStd::unique_ptr<Multiplayer::MockSceneInterface> m_mockSceneInterface;
  205. AZStd::unique_ptr<Multiplayer::MockPhysicsDefaultWorldRequestsHandler> m_mockDefaultWorld;
  206. AZStd::unique_ptr<AZ::ComponentDescriptor> m_rigidBodyDescriptor;
  207. AZStd::unique_ptr<AZ::ComponentDescriptor> m_netRigidBodyDescriptor;
  208. AZStd::unique_ptr<PhysX::RigidBody> m_rigidBody;
  209. AZStd::unique_ptr<EntityInfo> m_root;
  210. AZStd::unique_ptr<EntityInfo> m_child;
  211. };
  212. TEST_F(NetworkRigidBodyTests, TestHandleSendApplyImpulse)
  213. {
  214. NetworkRigidBodyComponentController* controller =
  215. dynamic_cast < NetworkRigidBodyComponentController*>(m_root->m_entity->FindComponent<NetworkRigidBodyComponent>()->GetController());
  216. controller->HandleSendApplyImpulse(nullptr, AZ::Vector3::CreateOne(), AZ::Vector3::CreateOne());
  217. }
  218. TEST_F(NetworkRigidBodyTests, TestSyncRewind)
  219. {
  220. m_root->m_entity->FindComponent<NetBindComponent>()->NotifySyncRewindState();
  221. }
  222. }