PathConstraintPath.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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/StreamUtils.h>
  7. #ifdef JPH_DEBUG_RENDERER
  8. #include <Jolt/Renderer/DebugRenderer.h>
  9. #endif // JPH_DEBUG_RENDERER
  10. JPH_NAMESPACE_BEGIN
  11. JPH_IMPLEMENT_SERIALIZABLE_ABSTRACT(PathConstraintPath)
  12. {
  13. JPH_ADD_BASE_CLASS(PathConstraintPath, SerializableObject)
  14. }
  15. #ifdef JPH_DEBUG_RENDERER
  16. // Helper function to transform the results of GetPointOnPath to world space
  17. static inline void sTransformPathPoint(RMat44Arg inTransform, Vec3Arg inPosition, RVec3 &outPosition, Vec3 &ioNormal, Vec3 &ioBinormal)
  18. {
  19. outPosition = inTransform * inPosition;
  20. ioNormal = inTransform.Multiply3x3(ioNormal);
  21. ioBinormal = inTransform.Multiply3x3(ioBinormal);
  22. }
  23. // Helper function to draw a path segment
  24. static inline void sDrawPathSegment(DebugRenderer *inRenderer, RVec3Arg inPrevPosition, RVec3Arg inPosition, Vec3Arg inNormal, Vec3Arg inBinormal)
  25. {
  26. inRenderer->DrawLine(inPrevPosition, inPosition, Color::sWhite);
  27. inRenderer->DrawArrow(inPosition, inPosition + 0.1f * inNormal, Color::sRed, 0.02f);
  28. inRenderer->DrawArrow(inPosition, inPosition + 0.1f * inBinormal, Color::sGreen, 0.02f);
  29. }
  30. void PathConstraintPath::DrawPath(DebugRenderer *inRenderer, RMat44Arg inBaseTransform) const
  31. {
  32. // Calculate first point
  33. Vec3 lfirst_pos, first_tangent, first_normal, first_binormal;
  34. GetPointOnPath(0.0f, lfirst_pos, first_tangent, first_normal, first_binormal);
  35. RVec3 first_pos;
  36. sTransformPathPoint(inBaseTransform, lfirst_pos, first_pos, first_normal, first_binormal);
  37. float t_max = GetPathMaxFraction();
  38. // Draw the segments
  39. RVec3 prev_pos = first_pos;
  40. for (float t = 0.1f; t < t_max; t += 0.1f)
  41. {
  42. Vec3 lpos, tangent, normal, binormal;
  43. GetPointOnPath(t, lpos, tangent, normal, binormal);
  44. RVec3 pos;
  45. sTransformPathPoint(inBaseTransform, lpos, pos, normal, binormal);
  46. sDrawPathSegment(inRenderer, prev_pos, pos, normal, binormal);
  47. prev_pos = pos;
  48. }
  49. // Draw last point
  50. Vec3 lpos, tangent, normal, binormal;
  51. GetPointOnPath(t_max, lpos, tangent, normal, binormal);
  52. RVec3 pos;
  53. sTransformPathPoint(inBaseTransform, lpos, pos, normal, binormal);
  54. sDrawPathSegment(inRenderer, prev_pos, pos, normal, binormal);
  55. }
  56. #endif // JPH_DEBUG_RENDERER
  57. void PathConstraintPath::SaveBinaryState(StreamOut &inStream) const
  58. {
  59. inStream.Write(GetRTTI()->GetHash());
  60. inStream.Write(mIsLooping);
  61. }
  62. void PathConstraintPath::RestoreBinaryState(StreamIn &inStream)
  63. {
  64. // Type hash read by sRestoreFromBinaryState
  65. inStream.Read(mIsLooping);
  66. }
  67. PathConstraintPath::PathResult PathConstraintPath::sRestoreFromBinaryState(StreamIn &inStream)
  68. {
  69. return StreamUtils::RestoreObject<PathConstraintPath>(inStream, &PathConstraintPath::RestoreBinaryState);
  70. }
  71. JPH_NAMESPACE_END