PathConstraintPath.cpp 3.5 KB

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