2
0

StaticCompoundShapeTest.cpp 3.0 KB

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