StaticRigidBodyComponentTests.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 <AzToolsFramework/UnitTest/AzToolsFrameworkTestHelpers.h>
  9. #include <LmbrCentral/Shape/BoxShapeComponentBus.h>
  10. #include <PhysX/PhysXLocks.h>
  11. #include <Source/EditorColliderComponent.h>
  12. #include <Source/EditorShapeColliderComponent.h>
  13. #include <Source/EditorRigidBodyComponent.h>
  14. #include <Source/EditorStaticRigidBodyComponent.h>
  15. #include <Source/StaticRigidBodyComponent.h>
  16. #include <Tests/EditorTestUtilities.h>
  17. #include <Tests/PhysXTestCommon.h>
  18. namespace PhysXEditorTests
  19. {
  20. int GetEditorStaticRigidBodyCount()
  21. {
  22. AzPhysics::SceneHandle sceneHandle;
  23. Physics::EditorWorldBus::BroadcastResult(sceneHandle, &Physics::EditorWorldRequests::GetEditorSceneHandle);
  24. physx::PxScene* pxScene = nullptr;
  25. if (auto* physicsSystem = AZ::Interface<AzPhysics::SystemInterface>::Get())
  26. {
  27. if (AzPhysics::Scene* scene = physicsSystem->GetScene(sceneHandle))
  28. {
  29. pxScene = static_cast<physx::PxScene*>(scene->GetNativePointer());
  30. }
  31. }
  32. PHYSX_SCENE_READ_LOCK(pxScene);
  33. return pxScene->getNbActors(physx::PxActorTypeFlag::eRIGID_STATIC);
  34. }
  35. void AddEditorBoxShapeComponent(EntityPtr& editorEntity)
  36. {
  37. editorEntity->CreateComponent(LmbrCentral::EditorBoxShapeComponentTypeId);
  38. // Set some dimensions to box component so we have shapes for physics collider
  39. const AZ::Vector3 boxDimensions(2.0f, 3.0f, 4.0f);
  40. LmbrCentral::BoxShapeComponentRequestsBus::Event(editorEntity->GetId(),
  41. &LmbrCentral::BoxShapeComponentRequests::SetBoxDimensions,
  42. boxDimensions);
  43. }
  44. TEST_F(PhysXEditorFixture, StaticRigidBodyComponent_NoRigidBody_NoRuntimeStaticRigidBodyComponent)
  45. {
  46. // Create editor entity
  47. EntityPtr editorEntity = CreateInactiveEditorEntity("Entity");
  48. editorEntity->CreateComponent<PhysX::EditorShapeColliderComponent>();
  49. AddEditorBoxShapeComponent(editorEntity);
  50. // Create game entity and verify StaticRigidBodyComponent was NOT created
  51. EntityPtr gameEntity = CreateActiveGameEntityFromEditorEntity(editorEntity.get());
  52. const auto* staticRigidBody = gameEntity->FindComponent<PhysX::StaticRigidBodyComponent>();
  53. EXPECT_TRUE(staticRigidBody == nullptr);
  54. }
  55. TEST_F(PhysXEditorFixture, StaticRigidBodyComponent_StaticRigidBody_RuntimeStaticRigidBodyComponentCreated)
  56. {
  57. // Create editor entity
  58. EntityPtr editorEntity = CreateInactiveEditorEntity("Entity");
  59. editorEntity->CreateComponent<PhysX::EditorShapeColliderComponent>();
  60. AddEditorBoxShapeComponent(editorEntity);
  61. // Add static rigid body component
  62. editorEntity->CreateComponent<PhysX::EditorStaticRigidBodyComponent>();
  63. // Create game entity and verify StaticRigidBodyComponent was created
  64. EntityPtr gameEntity = CreateActiveGameEntityFromEditorEntity(editorEntity.get());
  65. const auto* staticRigidBody = gameEntity->FindComponent<PhysX::StaticRigidBodyComponent>();
  66. EXPECT_TRUE(staticRigidBody != nullptr);
  67. }
  68. TEST_F(PhysXEditorFixture, StaticRigidBodyComponent_DynamicRigidBody_NoRuntimeStaticRigidBodyComponent)
  69. {
  70. // Create editor entity
  71. EntityPtr editorEntity = CreateInactiveEditorEntity("Entity");
  72. editorEntity->CreateComponent<PhysX::EditorShapeColliderComponent>();
  73. AddEditorBoxShapeComponent(editorEntity);
  74. // Add dynamic rigid body component
  75. editorEntity->CreateComponent<PhysX::EditorRigidBodyComponent>();
  76. // Create game entity and verify StaticRigidBodyComponent was NOT created
  77. EntityPtr gameEntity = CreateActiveGameEntityFromEditorEntity(editorEntity.get());
  78. const auto* staticRigidBody = gameEntity->FindComponent<PhysX::StaticRigidBodyComponent>();
  79. EXPECT_TRUE(staticRigidBody == nullptr);
  80. // Verify RigidBodyComponent was created
  81. const auto* rigidBody = gameEntity->FindComponent<PhysX::RigidBodyComponent>();
  82. EXPECT_TRUE(rigidBody != nullptr);
  83. }
  84. TEST_F(PhysXEditorFixture, StaticRigidBodyComponent_MultipleColliderComponents_SingleRuntimeStaticRigidBodyComponent)
  85. {
  86. // Create editor entity
  87. EntityPtr editorEntity = CreateInactiveEditorEntity("Entity");
  88. AddEditorBoxShapeComponent(editorEntity);
  89. // Add two EditorColliderComponent components to the entity
  90. editorEntity->CreateComponent<PhysX::EditorColliderComponent>();
  91. editorEntity->CreateComponent<PhysX::EditorColliderComponent>();
  92. // Add static rigid body component
  93. editorEntity->CreateComponent<PhysX::EditorStaticRigidBodyComponent>();
  94. // Create game entity and verify only one StaticRigidBodyComponent was created
  95. EntityPtr gameEntity = CreateActiveGameEntityFromEditorEntity(editorEntity.get());
  96. AZ::Entity::ComponentArrayType staticRigidBodyComponents =
  97. gameEntity->FindComponents(AZ::AzTypeInfo<PhysX::StaticRigidBodyComponent>::Uuid());
  98. EXPECT_EQ(staticRigidBodyComponents.size(), 1);
  99. }
  100. TEST_F(PhysXEditorFixture, StaticRigidBodyComponent_EditorColliderAndNoRigidBodyComponent_EntityIsInvalid)
  101. {
  102. // Create editor entity
  103. EntityPtr editorEntity = CreateInactiveEditorEntity("Entity");
  104. editorEntity->CreateComponent<PhysX::EditorColliderComponent>();
  105. AddEditorBoxShapeComponent(editorEntity);
  106. // the entity should not be in a valid state because the shape collider component requires a rigid body component
  107. AZ::Entity::DependencySortOutcome sortOutcome = editorEntity->EvaluateDependenciesGetDetails();
  108. EXPECT_FALSE(sortOutcome.IsSuccess());
  109. EXPECT_TRUE(sortOutcome.GetError().m_code == AZ::Entity::DependencySortResult::MissingRequiredService);
  110. }
  111. TEST_F(PhysXEditorFixture, StaticRigidBodyComponent_EditorColliderAndStaticRigidBodyComponent_EditorStaticRigidBodyCreated)
  112. {
  113. // Get current number of static rigid body actors in editor world
  114. const int originalStaticRigidBodyCount = GetEditorStaticRigidBodyCount();
  115. // Create editor entity
  116. EntityPtr editorEntity = CreateInactiveEditorEntity("Entity");
  117. editorEntity->CreateComponent<PhysX::EditorShapeColliderComponent>();
  118. AddEditorBoxShapeComponent(editorEntity);
  119. // Add static rigid body component
  120. editorEntity->CreateComponent<PhysX::EditorStaticRigidBodyComponent>();
  121. editorEntity->Activate();
  122. // Verify number of static rigid body actors increased by 1
  123. EXPECT_EQ(GetEditorStaticRigidBodyCount(), originalStaticRigidBodyCount + 1);
  124. }
  125. TEST_F(PhysXEditorFixture, StaticRigidBodyComponent_EditorColliderAndDynamicRigidBodyComponent_NoEditorStaticRigidBodyCreated)
  126. {
  127. // Get current number of static rigid body actors in editor world
  128. const int originalStaticRigidBodyCount = GetEditorStaticRigidBodyCount();
  129. // Create editor entity
  130. EntityPtr editorEntity = CreateInactiveEditorEntity("Entity");
  131. editorEntity->CreateComponent<PhysX::EditorShapeColliderComponent>();
  132. AddEditorBoxShapeComponent(editorEntity);
  133. // Add dynamic rigid body component
  134. editorEntity->CreateComponent<PhysX::EditorRigidBodyComponent>();
  135. editorEntity->Activate();
  136. // Verify number of static rigid body actors has not changed
  137. EXPECT_EQ(GetEditorStaticRigidBodyCount(), originalStaticRigidBodyCount);
  138. }
  139. }