PathConstraintPath.h 3.2 KB

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