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