OffsetCenterOfMassShapeTest.cpp 2.6 KB

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