CenterOfMassTest.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <TestFramework.h>
  4. #include <Tests/General/CenterOfMassTest.h>
  5. #include <Physics/Collision/Shape/StaticCompoundShape.h>
  6. #include <Physics/Collision/Shape/SphereShape.h>
  7. #include <Physics/Collision/Shape/CapsuleShape.h>
  8. #include <Physics/Collision/Shape/ConvexHullShape.h>
  9. #include <Physics/Body/BodyCreationSettings.h>
  10. #include <Layers.h>
  11. JPH_IMPLEMENT_RTTI_VIRTUAL(CenterOfMassTest)
  12. {
  13. JPH_ADD_BASE_CLASS(CenterOfMassTest, Test)
  14. }
  15. void CenterOfMassTest::Initialize()
  16. {
  17. // Floor
  18. CreateFloor();
  19. // Compound shape with center of mass offset
  20. Ref<StaticCompoundShapeSettings> compound_shape1 = new StaticCompoundShapeSettings;
  21. compound_shape1->AddShape(Vec3(10, 0, 0), Quat::sIdentity(), new SphereShape(2));
  22. Body &body1 = *mBodyInterface->CreateBody(BodyCreationSettings(compound_shape1, Vec3(0, 10.0f, 0), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING));
  23. mBodyInterface->AddBody(body1.GetID(), EActivation::Activate);
  24. // Create box with center of mass offset
  25. vector<Vec3> box;
  26. box.push_back(Vec3(10, 10, 10));
  27. box.push_back(Vec3(5, 10, 10));
  28. box.push_back(Vec3(10, 5, 10));
  29. box.push_back(Vec3(5, 5, 10));
  30. box.push_back(Vec3(10, 10, 5));
  31. box.push_back(Vec3(5, 10, 5));
  32. box.push_back(Vec3(10, 5, 5));
  33. box.push_back(Vec3(5, 5, 5));
  34. Body &body2 = *mBodyInterface->CreateBody(BodyCreationSettings(new ConvexHullShapeSettings(box), Vec3(0, 10.0f, 20.0f), Quat::sRotation(Vec3::sAxisX(), 0.25f * JPH_PI), EMotionType::Dynamic, Layers::MOVING));
  35. mBodyInterface->AddBody(body2.GetID(), EActivation::Activate);
  36. // Compound
  37. Ref<StaticCompoundShapeSettings> compound_shape2 = new StaticCompoundShapeSettings;
  38. Quat rotation = Quat::sRotation(Vec3::sAxisX(), 0.25f * JPH_PI);
  39. compound_shape2->AddShape(Vec3(10, 0, 0), rotation, new CapsuleShape(5, 1));
  40. compound_shape2->AddShape(rotation * Vec3(10, -5, 0), Quat::sIdentity(), new SphereShape(4));
  41. compound_shape2->AddShape(rotation * Vec3(10, 5, 0), Quat::sIdentity(), new SphereShape(2));
  42. Body &body3 = *mBodyInterface->CreateBody(BodyCreationSettings(compound_shape2, Vec3(0, 10.0f, 40.0f), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING));
  43. mBodyInterface->AddBody(body3.GetID(), EActivation::Activate);
  44. }