PathConstraintPath.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <Jolt/Jolt.h>
  4. #include <Jolt/Physics/Constraints/PathConstraintPath.h>
  5. #include <Jolt/Core/StreamIn.h>
  6. #include <Jolt/Core/StreamOut.h>
  7. #include <Jolt/Core/Factory.h>
  8. #ifdef JPH_DEBUG_RENDERER
  9. #include <Jolt/Renderer/DebugRenderer.h>
  10. #endif // JPH_DEBUG_RENDERER
  11. JPH_NAMESPACE_BEGIN
  12. JPH_IMPLEMENT_SERIALIZABLE_ABSTRACT(PathConstraintPath)
  13. {
  14. JPH_ADD_BASE_CLASS(PathConstraintPath, SerializableObject)
  15. }
  16. #ifdef JPH_DEBUG_RENDERER
  17. // Helper function to transform the results of GetPointOnPath to world space
  18. static inline void sTransformPathPoint(Mat44Arg inTransform, Vec3 &ioPosition, Vec3 &ioNormal, Vec3 &ioBinormal)
  19. {
  20. ioPosition = inTransform * ioPosition;
  21. ioNormal = inTransform.Multiply3x3(ioNormal);
  22. ioBinormal = inTransform.Multiply3x3(ioBinormal);
  23. }
  24. // Helper function to draw a path segment
  25. static inline void sDrawPathSegment(DebugRenderer *inRenderer, Vec3Arg inPrevPosition, Vec3Arg inPosition, Vec3Arg inNormal, Vec3Arg inBinormal)
  26. {
  27. inRenderer->DrawLine(inPrevPosition, inPosition, Color::sWhite);
  28. inRenderer->DrawArrow(inPosition, inPosition + 0.1f * inNormal, Color::sRed, 0.02f);
  29. inRenderer->DrawArrow(inPosition, inPosition + 0.1f * inBinormal, Color::sGreen, 0.02f);
  30. }
  31. void PathConstraintPath::DrawPath(DebugRenderer *inRenderer, Mat44Arg inBaseTransform) const
  32. {
  33. // Calculate first point
  34. Vec3 first_pos, first_tangent, first_normal, first_binormal;
  35. GetPointOnPath(0.0f, first_pos, first_tangent, first_normal, first_binormal);
  36. sTransformPathPoint(inBaseTransform, first_pos, first_normal, first_binormal);
  37. float t_max = GetPathMaxFraction();
  38. // Draw the segments
  39. Vec3 prev_pos = first_pos;
  40. for (float t = 0.1f; t < t_max; t += 0.1f)
  41. {
  42. Vec3 pos, tangent, normal, binormal;
  43. GetPointOnPath(t, pos, tangent, normal, binormal);
  44. sTransformPathPoint(inBaseTransform, pos, normal, binormal);
  45. sDrawPathSegment(inRenderer, prev_pos, pos, normal, binormal);
  46. prev_pos = pos;
  47. }
  48. // Draw last point
  49. Vec3 pos, tangent, normal, binormal;
  50. GetPointOnPath(t_max, pos, tangent, normal, binormal);
  51. sTransformPathPoint(inBaseTransform, pos, normal, binormal);
  52. sDrawPathSegment(inRenderer, prev_pos, pos, normal, binormal);
  53. }
  54. #endif // JPH_DEBUG_RENDERER
  55. void PathConstraintPath::SaveBinaryState(StreamOut &inStream) const
  56. {
  57. inStream.Write(GetRTTI()->GetHash());
  58. inStream.Write(mIsLooping);
  59. }
  60. void PathConstraintPath::RestoreBinaryState(StreamIn &inStream)
  61. {
  62. // Type hash read by sRestoreFromBinaryState
  63. inStream.Read(mIsLooping);
  64. }
  65. PathConstraintPath::PathResult PathConstraintPath::sRestoreFromBinaryState(StreamIn &inStream)
  66. {
  67. PathResult result;
  68. // Read the type of the constraint
  69. uint32 hash;
  70. inStream.Read(hash);
  71. if (inStream.IsEOF() || inStream.IsFailed())
  72. {
  73. result.SetError("Failed to read type id");
  74. return result;
  75. }
  76. // Get the RTTI for the shape
  77. const RTTI *rtti = Factory::sInstance->Find(hash);
  78. if (rtti == nullptr)
  79. {
  80. result.SetError("Failed to resolve type. Type not registered in factory?");
  81. return result;
  82. }
  83. // Construct and read the data of the shape
  84. Ref<PathConstraintPath> path = reinterpret_cast<PathConstraintPath *>(rtti->CreateObject());
  85. path->RestoreBinaryState(inStream);
  86. if (inStream.IsEOF() || inStream.IsFailed())
  87. {
  88. result.SetError("Failed to restore constraint");
  89. return result;
  90. }
  91. result.Set(path);
  92. return result;
  93. }
  94. JPH_NAMESPACE_END