ScaledOffsetCenterOfMassShapeTest.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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/ScaledShapes/ScaledOffsetCenterOfMassShapeTest.h>
  6. #include <Jolt/Physics/Collision/Shape/CylinderShape.h>
  7. #include <Jolt/Physics/Collision/Shape/OffsetCenterOfMassShape.h>
  8. #include <Jolt/Physics/Collision/Shape/ScaledShape.h>
  9. #include <Jolt/Physics/Collision/Shape/SphereShape.h>
  10. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  11. #include <Layers.h>
  12. JPH_IMPLEMENT_RTTI_VIRTUAL(ScaledOffsetCenterOfMassShapeTest)
  13. {
  14. JPH_ADD_BASE_CLASS(ScaledOffsetCenterOfMassShapeTest, Test)
  15. }
  16. void ScaledOffsetCenterOfMassShapeTest::Initialize()
  17. {
  18. // Floor
  19. Body &floor = CreateFloor();
  20. floor.SetFriction(1.0f);
  21. Ref<ShapeSettings> cylinder = new CylinderShapeSettings(1.0f, 0.1f);
  22. Ref<OffsetCenterOfMassShapeSettings> top = new OffsetCenterOfMassShapeSettings(Vec3(0, 1, 0), cylinder);
  23. Ref<OffsetCenterOfMassShapeSettings> bottom = new OffsetCenterOfMassShapeSettings(Vec3(0, -1, 0), cylinder);
  24. // Initial body rotation
  25. Quat rotation = Quat::sRotation(Vec3::sAxisZ(), 0.4f * JPH_PI);
  26. // Cylinder with center of mass moved to the top side
  27. Body &body_top = *mBodyInterface->CreateBody(BodyCreationSettings(new ScaledShapeSettings(top, Vec3(2.0f, 1.0f, 2.0f)), RVec3(-5, 5, 0), rotation, EMotionType::Dynamic, Layers::MOVING));
  28. body_top.SetFriction(1.0f);
  29. mBodyInterface->AddBody(body_top.GetID(), EActivation::Activate);
  30. // Cylinder with center of mass centered
  31. Body &body_center = *mBodyInterface->CreateBody(BodyCreationSettings(new ScaledShapeSettings(cylinder, Vec3(2.0f, 1.0f, 2.0f)), RVec3(0, 5, 0), rotation, EMotionType::Dynamic, Layers::MOVING));
  32. body_center.SetFriction(1.0f);
  33. mBodyInterface->AddBody(body_center.GetID(), EActivation::Activate);
  34. // Cylinder with center of mass moved to the bottom side
  35. Body &body_bottom = *mBodyInterface->CreateBody(BodyCreationSettings(new ScaledShapeSettings(bottom, Vec3(2.0f, 1.0f, 2.0f)), RVec3(5, 5, 0), rotation, EMotionType::Dynamic, Layers::MOVING));
  36. body_bottom.SetFriction(1.0f);
  37. mBodyInterface->AddBody(body_bottom.GetID(), EActivation::Activate);
  38. // Shape that is scaled before the offset center of mass offset is applied
  39. ShapeRefC pre_scaled = OffsetCenterOfMassShapeSettings(Vec3(0, 0, 5.0f), new ScaledShape(new SphereShape(1.0f), JPH::Vec3::sReplicate(2.0f))).Create().Get();
  40. Body &body_pre_scaled = *mBodyInterface->CreateBody(BodyCreationSettings(pre_scaled, RVec3(0, 5, -15), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING));
  41. mBodyInterface->AddBody(body_pre_scaled.GetID(), EActivation::Activate);
  42. // Shape that is scaled after the offset center of mass offset is applied
  43. ShapeRefC post_scaled = new ScaledShape(OffsetCenterOfMassShapeSettings(Vec3(0, 0, 5.0f), new SphereShape(1.0f)).Create().Get(), JPH::Vec3::sReplicate(2.0f));
  44. Body &body_post_scaled = *mBodyInterface->CreateBody(BodyCreationSettings(post_scaled, RVec3(5, 5, -15), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING));
  45. mBodyInterface->AddBody(body_post_scaled.GetID(), EActivation::Activate);
  46. }