PhysicsScene.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <Jolt.h>
  4. #include <Physics/PhysicsScene.h>
  5. #include <Physics/PhysicsSystem.h>
  6. #include <ObjectStream/TypeDeclarations.h>
  7. namespace JPH {
  8. JPH_IMPLEMENT_SERIALIZABLE_NON_VIRTUAL(PhysicsScene)
  9. {
  10. JPH_ADD_ATTRIBUTE(PhysicsScene, mBodies)
  11. }
  12. void PhysicsScene::AddBody(const BodyCreationSettings &inBody)
  13. {
  14. mBodies.push_back(inBody);
  15. }
  16. bool PhysicsScene::FixInvalidScales()
  17. {
  18. const Vec3 unit_scale = Vec3::sReplicate(1.0f);
  19. bool success = true;
  20. for (BodyCreationSettings &b : mBodies)
  21. {
  22. // Test if there is an invalid scale in the shape hierarchy
  23. const Shape *shape = b.GetShape();
  24. if (!shape->IsValidScale(unit_scale))
  25. {
  26. // Fix it up
  27. Shape::ShapeResult result = shape->ScaleShape(unit_scale);
  28. if (result.IsValid())
  29. b.SetShape(result.Get());
  30. else
  31. success = false;
  32. }
  33. }
  34. return success;
  35. }
  36. void PhysicsScene::CreateBodies(PhysicsSystem *inSystem) const
  37. {
  38. BodyInterface &bi = inSystem->GetBodyInterface();
  39. for (const BodyCreationSettings &b : mBodies)
  40. bi.CreateAndAddBody(b, EActivation::Activate);
  41. }
  42. void PhysicsScene::SaveBinaryState(StreamOut &inStream, bool inSaveShapes, bool inSaveGroupFilter) const
  43. {
  44. BodyCreationSettings::ShapeToIDMap shape_to_id;
  45. BodyCreationSettings::MaterialToIDMap material_to_id;
  46. BodyCreationSettings::GroupFilterToIDMap group_filter_to_id;
  47. // Save bodies
  48. inStream.Write((uint32)mBodies.size());
  49. for (const BodyCreationSettings &b : mBodies)
  50. b.SaveWithChildren(inStream, inSaveShapes? &shape_to_id : nullptr, inSaveShapes? &material_to_id : nullptr, inSaveGroupFilter? &group_filter_to_id : nullptr);
  51. }
  52. PhysicsScene::PhysicsSceneResult PhysicsScene::sRestoreFromBinaryState(StreamIn &inStream)
  53. {
  54. PhysicsSceneResult result;
  55. // Create scene
  56. Ref<PhysicsScene> scene = new PhysicsScene();
  57. BodyCreationSettings::IDToShapeMap id_to_shape;
  58. BodyCreationSettings::IDToMaterialMap id_to_material;
  59. BodyCreationSettings::IDToGroupFilterMap id_to_group_filter;
  60. // Reserve some memory to avoid frequent reallocations
  61. id_to_shape.reserve(1024);
  62. id_to_material.reserve(128);
  63. id_to_group_filter.reserve(128);
  64. // Read bodies
  65. uint32 len = 0;
  66. inStream.Read(len);
  67. scene->mBodies.resize(len);
  68. for (BodyCreationSettings &b : scene->mBodies)
  69. {
  70. // Read creation settings
  71. BodyCreationSettings::BCSResult bcs_result = BodyCreationSettings::sRestoreWithChildren(inStream, id_to_shape, id_to_material, id_to_group_filter);
  72. if (bcs_result.HasError())
  73. {
  74. result.SetError(bcs_result.GetError());
  75. return result;
  76. }
  77. b = bcs_result.Get();
  78. }
  79. result.Set(scene);
  80. return result;
  81. }
  82. } // JPH