CenterOfMassTest.cpp 2.3 KB

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