ConstraintVsCOMChangeTest.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2023 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <TestFramework.h>
  5. #include <Tests/Constraints/ConstraintVsCOMChangeTest.h>
  6. #include <Jolt/Physics/Collision/Shape/MutableCompoundShape.h>
  7. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  8. #include <Jolt/Physics/Collision/GroupFilterTable.h>
  9. #include <Jolt/Physics/Constraints/HingeConstraint.h>
  10. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  11. #include <Layers.h>
  12. JPH_IMPLEMENT_RTTI_VIRTUAL(ConstraintVsCOMChangeTest)
  13. {
  14. JPH_ADD_BASE_CLASS(ConstraintVsCOMChangeTest, Test)
  15. }
  16. void ConstraintVsCOMChangeTest::Initialize()
  17. {
  18. constexpr int cChainLength = 15;
  19. constexpr float cMinAngle = DegreesToRadians(-10.0f);
  20. constexpr float cMaxAngle = DegreesToRadians(20.0f);
  21. // Floor
  22. CreateFloor();
  23. // Create box shape
  24. mBox = new BoxShape(Vec3::sReplicate(0.5f * cBoxSize));
  25. // Build a collision group filter that disables collision between adjacent bodies
  26. Ref<GroupFilterTable> group_filter = new GroupFilterTable(cChainLength);
  27. for (CollisionGroup::SubGroupID i = 0; i < cChainLength - 1; ++i)
  28. group_filter->DisableCollision(i, i + 1);
  29. // Create chain of bodies
  30. RVec3 position(0, 25, 0);
  31. for (int i = 0; i < cChainLength; ++i)
  32. {
  33. position += Vec3(cBoxSize, 0, 0);
  34. Quat rotation = Quat::sIdentity();
  35. // Create compound shape specific for this body
  36. MutableCompoundShapeSettings compound_shape;
  37. compound_shape.SetEmbedded();
  38. compound_shape.AddShape(Vec3::sZero(), Quat::sIdentity(), mBox);
  39. // Create body
  40. Body& segment = *mBodyInterface->CreateBody(BodyCreationSettings(&compound_shape, position, rotation, i == 0 ? EMotionType::Static : EMotionType::Dynamic, i == 0 ? Layers::NON_MOVING : Layers::MOVING));
  41. segment.SetCollisionGroup(CollisionGroup(group_filter, 0, CollisionGroup::SubGroupID(i)));
  42. mBodyInterface->AddBody(segment.GetID(), EActivation::Activate);
  43. if (i > 0)
  44. {
  45. // Create hinge
  46. HingeConstraintSettings settings;
  47. settings.mPoint1 = settings.mPoint2 = position + Vec3(-0.5f * cBoxSize, -0.5f * cBoxSize, 0);
  48. settings.mHingeAxis1 = settings.mHingeAxis2 = Vec3::sAxisZ();
  49. settings.mNormalAxis1 = settings.mNormalAxis2 = Vec3::sAxisX();
  50. settings.mLimitsMin = cMinAngle;
  51. settings.mLimitsMax = cMaxAngle;
  52. Constraint* constraint = settings.Create(*mBodies.back(), segment);
  53. mPhysicsSystem->AddConstraint(constraint);
  54. mConstraints.push_back(constraint);
  55. }
  56. mBodies.push_back(&segment);
  57. }
  58. }
  59. void ConstraintVsCOMChangeTest::PrePhysicsUpdate(const PreUpdateParams& inParams)
  60. {
  61. // Increment time
  62. mTime += inParams.mDeltaTime;
  63. UpdateShapes();
  64. }
  65. void ConstraintVsCOMChangeTest::SaveState(StateRecorder& inStream) const
  66. {
  67. inStream.Write(mTime);
  68. }
  69. void ConstraintVsCOMChangeTest::RestoreState(StateRecorder& inStream)
  70. {
  71. inStream.Read(mTime);
  72. UpdateShapes();
  73. }
  74. void ConstraintVsCOMChangeTest::UpdateShapes()
  75. {
  76. // Check if we need to change the configuration
  77. int num_shapes = int(mTime) & 1? 2 : 1;
  78. if (mNumShapes != num_shapes)
  79. {
  80. mNumShapes = num_shapes;
  81. // Change the COM of the bodies
  82. for (int i = 1; i < (int)mBodies.size(); i += 2)
  83. {
  84. Body *b = mBodies[i];
  85. MutableCompoundShape *s = static_cast<MutableCompoundShape *>(const_cast<Shape *>(b->GetShape()));
  86. // Remember the center of mass before the change
  87. Vec3 prev_com = s->GetCenterOfMass();
  88. // First remove all existing shapes
  89. for (int j = s->GetNumSubShapes() - 1; j >= 0; --j)
  90. s->RemoveShape(j);
  91. // Then create the desired number of shapes
  92. for (int j = 0; j < num_shapes; ++j)
  93. s->AddShape(Vec3(0, 0, (1.0f + cBoxSize) * j), Quat::sIdentity(), mBox);
  94. // Update the center of mass to account for the new box configuration
  95. s->AdjustCenterOfMass();
  96. // Notify the physics system that the shape has changed
  97. mBodyInterface->NotifyShapeChanged(b->GetID(), prev_com, true, EActivation::Activate);
  98. // Notify the constraints that the shape has changed (this could be done more efficient as we know which constraints are affected)
  99. Vec3 delta_com = s->GetCenterOfMass() - prev_com;
  100. for (Constraint *c : mConstraints)
  101. c->NotifyShapeChanged(b->GetID(), delta_com);
  102. }
  103. }
  104. }