PathConstraintPathHermite.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Jolt/Physics/Constraints/PathConstraintPath.h>
  5. JPH_NAMESPACE_BEGIN
  6. /// A path that follows a Hermite spline
  7. class PathConstraintPathHermite final : public PathConstraintPath
  8. {
  9. public:
  10. JPH_DECLARE_SERIALIZABLE_VIRTUAL(PathConstraintPathHermite)
  11. // See PathConstraintPath::GetPathMaxFraction
  12. virtual float GetPathMaxFraction() const override { return float(IsLooping()? mPoints.size() : mPoints.size() - 1); }
  13. // See PathConstraintPath::GetClosestPoint
  14. virtual float GetClosestPoint(Vec3Arg inPosition) const override;
  15. // See PathConstraintPath::GetPointOnPath
  16. virtual void GetPointOnPath(float inFraction, Vec3 &outPathPosition, Vec3 &outPathTangent, Vec3 &outPathNormal, Vec3 &outPathBinormal) const override;
  17. /// Adds a point to the path
  18. void AddPoint(Vec3Arg inPosition, Vec3Arg inTangent, Vec3Arg inNormal) { mPoints.push_back({ inPosition, inTangent, inNormal}); }
  19. // See: PathConstraintPath::SaveBinaryState
  20. virtual void SaveBinaryState(StreamOut &inStream) const override;
  21. struct Point
  22. {
  23. JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(Point)
  24. Vec3 mPosition; ///< Position on the path
  25. Vec3 mTangent; ///< Tangent of the path, does not need to be normalized (in the direction of the path)
  26. Vec3 mNormal; ///< Normal of the path (together with the tangent along the curve this forms a basis for the constraint)
  27. };
  28. protected:
  29. // See: PathConstraintPath::RestoreBinaryState
  30. virtual void RestoreBinaryState(StreamIn &inStream) override;
  31. private:
  32. /// Helper function that returns the index of the path segment and the fraction t on the path segment based on the full path fraction
  33. inline void GetIndexAndT(float inFraction, int &outIndex, float &outT) const;
  34. using Points = Array<Point>;
  35. Points mPoints; ///< Points on the Hermite spline
  36. };
  37. JPH_NAMESPACE_END