3
0

ActorClothCollidersTest.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 <AZTestShared/Math/MathTestHelpers.h>
  9. #include <AzCore/Component/Entity.h>
  10. #include <AzFramework/Components/TransformComponent.h>
  11. #include <Components/ClothComponentMesh/ActorClothColliders.h>
  12. #include <UnitTestHelper.h>
  13. #include <ActorHelper.h>
  14. #include <Integration/Components/ActorComponent.h>
  15. namespace NvCloth
  16. {
  17. namespace Internal
  18. {
  19. extern const size_t NvClothMaxNumSphereColliders;
  20. extern const size_t NvClothMaxNumCapsuleColliders;
  21. }
  22. }
  23. namespace UnitTest
  24. {
  25. //! Fixture to setup entity with actor component.
  26. class NvClothActorClothColliders
  27. : public ::testing::Test
  28. {
  29. protected:
  30. // ::testing::Test overrides ...
  31. void SetUp() override;
  32. void TearDown() override;
  33. EMotionFX::Integration::ActorComponent* m_actorComponent = nullptr;
  34. private:
  35. AZStd::unique_ptr<AZ::Entity> m_entity;
  36. };
  37. void NvClothActorClothColliders::SetUp()
  38. {
  39. m_entity = AZStd::make_unique<AZ::Entity>();
  40. m_entity->CreateComponent<AzFramework::TransformComponent>();
  41. m_actorComponent = m_entity->CreateComponent<EMotionFX::Integration::ActorComponent>();
  42. m_entity->Init();
  43. m_entity->Activate();
  44. }
  45. void NvClothActorClothColliders::TearDown()
  46. {
  47. m_entity->Deactivate();
  48. m_actorComponent = nullptr;
  49. m_entity.reset();
  50. }
  51. TEST_F(NvClothActorClothColliders, ActorClothColliders_DefaultConstruct_ReturnsEmptyData)
  52. {
  53. AZ::EntityId entityId;
  54. NvCloth::ActorClothColliders actorClothColliders(entityId);
  55. EXPECT_TRUE(actorClothColliders.GetSphereColliders().empty());
  56. EXPECT_TRUE(actorClothColliders.GetSpheres().empty());
  57. EXPECT_TRUE(actorClothColliders.GetCapsuleColliders().empty());
  58. EXPECT_TRUE(actorClothColliders.GetCapsuleIndices().empty());
  59. }
  60. TEST_F(NvClothActorClothColliders, ActorClothColliders_CreateWithInvalidEntityId_ReturnsNull)
  61. {
  62. AZ::EntityId entityId;
  63. AZStd::unique_ptr<NvCloth::ActorClothColliders> actorClothColliders = NvCloth::ActorClothColliders::Create(entityId);
  64. EXPECT_TRUE(actorClothColliders.get() == nullptr);
  65. }
  66. TEST_F(NvClothActorClothColliders, ActorClothColliders_CreateWithActorWithNoClothColliders_ReturnsNull)
  67. {
  68. {
  69. auto actor = AZStd::make_unique<ActorHelper>("actor_test");
  70. actor->FinishSetup();
  71. m_actorComponent->SetActorAsset(CreateAssetFromActor(AZStd::move(actor)));
  72. }
  73. AZStd::unique_ptr<NvCloth::ActorClothColliders> actorClothColliders = NvCloth::ActorClothColliders::Create(m_actorComponent->GetEntityId());
  74. EXPECT_TRUE(actorClothColliders.get() == nullptr);
  75. }
  76. TEST_F(NvClothActorClothColliders, ActorClothColliders_CreateWithActorWithBoxClothCollider_ReturnsNull)
  77. {
  78. const char* const jointRootName = "joint_root";
  79. {
  80. const auto collider = CreateBoxCollider(jointRootName, AZ::Vector3(0.2f, 0.3f, 0.47f));
  81. auto actor = AZStd::make_unique<ActorHelper>("actor_test");
  82. actor->AddJoint(jointRootName);
  83. actor->AddClothCollider(collider);
  84. actor->FinishSetup();
  85. m_actorComponent->SetActorAsset(CreateAssetFromActor(AZStd::move(actor)));
  86. }
  87. // ActorClothColliders only supports spheres or capsules, other shapes will not be taken into account.
  88. // Since there is no colliders added then it'll return null.
  89. AZStd::unique_ptr<NvCloth::ActorClothColliders> actorClothColliders = NvCloth::ActorClothColliders::Create(m_actorComponent->GetEntityId());
  90. EXPECT_TRUE(actorClothColliders.get() == nullptr);
  91. }
  92. TEST_F(NvClothActorClothColliders, ActorClothColliders_CreateWithActorWithSphereClothCollider_ReturnsValidConstraints)
  93. {
  94. const char* const jointRootName = "joint_root";
  95. const float radius = 2.3f;
  96. const AZ::Transform colliderOffet = AZ::Transform::CreateFromQuaternionAndTranslation(AZ::Quaternion::CreateRotationX(AZ::DegToRad(65.0f)), AZ::Vector3(-0.5f, 3.0f, 6.0f));
  97. const AZ::Transform jointTransform = AZ::Transform::CreateTranslation(AZ::Vector3(2.0f, 53.0f, -65.0f));
  98. {
  99. const auto collider = CreateSphereCollider(jointRootName, radius, colliderOffet);
  100. auto actor = AZStd::make_unique<ActorHelper>("actor_test");
  101. actor->AddJoint(jointRootName, jointTransform);
  102. actor->AddClothCollider(collider);
  103. actor->FinishSetup();
  104. m_actorComponent->SetActorAsset(CreateAssetFromActor(AZStd::move(actor)));
  105. }
  106. AZStd::unique_ptr<NvCloth::ActorClothColliders> actorClothColliders = NvCloth::ActorClothColliders::Create(m_actorComponent->GetEntityId());
  107. EXPECT_TRUE(actorClothColliders.get() != nullptr);
  108. const AZStd::vector<NvCloth::SphereCollider>& sphereColliders = actorClothColliders->GetSphereColliders();
  109. const AZStd::vector<AZ::Vector4>& nativeSpheres = actorClothColliders->GetSpheres();
  110. const AZStd::vector<NvCloth::CapsuleCollider>& capsuleColliders = actorClothColliders->GetCapsuleColliders();
  111. const AZStd::vector<uint32_t>& nativeCapsuleIndices = actorClothColliders->GetCapsuleIndices();
  112. ASSERT_EQ(sphereColliders.size(), 1);
  113. ASSERT_EQ(nativeSpheres.size(), 1);
  114. EXPECT_TRUE(capsuleColliders.empty());
  115. EXPECT_TRUE(nativeCapsuleIndices.empty());
  116. EXPECT_NEAR(sphereColliders[0].m_radius, radius, Tolerance);
  117. EXPECT_EQ(sphereColliders[0].m_nvSphereIndex, 0);
  118. EXPECT_EQ(sphereColliders[0].m_jointIndex, 0);
  119. EXPECT_THAT(sphereColliders[0].m_offsetTransform, IsCloseTolerance(colliderOffet, Tolerance));
  120. EXPECT_THAT(sphereColliders[0].m_currentModelSpaceTransform, IsCloseTolerance(jointTransform * colliderOffet, Tolerance));
  121. EXPECT_THAT(nativeSpheres[0].GetAsVector3(), IsCloseTolerance((jointTransform * colliderOffet).GetTranslation(), Tolerance));
  122. EXPECT_NEAR(nativeSpheres[0].GetW(), radius, Tolerance);
  123. }
  124. TEST_F(NvClothActorClothColliders, ActorClothColliders_CreateWithActorWithCapsuleClothCollider_ReturnsValidConstraints)
  125. {
  126. const char* const jointRootName = "joint_root";
  127. const float height = 4.7f;
  128. const float radius = 1.2f;
  129. const AZ::Transform colliderOffet = AZ::Transform::CreateFromQuaternionAndTranslation(AZ::Quaternion::CreateRotationX(AZ::DegToRad(65.0f)), AZ::Vector3(-0.5f, 3.0f, 6.0f));
  130. const AZ::Transform jointTransform = AZ::Transform::CreateTranslation(AZ::Vector3(2.0f, 53.0f, -65.0f));
  131. {
  132. const auto collider = CreateCapsuleCollider(jointRootName, height, radius, colliderOffet);
  133. auto actor = AZStd::make_unique<ActorHelper>("actor_test");
  134. actor->AddJoint(jointRootName, jointTransform);
  135. actor->AddClothCollider(collider);
  136. actor->FinishSetup();
  137. m_actorComponent->SetActorAsset(CreateAssetFromActor(AZStd::move(actor)));
  138. }
  139. AZStd::unique_ptr<NvCloth::ActorClothColliders> actorClothColliders = NvCloth::ActorClothColliders::Create(m_actorComponent->GetEntityId());
  140. EXPECT_TRUE(actorClothColliders.get() != nullptr);
  141. const AZStd::vector<NvCloth::SphereCollider>& sphereColliders = actorClothColliders->GetSphereColliders();
  142. const AZStd::vector<AZ::Vector4>& nativeSpheres = actorClothColliders->GetSpheres();
  143. const AZStd::vector<NvCloth::CapsuleCollider>& capsuleColliders = actorClothColliders->GetCapsuleColliders();
  144. const AZStd::vector<uint32_t>& nativeCapsuleIndices = actorClothColliders->GetCapsuleIndices();
  145. EXPECT_TRUE(sphereColliders.empty());
  146. ASSERT_EQ(nativeSpheres.size(), 2); // Each capsule produces 2 spheres
  147. ASSERT_EQ(capsuleColliders.size(), 1);
  148. ASSERT_EQ(nativeCapsuleIndices.size(), 2); // Each capsule is 2 indices
  149. EXPECT_NEAR(capsuleColliders[0].m_height, height, Tolerance);
  150. EXPECT_NEAR(capsuleColliders[0].m_radius, radius, Tolerance);
  151. EXPECT_EQ(capsuleColliders[0].m_capsuleIndex, 0);
  152. EXPECT_EQ(capsuleColliders[0].m_sphereAIndex, 0);
  153. EXPECT_EQ(capsuleColliders[0].m_sphereBIndex, 1);
  154. EXPECT_EQ(capsuleColliders[0].m_jointIndex, 0);
  155. EXPECT_THAT(capsuleColliders[0].m_offsetTransform, IsCloseTolerance(colliderOffet, Tolerance));
  156. EXPECT_THAT(capsuleColliders[0].m_currentModelSpaceTransform, IsCloseTolerance(jointTransform * colliderOffet, Tolerance));
  157. EXPECT_THAT(nativeSpheres[0], IsCloseTolerance(AZ::Vector4(1.5f, 54.9577f, -58.514f, radius), Tolerance));
  158. EXPECT_THAT(nativeSpheres[1], IsCloseTolerance(AZ::Vector4(1.5f, 57.0423f, -59.486f, radius), Tolerance));
  159. EXPECT_NEAR(nativeSpheres[0].GetAsVector3().GetDistance(nativeSpheres[1].GetAsVector3()), height - 2.0f * radius, Tolerance);
  160. EXPECT_EQ(nativeCapsuleIndices[0], 0);
  161. EXPECT_EQ(nativeCapsuleIndices[1], 1);
  162. }
  163. TEST_F(NvClothActorClothColliders, ActorClothColliders_CreateWithActorWithSphereAndCapsuleClothColliders_ReturnsValidConstraints)
  164. {
  165. const char* const jointRootName = "joint_root";
  166. {
  167. const auto sphereCollider = CreateSphereCollider(jointRootName, 0.2f);
  168. const auto capsuleCollider = CreateCapsuleCollider(jointRootName, 2.0f, 0.75f);
  169. auto actor = AZStd::make_unique<ActorHelper>("actor_test");
  170. actor->AddJoint(jointRootName);
  171. actor->AddClothCollider(sphereCollider);
  172. actor->AddClothCollider(capsuleCollider);
  173. actor->AddClothCollider(sphereCollider);
  174. actor->AddClothCollider(sphereCollider);
  175. actor->AddClothCollider(capsuleCollider);
  176. actor->FinishSetup();
  177. m_actorComponent->SetActorAsset(CreateAssetFromActor(AZStd::move(actor)));
  178. }
  179. AZStd::unique_ptr<NvCloth::ActorClothColliders> actorClothColliders = NvCloth::ActorClothColliders::Create(m_actorComponent->GetEntityId());
  180. const AZStd::vector<NvCloth::SphereCollider>& sphereColliders = actorClothColliders->GetSphereColliders();
  181. const AZStd::vector<AZ::Vector4>& nativeSpheres = actorClothColliders->GetSpheres();
  182. const AZStd::vector<NvCloth::CapsuleCollider>& capsuleColliders = actorClothColliders->GetCapsuleColliders();
  183. const AZStd::vector<uint32_t>& nativeCapsuleIndices = actorClothColliders->GetCapsuleIndices();
  184. EXPECT_EQ(sphereColliders.size(), 3);
  185. EXPECT_EQ(nativeSpheres.size(), 3 + 2*2); // 3 spheres + 2 capsules (2 spheres per capsule)
  186. EXPECT_EQ(capsuleColliders.size(), 2);
  187. EXPECT_EQ(nativeCapsuleIndices.size(), 2*2); // 2 capsules (2 indices per capsule)
  188. }
  189. TEST_F(NvClothActorClothColliders, ActorClothColliders_CreateWithActorSurpassingMaxNumberOfSpheres_ConstructsUpToMaxNumberOfSpheres)
  190. {
  191. const char* const jointRootName = "joint_root";
  192. {
  193. const auto sphereCollider = CreateSphereCollider(jointRootName, 0.2f);
  194. auto actor = AZStd::make_unique<ActorHelper>("actor_test");
  195. actor->AddJoint(jointRootName);
  196. for (size_t i = 0; i < NvCloth::Internal::NvClothMaxNumSphereColliders * 2; ++i)
  197. {
  198. actor->AddClothCollider(sphereCollider);
  199. }
  200. actor->FinishSetup();
  201. m_actorComponent->SetActorAsset(CreateAssetFromActor(AZStd::move(actor)));
  202. }
  203. AZStd::unique_ptr<NvCloth::ActorClothColliders> actorClothColliders = NvCloth::ActorClothColliders::Create(m_actorComponent->GetEntityId());
  204. const AZStd::vector<NvCloth::SphereCollider>& sphereColliders = actorClothColliders->GetSphereColliders();
  205. const AZStd::vector<AZ::Vector4>& nativeSpheres = actorClothColliders->GetSpheres();
  206. EXPECT_EQ(sphereColliders.size(), NvCloth::Internal::NvClothMaxNumSphereColliders);
  207. EXPECT_EQ(nativeSpheres.size(), NvCloth::Internal::NvClothMaxNumSphereColliders);
  208. }
  209. TEST_F(NvClothActorClothColliders, ActorClothColliders_CreateWithActorSurpassingMaxNumberOfCapsules_ConstructsUpToMaxNumberOfCapsules)
  210. {
  211. // Since each capsule has its own unique two spheres, the maximum number of
  212. // spheres is reached by the time half of maximum number of capsules is reached.
  213. const size_t maxNumberOfCapsules = NvCloth::Internal::NvClothMaxNumCapsuleColliders / 2;
  214. const char* const jointRootName = "joint_root";
  215. {
  216. const auto capsuleCollider = CreateCapsuleCollider(jointRootName, 2.0f, 0.75f);
  217. auto actor = AZStd::make_unique<ActorHelper>("actor_test");
  218. actor->AddJoint(jointRootName);
  219. for (size_t i = 0; i < maxNumberOfCapsules * 2; ++i)
  220. {
  221. actor->AddClothCollider(capsuleCollider);
  222. }
  223. actor->FinishSetup();
  224. m_actorComponent->SetActorAsset(CreateAssetFromActor(AZStd::move(actor)));
  225. }
  226. AZStd::unique_ptr<NvCloth::ActorClothColliders> actorClothColliders = NvCloth::ActorClothColliders::Create(m_actorComponent->GetEntityId());
  227. const AZStd::vector<AZ::Vector4>& nativeSpheres = actorClothColliders->GetSpheres();
  228. const AZStd::vector<NvCloth::CapsuleCollider>& capsuleColliders = actorClothColliders->GetCapsuleColliders();
  229. const AZStd::vector<uint32_t>& nativeCapsuleIndices = actorClothColliders->GetCapsuleIndices();
  230. EXPECT_EQ(nativeSpheres.size(), maxNumberOfCapsules * 2);
  231. EXPECT_EQ(capsuleColliders.size(), maxNumberOfCapsules);
  232. EXPECT_EQ(nativeCapsuleIndices.size(), maxNumberOfCapsules * 2);
  233. }
  234. TEST_F(NvClothActorClothColliders, ActorClothColliders_CreateWithActorWithNoSpaceForAnotherCapsule_CapsuleIsNotAdded)
  235. {
  236. const char* const jointRootName = "joint_root";
  237. {
  238. const auto sphereCollider = CreateSphereCollider(jointRootName, 0.2f);
  239. const auto capsuleCollider = CreateCapsuleCollider(jointRootName, 2.0f, 0.75f);
  240. auto actor = AZStd::make_unique<ActorHelper>("actor_test");
  241. actor->AddJoint(jointRootName);
  242. for (size_t i = 0; i < NvCloth::Internal::NvClothMaxNumSphereColliders - 1; ++i)
  243. {
  244. actor->AddClothCollider(sphereCollider);
  245. }
  246. actor->AddClothCollider(capsuleCollider); // This last capsule will not fit because it cannot add 2 additional spheres
  247. actor->FinishSetup();
  248. m_actorComponent->SetActorAsset(CreateAssetFromActor(AZStd::move(actor)));
  249. }
  250. AZStd::unique_ptr<NvCloth::ActorClothColliders> actorClothColliders = NvCloth::ActorClothColliders::Create(m_actorComponent->GetEntityId());
  251. EXPECT_TRUE(actorClothColliders->GetCapsuleColliders().empty());
  252. EXPECT_TRUE(actorClothColliders->GetCapsuleIndices().empty());
  253. }
  254. TEST_F(NvClothActorClothColliders, ActorClothColliders_Update_ReturnsUpdatedConstraints)
  255. {
  256. const char* const jointRootName = "joint_root";
  257. const char* const jointChildName = "joint_child";
  258. const float height = 12.3f;
  259. const float radius = 2.3f;
  260. const AZ::Transform sphereColliderOffet = AZ::Transform::CreateFromQuaternionAndTranslation(AZ::Quaternion::CreateRotationX(AZ::DegToRad(65.0f)), AZ::Vector3(-0.5f, 3.0f, 6.0f));
  261. const AZ::Transform capsuleColliderOffet = AZ::Transform::CreateFromQuaternionAndTranslation(AZ::Quaternion::CreateRotationX(AZ::DegToRad(-5.0f)), AZ::Vector3(2.5f, 6.0f, -4.0f));
  262. const AZ::Transform jointRootTransform = AZ::Transform::CreateTranslation(AZ::Vector3(2.0f, 53.0f, -65.0f));
  263. const AZ::Transform jointChildTransform = AZ::Transform::CreateFromQuaternionAndTranslation(AZ::Quaternion::CreateRotationY(AZ::DegToRad(36.0f)), AZ::Vector3(3.0f, -2.3f, 16.0f));
  264. {
  265. const auto sphereCollider = CreateSphereCollider(jointRootName, radius, sphereColliderOffet);
  266. const auto capsuleCollider = CreateCapsuleCollider(jointChildName, height, radius, capsuleColliderOffet);
  267. auto actor = AZStd::make_unique<ActorHelper>("actor_test");
  268. actor->AddJoint(jointRootName, jointRootTransform);
  269. actor->AddJoint(jointChildName, jointChildTransform, jointRootName);
  270. actor->AddClothCollider(sphereCollider);
  271. actor->AddClothCollider(capsuleCollider);
  272. actor->FinishSetup();
  273. m_actorComponent->SetActorAsset(CreateAssetFromActor(AZStd::move(actor)));
  274. }
  275. AZStd::unique_ptr<NvCloth::ActorClothColliders> actorClothColliders = NvCloth::ActorClothColliders::Create(m_actorComponent->GetEntityId());
  276. // Update actor instance's joints transforms
  277. const AZ::Transform newJointRootTransform = AZ::Transform::CreateFromQuaternionAndTranslation(AZ::Quaternion::CreateRotationZ(AZ::DegToRad(-32.0f)), AZ::Vector3(2.5f, -6.0f, 0.2f));
  278. const AZ::Transform newJointChildTransform = AZ::Transform::CreateTranslation(AZ::Vector3(-2.0f, 3.0f, 0.0f));
  279. EMotionFX::Pose* currentPose = m_actorComponent->GetActorInstance()->GetTransformData()->GetCurrentPose();
  280. currentPose->SetLocalSpaceTransform(0, newJointRootTransform);
  281. currentPose->SetLocalSpaceTransform(1, newJointChildTransform);
  282. actorClothColliders->Update();
  283. const AZStd::vector<NvCloth::SphereCollider>& sphereColliders = actorClothColliders->GetSphereColliders();
  284. const AZStd::vector<AZ::Vector4>& nativeSpheres = actorClothColliders->GetSpheres();
  285. const AZStd::vector<NvCloth::CapsuleCollider>& capsuleColliders = actorClothColliders->GetCapsuleColliders();
  286. EXPECT_THAT(sphereColliders[0].m_offsetTransform, IsCloseTolerance(sphereColliderOffet, Tolerance));
  287. EXPECT_THAT(sphereColliders[0].m_currentModelSpaceTransform, IsCloseTolerance(newJointRootTransform * sphereColliderOffet, Tolerance));
  288. EXPECT_THAT(nativeSpheres[0].GetAsVector3(), IsCloseTolerance((newJointRootTransform * sphereColliderOffet).GetTranslation(), Tolerance));
  289. EXPECT_THAT(capsuleColliders[0].m_offsetTransform, IsCloseTolerance(capsuleColliderOffet, Tolerance));
  290. EXPECT_THAT(capsuleColliders[0].m_currentModelSpaceTransform, IsCloseTolerance(newJointRootTransform * newJointChildTransform * capsuleColliderOffet, Tolerance));
  291. EXPECT_THAT(nativeSpheres[1].GetAsVector3(), IsCloseTolerance(AZ::Vector3(7.87111f, 1.65204f, 0.0353498f), Tolerance));
  292. EXPECT_THAT(nativeSpheres[2].GetAsVector3(), IsCloseTolerance(AZ::Vector3(7.51548f, 1.08291f, -7.63535), Tolerance));
  293. }
  294. } // namespace UnitTest