OffsetCenterOfMassShapeTest.cpp 2.7 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/Shapes/OffsetCenterOfMassShapeTest.h>
  6. #include <Jolt/Physics/Collision/Shape/SphereShape.h>
  7. #include <Jolt/Physics/Collision/Shape/OffsetCenterOfMassShape.h>
  8. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  9. #include <Layers.h>
  10. JPH_IMPLEMENT_RTTI_VIRTUAL(OffsetCenterOfMassShapeTest)
  11. {
  12. JPH_ADD_BASE_CLASS(OffsetCenterOfMassShapeTest, Test)
  13. }
  14. void OffsetCenterOfMassShapeTest::Initialize()
  15. {
  16. // Floor
  17. Body &floor = CreateFloor();
  18. floor.SetFriction(1.0f);
  19. Ref<ShapeSettings> sphere = new SphereShapeSettings(1.0f);
  20. Ref<OffsetCenterOfMassShapeSettings> left = new OffsetCenterOfMassShapeSettings(Vec3(-1, 0, 0), sphere);
  21. Ref<OffsetCenterOfMassShapeSettings> right = new OffsetCenterOfMassShapeSettings(Vec3(1, 0, 0), sphere);
  22. // Sphere with center of mass moved to the left side
  23. Body &body_left = *mBodyInterface->CreateBody(BodyCreationSettings(left, RVec3(-5, 5, 0), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING));
  24. body_left.SetFriction(1.0f);
  25. mBodyInterface->AddBody(body_left.GetID(), EActivation::Activate);
  26. // Sphere with center of mass centered
  27. Body &body_center = *mBodyInterface->CreateBody(BodyCreationSettings(sphere, RVec3(0, 5, 0), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING));
  28. body_center.SetFriction(1.0f);
  29. mBodyInterface->AddBody(body_center.GetID(), EActivation::Activate);
  30. // Sphere with center of mass moved to the right side
  31. Body &body_right = *mBodyInterface->CreateBody(BodyCreationSettings(right, RVec3(5, 5, 0), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING));
  32. body_right.SetFriction(1.0f);
  33. mBodyInterface->AddBody(body_right.GetID(), EActivation::Activate);
  34. // Create body and apply a large angular impulse so see that it spins around the COM
  35. BodyCreationSettings bcs(new OffsetCenterOfMassShapeSettings(Vec3(-3, 0, 0), new SphereShapeSettings(1.0f)), RVec3(-5, 5, 10), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  36. bcs.mGravityFactor = 0.0f;
  37. bcs.mLinearDamping = 0.0f;
  38. bcs.mAngularDamping = 0.0f;
  39. Body *body_rotating1 = mBodyInterface->CreateBody(bcs);
  40. mBodyInterface->AddBody(body_rotating1->GetID(), EActivation::Activate);
  41. body_rotating1->AddAngularImpulse(Vec3(0, 1.0e6f, 0));
  42. // Create the same body but this time apply a torque
  43. bcs.mPosition = RVec3(5, 5, 10);
  44. Body *body_rotating2 = mBodyInterface->CreateBody(bcs);
  45. mBodyInterface->AddBody(body_rotating2->GetID(), EActivation::Activate);
  46. body_rotating2->AddTorque(Vec3(0, 1.0e6f * 60.0f, 0)); // Assuming physics sim is at 60Hz here, otherwise the bodies won't rotate with the same speed
  47. }