PathConstraintTests.cpp 1.7 KB

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