PathConstraintPathHermite.h 2.0 KB

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