StaticCompoundShapeTest.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <TestFramework.h>
  4. #include <Tests/Shapes/StaticCompoundShapeTest.h>
  5. #include <Jolt/Physics/Collision/Shape/StaticCompoundShape.h>
  6. #include <Jolt/Physics/Collision/Shape/SphereShape.h>
  7. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  8. #include <Jolt/Physics/Collision/Shape/CapsuleShape.h>
  9. #include <Jolt/Physics/Collision/Shape/TaperedCapsuleShape.h>
  10. #include <Jolt/Physics/Collision/Shape/CylinderShape.h>
  11. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  12. #include <Layers.h>
  13. JPH_IMPLEMENT_RTTI_VIRTUAL(StaticCompoundShapeTest)
  14. {
  15. JPH_ADD_BASE_CLASS(StaticCompoundShapeTest, Test)
  16. }
  17. void StaticCompoundShapeTest::Initialize()
  18. {
  19. // Floor
  20. CreateFloor();
  21. // Simple compound
  22. Ref<StaticCompoundShapeSettings> compound_shape1 = new StaticCompoundShapeSettings;
  23. compound_shape1->AddShape(Vec3::sZero(), Quat::sIdentity(), new CapsuleShape(5, 1));
  24. compound_shape1->AddShape(Vec3(0, -5, 0), Quat::sIdentity(), new SphereShape(2));
  25. compound_shape1->AddShape(Vec3(0, 5, 0), Quat::sIdentity(), new SphereShape(2));
  26. // Compound with sub compound and rotation
  27. Ref<StaticCompoundShapeSettings> sub_compound = new StaticCompoundShapeSettings;
  28. sub_compound->AddShape(Vec3(0, 1.5f, 0), Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI), new BoxShape(Vec3(1.5f, 0.25f, 0.2f)));
  29. sub_compound->AddShape(Vec3(1.5f, 0, 0), Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI), new CylinderShape(1.5f, 0.2f));
  30. sub_compound->AddShape(Vec3(0, 0, 1.5f), Quat::sRotation(Vec3::sAxisX(), 0.5f * JPH_PI), new TaperedCapsuleShapeSettings(1.5f, 0.25f, 0.2f));
  31. Ref<StaticCompoundShapeSettings> compound_shape2 = new StaticCompoundShapeSettings;
  32. compound_shape2->AddShape(Vec3(0, 0, 0), Quat::sRotation(Vec3::sAxisX(), -0.25f * JPH_PI) * Quat::sRotation(Vec3::sAxisZ(), 0.25f * JPH_PI), sub_compound);
  33. compound_shape2->AddShape(Vec3(0, -0.1f, 0), Quat::sRotation(Vec3::sAxisX(), 0.25f * JPH_PI) * Quat::sRotation(Vec3::sAxisZ(), -0.75f * JPH_PI), sub_compound);
  34. // Compound with large amount of sub shapes
  35. Ref<StaticCompoundShapeSettings> compound_shape3 = new StaticCompoundShapeSettings;
  36. for (int y = -2; y <= 2; ++y)
  37. for (int x = -2; x <= 2; ++x)
  38. for (int z = -2; z <= 2; ++z)
  39. compound_shape3->AddShape(Vec3(0.5f * x, 0.5f * y, 0.5f * z), Quat::sRotation(Vec3::sAxisX(), -0.25f * JPH_PI) * Quat::sRotation(Vec3::sAxisZ(), 0.25f * JPH_PI), new BoxShape(Vec3::sReplicate(0.5f)));
  40. Ref<StaticCompoundShapeSettings> shapes[] = { compound_shape1, compound_shape2, compound_shape3 };
  41. for (int i = 0; i < 10; ++i)
  42. for (int j = 0; j < 3; ++j)
  43. {
  44. Quat rotation;
  45. if ((i & 1) == 0)
  46. rotation = Quat::sRotation(Vec3::sAxisX(), 0.5f * JPH_PI);
  47. else
  48. rotation = Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI);
  49. Body &body = *mBodyInterface->CreateBody(BodyCreationSettings(shapes[j], Vec3(0, 10.0f + 4.0f * i, j * 20.0f), rotation, EMotionType::Dynamic, Layers::MOVING));
  50. mBodyInterface->AddBody(body.GetID(), EActivation::Activate);
  51. }
  52. }