PathConstraintTests.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include "UnitTestFramework.h"
  4. #include "PhysicsTestContext.h"
  5. #include <Jolt/Physics/Constraints/PathConstraintPathHermite.h>
  6. #include <Jolt/Physics/Constraints/PathConstraintPath.h>
  7. #include "Layers.h"
  8. TEST_SUITE("PathConstraintTests")
  9. {
  10. // Test a straight line using a hermite spline.
  11. TEST_CASE("TestPathConstraintPathHermite")
  12. {
  13. // A straight spline
  14. // This has e.g. for t = 0.1 a local minimum at 0.7 which breaks the Newton Raphson root finding if not doing the bisection algorithm first.
  15. Vec3 p1 = Vec3(1424.96313f, 468.565399f, 483.655975f);
  16. Vec3 t1 = Vec3(61.4222832f, 42.8926392f, -1.70530257e-13f);
  17. Vec3 n1 = Vec3(0, 0, 1);
  18. Vec3 p2 = Vec3(1445.20105f, 482.364319f, 483.655975f);
  19. Vec3 t2 = Vec3(20.2380009f, 13.7989082f, -5.68434189e-14f);
  20. Vec3 n2 = Vec3(0, 0, 1);
  21. // Construct path
  22. Ref<PathConstraintPathHermite> path = new PathConstraintPathHermite;
  23. path->AddPoint(p1, t1, n1);
  24. path->AddPoint(p2, t2, n2);
  25. // Test that positions before and after the line return 0 and 1
  26. float before_start = path->GetClosestPoint(p1 - 0.01f * t1);
  27. CHECK(before_start == 0.0f);
  28. float after_end = path->GetClosestPoint(p2 + 0.01f * t2);
  29. CHECK(after_end == 1.0f);
  30. for (int i = 0; i <= 10; ++i)
  31. {
  32. // Get point on the curve
  33. float fraction = 0.1f * i;
  34. Vec3 pos, tgt, nrm, bin;
  35. path->GetPointOnPath(fraction, pos, tgt, nrm, bin);
  36. // Let the path determine the fraction of the closest point
  37. float closest_fraction = path->GetClosestPoint(pos);
  38. // Validate that it is equal to what we put in
  39. CHECK_APPROX_EQUAL(fraction, closest_fraction, 1.0e-4f);
  40. }
  41. }
  42. }