2
0

MutableCompoundShapeTest.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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/MutableCompoundShapeTest.h>
  6. #include <Jolt/Physics/Collision/Shape/StaticCompoundShape.h>
  7. #include <Jolt/Physics/Collision/Shape/MutableCompoundShape.h>
  8. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  9. #include <Jolt/Physics/Collision/Shape/TaperedCapsuleShape.h>
  10. #include <Jolt/Physics/Collision/Shape/CylinderShape.h>
  11. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  12. #include <Jolt/Core/StreamWrapper.h>
  13. #include <Layers.h>
  14. JPH_IMPLEMENT_RTTI_VIRTUAL(MutableCompoundShapeTest)
  15. {
  16. JPH_ADD_BASE_CLASS(MutableCompoundShapeTest, Test)
  17. }
  18. void MutableCompoundShapeTest::Initialize()
  19. {
  20. // Floor (extra thick because we can randomly add sub shapes that then may stick out underneath the floor and cause objects to be pushed through)
  21. Body &floor = *mBodyInterface->CreateBody(BodyCreationSettings(new BoxShape(Vec3(100.0f, 10.0f, 100.0f), 0.0f), RVec3(0.0f, -10.0f, 0.0f), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING));
  22. mBodyInterface->AddBody(floor.GetID(), EActivation::DontActivate);
  23. // Compound with sub compound and rotation
  24. StaticCompoundShapeSettings sub_compound_settings;
  25. sub_compound_settings.AddShape(Vec3(0, 1.5f, 0), Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI), new BoxShape(Vec3(1.5f, 0.25f, 0.2f)));
  26. sub_compound_settings.AddShape(Vec3(1.5f, 0, 0), Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI), new CylinderShape(1.5f, 0.2f));
  27. sub_compound_settings.AddShape(Vec3(0, 0, 1.5f), Quat::sRotation(Vec3::sAxisX(), 0.5f * JPH_PI), new TaperedCapsuleShapeSettings(1.5f, 0.25f, 0.2f));
  28. mSubCompound = sub_compound_settings.Create().Get();
  29. for (int i = 0; i < 10; ++i)
  30. {
  31. // Create a mutable compound per body and fill it up with 2 shapes initially
  32. Ref<MutableCompoundShapeSettings> compound_shape = new MutableCompoundShapeSettings;
  33. compound_shape->AddShape(Vec3::sZero(), Quat::sRotation(Vec3::sAxisX(), -0.25f * JPH_PI) * Quat::sRotation(Vec3::sAxisZ(), 0.25f * JPH_PI), mSubCompound);
  34. compound_shape->AddShape(Vec3::sZero(), Quat::sRotation(Vec3::sAxisX(), 0.25f * JPH_PI) * Quat::sRotation(Vec3::sAxisZ(), -0.75f * JPH_PI), mSubCompound);
  35. // Create a body
  36. Body &body = *mBodyInterface->CreateBody(BodyCreationSettings(compound_shape, RVec3(0, 10.0f + 5.0f * i, 0), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING));
  37. mBodyInterface->AddBody(body.GetID(), EActivation::Activate);
  38. mBodyIDs.push_back(body.GetID());
  39. }
  40. }
  41. void MutableCompoundShapeTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
  42. {
  43. BodyInterface &no_lock = mPhysicsSystem->GetBodyInterfaceNoLock();
  44. uniform_real_distribution<float> roll_distribution(0, 1);
  45. for (BodyID id : mBodyIDs)
  46. {
  47. BodyLockWrite lock(mPhysicsSystem->GetBodyLockInterface(), id);
  48. if (lock.Succeeded())
  49. {
  50. Body &body = lock.GetBody();
  51. // Get the shape
  52. MutableCompoundShape *shape = static_cast<MutableCompoundShape *>(const_cast<Shape *>(body.GetShape()));
  53. // Remember center of mass from before changes
  54. Vec3 old_com = shape->GetCenterOfMass();
  55. // Consistently seeded random engine so that bodies move in a predictable way
  56. default_random_engine consistent_random;
  57. // Simulate an engine data structure with strided positions/rotations
  58. struct PositionRotation
  59. {
  60. Vec3 mPosition;
  61. Quat mRotation;
  62. };
  63. Array<PositionRotation> pos_rot;
  64. // Animate sub shapes
  65. uint count = shape->GetNumSubShapes();
  66. for (uint i = 0; i < count; ++i)
  67. {
  68. const CompoundShape::SubShape &sub_shape = shape->GetSubShape(i);
  69. pos_rot.push_back({ Vec3::sZero(), (Quat::sRotation(Vec3::sRandom(consistent_random), DegreesToRadians(10.0f) * inParams.mDeltaTime) * sub_shape.GetRotation()).Normalized() });
  70. }
  71. // Set the new rotations/orientations on the sub shapes
  72. shape->ModifyShapes(0, count, &pos_rot.front().mPosition, &pos_rot.front().mRotation, sizeof(PositionRotation), sizeof(PositionRotation));
  73. // Initialize frame dependent random number generator
  74. default_random_engine frame_random(mFrameNumber++);
  75. // Roll the dice
  76. float roll = roll_distribution(frame_random);
  77. if (roll < 0.001f && count > 1)
  78. {
  79. // Remove a random shape
  80. uniform_int_distribution<uint> index_distribution(0, count - 1);
  81. shape->RemoveShape(index_distribution(frame_random));
  82. }
  83. else if (roll < 0.002f && count < 10)
  84. {
  85. // Add a shape in a random rotation
  86. shape->AddShape(Vec3::sZero(), Quat::sRandom(frame_random), mSubCompound);
  87. }
  88. // Ensure that the center of mass is updated
  89. shape->AdjustCenterOfMass();
  90. // Since we're already locking the body, we don't need to lock it again
  91. // We always update the mass properties of the shape because we're reorienting them every frame
  92. no_lock.NotifyShapeChanged(id, old_com, true, EActivation::Activate);
  93. }
  94. }
  95. }
  96. void MutableCompoundShapeTest::SaveState(StateRecorder &inStream) const
  97. {
  98. inStream.Write(mFrameNumber);
  99. for (BodyID id : mBodyIDs)
  100. {
  101. BodyLockRead lock(mPhysicsSystem->GetBodyLockInterface(), id);
  102. if (lock.Succeeded())
  103. {
  104. const Body &body = lock.GetBody();
  105. // Write the shape as a binary string
  106. stringstream data;
  107. StreamOutWrapper stream_out(data);
  108. body.GetShape()->SaveBinaryState(stream_out);
  109. inStream.Write(data.str());
  110. }
  111. }
  112. }
  113. void MutableCompoundShapeTest::RestoreState(StateRecorder &inStream)
  114. {
  115. inStream.Read(mFrameNumber);
  116. for (BodyID id : mBodyIDs)
  117. {
  118. BodyLockWrite lock(mPhysicsSystem->GetBodyLockInterface(), id);
  119. if (lock.Succeeded())
  120. {
  121. Body &body = lock.GetBody();
  122. // Read the shape as a binary string
  123. string str;
  124. if (inStream.IsValidating())
  125. {
  126. stringstream data;
  127. StreamOutWrapper stream_out(data);
  128. body.GetShape()->SaveBinaryState(stream_out);
  129. str = data.str();
  130. }
  131. inStream.Read(str);
  132. // Deserialize the shape
  133. stringstream data(str);
  134. StreamInWrapper stream_in(data);
  135. Shape::ShapeResult result = Shape::sRestoreFromBinaryState(stream_in);
  136. MutableCompoundShape *shape = static_cast<MutableCompoundShape *>(result.Get().GetPtr());
  137. // Restore the pointers to the sub compound
  138. ShapeList sub_shapes(shape->GetNumSubShapes(), mSubCompound);
  139. shape->RestoreSubShapeState(sub_shapes.data(), (uint)sub_shapes.size());
  140. // Update the shape (we're under lock protection, so use the no lock interface)
  141. mPhysicsSystem->GetBodyInterfaceNoLock().SetShape(id, shape, false, EActivation::DontActivate);
  142. }
  143. }
  144. }