ScaledConvexHullShapeTest.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <TestFramework.h>
  4. #include <Tests/ScaledShapes/ScaledConvexHullShapeTest.h>
  5. #include <Physics/Collision/Shape/ConvexHullShape.h>
  6. #include <Physics/Collision/Shape/ScaledShape.h>
  7. #include <Physics/Body/BodyCreationSettings.h>
  8. #include <Layers.h>
  9. JPH_IMPLEMENT_RTTI_VIRTUAL(ScaledConvexHullShapeTest)
  10. {
  11. JPH_ADD_BASE_CLASS(ScaledConvexHullShapeTest, Test)
  12. }
  13. void ScaledConvexHullShapeTest::Initialize()
  14. {
  15. // Floor
  16. CreateFloor();
  17. // Create tetrahedron
  18. vector<Vec3> tetrahedron;
  19. tetrahedron.push_back(Vec3::sZero());
  20. tetrahedron.push_back(Vec3(10, 0, 12.5f));
  21. tetrahedron.push_back(Vec3(15, 0, 2.5f));
  22. tetrahedron.push_back(Vec3(10, -5, 5));
  23. // Create vertices for box
  24. vector<Vec3> box;
  25. box.push_back(Vec3(1, 2, 3));
  26. box.push_back(Vec3(-1, 2, 3));
  27. box.push_back(Vec3(1, -2, 3));
  28. box.push_back(Vec3(-1, -2, 3));
  29. box.push_back(Vec3(1, 2, -3));
  30. box.push_back(Vec3(-1, 2, -3));
  31. box.push_back(Vec3(1, -2, -3));
  32. box.push_back(Vec3(-1, -2, -3));
  33. // Rotate and translate vertices
  34. Mat44 m = Mat44::sTranslation(Vec3(3.0f, -2.0f, 1.0f)) * Mat44::sRotationY(0.2f * JPH_PI) * Mat44::sRotationZ(0.1f * JPH_PI);
  35. for (Vec3 &v : box)
  36. v = m * v;
  37. // Create convex hulls
  38. RefConst<ShapeSettings> hull_shape[2] = { new ConvexHullShapeSettings(tetrahedron), new ConvexHullShapeSettings(box) };
  39. for (int i = 0; i < 2; ++i)
  40. {
  41. // Original shape
  42. Body &body1 = *mBodyInterface->CreateBody(BodyCreationSettings(hull_shape[i], Vec3(-40, 10, i * 20.0f), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING));
  43. mBodyInterface->AddBody(body1.GetID(), EActivation::Activate);
  44. // Uniformly scaled shape
  45. Body &body2 = *mBodyInterface->CreateBody(BodyCreationSettings(new ScaledShapeSettings(hull_shape[i], Vec3::sReplicate(0.25f)), Vec3(-20, 10, i * 20.0f), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING));
  46. mBodyInterface->AddBody(body2.GetID(), EActivation::Activate);
  47. // Non-uniform scaled shape
  48. Body &body3 = *mBodyInterface->CreateBody(BodyCreationSettings(new ScaledShapeSettings(hull_shape[i], Vec3(0.25f, 0.5f, 1.5f)), Vec3(0, 10, i * 20.0f), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING));
  49. mBodyInterface->AddBody(body3.GetID(), EActivation::Activate);
  50. // Flipped in 2 axis
  51. Body &body4 = *mBodyInterface->CreateBody(BodyCreationSettings(new ScaledShapeSettings(hull_shape[i], Vec3(-0.25f, 0.5f, -1.5f)), Vec3(20, 10, i * 20.0f), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING));
  52. mBodyInterface->AddBody(body4.GetID(), EActivation::Activate);
  53. // Inside out
  54. Body &body5 = *mBodyInterface->CreateBody(BodyCreationSettings(new ScaledShapeSettings(hull_shape[i], Vec3(-0.25f, 0.5f, 1.5f)), Vec3(40, 10, i * 20.0f), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING));
  55. mBodyInterface->AddBody(body5.GetID(), EActivation::Activate);
  56. }
  57. }