MutableCompoundShapeTest.cpp 6.3 KB

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