SimulationInterfaceTests.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 "TestFixture.h"
  9. #include <SimulationInterfaces/SimulationEntityManagerRequestBus.h>
  10. namespace UnitTest
  11. {
  12. TEST_F(SimulationInterfaceTestFixture, EmptyScene)
  13. {
  14. using namespace SimulationInterfaces;
  15. AZStd::vector<AZStd::string> entities;
  16. SimulationEntityManagerRequestBus::BroadcastResult(entities, &SimulationEntityManagerRequestBus::Events::GetEntities, EntityFilters());
  17. EXPECT_EQ(entities.size(), 0);
  18. }
  19. TEST_F(SimulationInterfaceTestFixture, AddSimulatedEntityThenRemove)
  20. {
  21. using namespace SimulationInterfaces;
  22. const AZ::EntityId entityId1 = CreateEntityWithStaticBodyComponent("Foo", AZ::Transform::CreateIdentity());
  23. const AZ::EntityId entityId2 = CreateEntityWithStaticBodyComponent("Bar", AZ::Transform::CreateIdentity());
  24. AZStd::vector<AZStd::string> entities;
  25. SimulationEntityManagerRequestBus::BroadcastResult(entities, &SimulationEntityManagerRequestBus::Events::GetEntities, EntityFilters());
  26. ASSERT_EQ(entities.size(), 2);
  27. DeleteEntity(entityId1);
  28. AZStd::vector<AZStd::string> entities2;
  29. SimulationEntityManagerRequestBus::BroadcastResult(entities2, &SimulationEntityManagerRequestBus::Events::GetEntities, EntityFilters());
  30. EXPECT_EQ(entities2.size(), 1);
  31. DeleteEntity(entityId2);
  32. AZStd::vector<AZStd::string> entities3;
  33. SimulationEntityManagerRequestBus::BroadcastResult(entities3, &SimulationEntityManagerRequestBus::Events::GetEntities, EntityFilters());
  34. EXPECT_EQ(entities3.size(), 0);
  35. }
  36. TEST_F(SimulationInterfaceTestFixture, AddEntitiesWithDupName)
  37. {
  38. using namespace SimulationInterfaces;
  39. const AZ::EntityId entityId1 = CreateEntityWithStaticBodyComponent("Bar1", AZ::Transform::CreateIdentity());
  40. const AZ::EntityId entityId2 = CreateEntityWithStaticBodyComponent("Bar1", AZ::Transform::CreateIdentity());
  41. AZStd::vector<AZStd::string> entities;
  42. SimulationEntityManagerRequestBus::BroadcastResult(entities, &SimulationEntityManagerRequestBus::Events::GetEntities, EntityFilters());
  43. EXPECT_EQ(entities.size(), 2);
  44. EXPECT_NE(entities[0], entities[1]);
  45. DeleteEntity(entityId1);
  46. DeleteEntity(entityId2);
  47. }
  48. TEST_F(SimulationInterfaceTestFixture, TestShapeFilter)
  49. {
  50. // This test is disabled since due to some issue outside to this gem, the rigid body is created without the collider shape
  51. // and the filter is not applied. This test will be enabled once the issue is resolved.
  52. GTEST_SKIP()<<"Need to fix the issue with the collider shape creation.";
  53. using namespace SimulationInterfaces;
  54. const AZ::EntityId entityId1 =
  55. CreateEntityWithStaticBodyComponent("Inside", AZ::Transform::CreateTranslation(AZ::Vector3(0.0f, 0.0f, 0.0f)));
  56. const AZ::EntityId entityId2 =
  57. CreateEntityWithStaticBodyComponent("Outside", AZ::Transform::CreateTranslation(AZ::Vector3(10.0f, 0.0f, 0.0f)));
  58. EntityFilters filter;
  59. filter.m_bounds_shape = AZStd::make_shared<Physics::SphereShapeConfiguration>(2.0f);
  60. AZStd::vector<AZStd::string> entities;
  61. SimulationEntityManagerRequestBus::BroadcastResult(entities, &SimulationEntityManagerRequestBus::Events::GetEntities, filter);
  62. auto* physicsSystem = AZ::Interface<AzPhysics::SystemInterface>::Get();
  63. physicsSystem->Simulate(1.0f / 60.0f);
  64. ASSERT_EQ(entities.size(), 1);
  65. EXPECT_EQ(entities.front(), "Inside");
  66. DeleteEntity(entityId1);
  67. DeleteEntity(entityId2);
  68. }
  69. TEST_F(SimulationInterfaceTestFixture, TestRegexFilter)
  70. {
  71. using namespace SimulationInterfaces;
  72. const AZ::EntityId entityId1 =
  73. CreateEntityWithStaticBodyComponent("WillMatch", AZ::Transform::CreateTranslation(AZ::Vector3(0.0f, 0.0f, 0.0f)));
  74. const AZ::EntityId entityId2 =
  75. CreateEntityWithStaticBodyComponent("WontMatch", AZ::Transform::CreateTranslation(AZ::Vector3(10.0f, 0.0f, 0.0f)));
  76. EntityFilters filter;
  77. filter.m_filter = "Will.*";
  78. AZStd::vector<AZStd::string> entities;
  79. SimulationEntityManagerRequestBus::BroadcastResult(entities, &SimulationEntityManagerRequestBus::Events::GetEntities, filter);
  80. ASSERT_EQ(entities.size(), 1);
  81. EXPECT_EQ(entities.front(), "WillMatch");
  82. DeleteEntity(entityId1);
  83. DeleteEntity(entityId2);
  84. }
  85. TEST_F(SimulationInterfaceTestFixture, TestRegexFilterInvalid)
  86. {
  87. // Invalid regex should not match any entity
  88. using namespace SimulationInterfaces;
  89. const AZ::EntityId entityId1 =
  90. CreateEntityWithStaticBodyComponent("WillMatch", AZ::Transform::CreateTranslation(AZ::Vector3(0.0f, 0.0f, 0.0f)));
  91. const AZ::EntityId entityId2 =
  92. CreateEntityWithStaticBodyComponent("WontMatch", AZ::Transform::CreateTranslation(AZ::Vector3(10.0f, 0.0f, 0.0f)));
  93. EntityFilters filter;
  94. filter.m_filter = "[a-z";
  95. AZStd::vector<AZStd::string> entities;
  96. SimulationEntityManagerRequestBus::BroadcastResult(entities, &SimulationEntityManagerRequestBus::Events::GetEntities, filter);
  97. EXPECT_EQ(entities.size(), 0);
  98. DeleteEntity(entityId1);
  99. DeleteEntity(entityId2);
  100. }
  101. TEST_F(SimulationInterfaceTestFixture, SmokeTestGetEntityState)
  102. {
  103. // Invalid regex should not match any entity
  104. using namespace SimulationInterfaces;
  105. const AZStd::string entityName = "DroppedBall";
  106. const AZ::EntityId entityId1 =
  107. CreateEntityWithStaticBodyComponent(entityName, AZ::Transform::CreateTranslation(AZ::Vector3(2.0f, 0.0f, 10.0f)));
  108. EntityFilters filter;
  109. EntityState stateBefore;
  110. SimulationEntityManagerRequestBus::BroadcastResult(stateBefore, &SimulationEntityManagerRequestBus::Events::GetEntityState, entityName);
  111. EXPECT_EQ(stateBefore.m_pose.GetTranslation(), AZ::Vector3(2.0f, 0.0f, 10.0f));
  112. for (int i = 0; i < 10; i++)
  113. {
  114. auto* physicsSystem = AZ::Interface<AzPhysics::SystemInterface>::Get();
  115. physicsSystem->Simulate(1.0f / 60.0f);
  116. }
  117. EntityState stateAfter;
  118. SimulationEntityManagerRequestBus::BroadcastResult(stateAfter, &SimulationEntityManagerRequestBus::Events::GetEntityState, entityName);
  119. AZ::Vector3 deltaPos = stateAfter.m_pose.GetTranslation() - stateBefore.m_pose.GetTranslation();
  120. // check if the entity moved
  121. EXPECT_GT(deltaPos.GetLength(), 0.0f);
  122. // check if entity has velocity
  123. EXPECT_GT(stateAfter.m_twist_linear.GetLength(), 0.0f);
  124. DeleteEntity(entityId1);
  125. }
  126. } // namespace UnitTest
  127. // required to support running integration tests with Qt and PhysX
  128. AZTEST_EXPORT int AZ_UNIT_TEST_HOOK_NAME(int argc, char** argv)
  129. {
  130. ::testing::InitGoogleMock(&argc, argv);
  131. AzQtComponents::PrepareQtPaths();
  132. QApplication app(argc, argv);
  133. AZ::Test::printUnusedParametersWarning(argc, argv);
  134. AZ::Test::addTestEnvironments({ new UnitTest::SimulationInterfaceTestEnvironment() });
  135. int result = RUN_ALL_TESTS();
  136. return result;
  137. }
  138. IMPLEMENT_TEST_EXECUTABLE_MAIN();