PathConstraintPath.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. /// @param inFractionHint Last known fraction along the path (can be used to speed up the search)
  27. /// @return Fraction of closest point along the path
  28. virtual float GetClosestPoint(Vec3Arg inPosition, float inFractionHint) const = 0;
  29. /// Given the fraction along the path, get the point, tangent and normal.
  30. /// @param inFraction Fraction along the path [0, GetPathMaxFraction()].
  31. /// @param outPathPosition Returns the closest position to inSearchPosition on the path.
  32. /// @param outPathTangent Returns the tangent to the path at outPathPosition (the vector that follows the direction of the path)
  33. /// @param outPathNormal Return the normal to the path at outPathPosition (a vector that's perpendicular to outPathTangent)
  34. /// @param outPathBinormal Returns the binormal to the path at outPathPosition (a vector so that normal cross tangent = binormal)
  35. virtual void GetPointOnPath(float inFraction, Vec3 &outPathPosition, Vec3 &outPathTangent, Vec3 &outPathNormal, Vec3 &outPathBinormal) const = 0;
  36. /// If the path is looping or not. If a path is looping, the first and last point are automatically connected to each other. They should not be the same points.
  37. void SetIsLooping(bool inIsLooping) { mIsLooping = inIsLooping; }
  38. bool IsLooping() const { return mIsLooping; }
  39. #ifdef JPH_DEBUG_RENDERER
  40. /// Draw the path relative to inBaseTransform. Used for debug purposes.
  41. void DrawPath(DebugRenderer *inRenderer, RMat44Arg inBaseTransform) const;
  42. #endif // JPH_DEBUG_RENDERER
  43. /// Saves the contents of the path in binary form to inStream.
  44. virtual void SaveBinaryState(StreamOut &inStream) const;
  45. /// Creates a Shape of the correct type and restores its contents from the binary stream inStream.
  46. static PathResult sRestoreFromBinaryState(StreamIn &inStream);
  47. protected:
  48. /// This function should not be called directly, it is used by sRestoreFromBinaryState.
  49. virtual void RestoreBinaryState(StreamIn &inStream);
  50. private:
  51. /// If the path is looping or not. If a path is looping, the first and last point are automatically connected to each other. They should not be the same points.
  52. bool mIsLooping = false;
  53. };
  54. JPH_NAMESPACE_END