PathConstraintPath.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Jolt/Core/Reference.h>
  5. #include <Jolt/Core/Result.h>
  6. #include <Jolt/ObjectStream/SerializableObject.h>
  7. JPH_NAMESPACE_BEGIN
  8. class StreamIn;
  9. class StreamOut;
  10. #ifdef JPH_DEBUG_RENDERER
  11. class DebugRenderer;
  12. #endif // JPH_DEBUG_RENDERER
  13. /// The path for a path constraint. It allows attaching two bodies to each other while giving the second body the freedom to move along a path relative to the first.
  14. class PathConstraintPath : public SerializableObject, public RefTarget<PathConstraintPath>
  15. {
  16. public:
  17. JPH_DECLARE_SERIALIZABLE_ABSTRACT(PathConstraintPath)
  18. using PathResult = Result<Ref<PathConstraintPath>>;
  19. /// Virtual destructor to ensure that derived types get their destructors called
  20. virtual ~PathConstraintPath() override = default;
  21. /// Gets the max fraction along the path. I.e. sort of the length of the path.
  22. virtual float GetPathMaxFraction() const = 0;
  23. /// Get the globally closest point on the curve (Could be slow!)
  24. /// @param inPosition Position to find closest point for
  25. /// @return Fraction of closest point along the path
  26. virtual float GetClosestPoint(Vec3Arg inPosition) const = 0;
  27. /// Given the fraction along the path, get the point, tangent and normal.
  28. /// @param inFraction Fraction along the path [0, GetPathMaxFraction()].
  29. /// @param outPathPosition Returns the closest position to inSearchPosition on the path.
  30. /// @param outPathTangent Returns the tangent to the path at outPathPosition (the vector that follows the direction of the path)
  31. /// @param outPathNormal Return the normal to the path at outPathPosition (a vector that's perpendicular to outPathTangent)
  32. /// @param outPathBinormal Returns the binormal to the path at outPathPosition (a vector so that normal cross tangent = binormal)
  33. virtual void GetPointOnPath(float inFraction, Vec3 &outPathPosition, Vec3 &outPathTangent, Vec3 &outPathNormal, Vec3 &outPathBinormal) const = 0;
  34. /// If the path is looping or not. If a path is looping, the first and last point are automatically connected to eachother. They should not be the same points.
  35. void SetIsLooping(bool inIsLooping) { mIsLooping = inIsLooping; }
  36. bool IsLooping() const { return mIsLooping; }
  37. #ifdef JPH_DEBUG_RENDERER
  38. /// Draw the path relative to inBaseTransform. Used for debug purposes.
  39. void DrawPath(DebugRenderer *inRenderer, Mat44Arg inBaseTransform) const;
  40. #endif // JPH_DEBUG_RENDERER
  41. /// Saves the contents of the path in binary form to inStream.
  42. virtual void SaveBinaryState(StreamOut &inStream) const;
  43. /// Creates a Shape of the correct type and restores its contents from the binary stream inStream.
  44. static PathResult sRestoreFromBinaryState(StreamIn &inStream);
  45. protected:
  46. /// This function should not be called directly, it is used by sRestoreFromBinaryState.
  47. virtual void RestoreBinaryState(StreamIn &inStream);
  48. private:
  49. /// If the path is looping or not. If a path is looping, the first and last point are automatically connected to eachother. They should not be the same points.
  50. bool mIsLooping = false;
  51. };
  52. JPH_NAMESPACE_END