diff --git a/wpilibc/src/main/native/cpp/spline/SplineHelper.cpp b/wpilibc/src/main/native/cpp/spline/SplineHelper.cpp index a6b6a6996c..e50e686c17 100644 --- a/wpilibc/src/main/native/cpp/spline/SplineHelper.cpp +++ b/wpilibc/src/main/native/cpp/spline/SplineHelper.cpp @@ -63,10 +63,14 @@ std::vector SplineHelper::CubicSplinesFromControlVectors( yInitial[1]); if (waypoints.size() > 4) { for (size_t i = 1; i <= waypoints.size() - 4; ++i) { - dx.emplace_back(3 * (waypoints[i + 1].X().to() - - waypoints[i - 1].X().to())); - dy.emplace_back(3 * (waypoints[i + 1].Y().to() - - waypoints[i - 1].Y().to())); + // dx and dy represent the derivatives of the internal waypoints. The + // derivative of the second internal waypoint should involve the third + // and first internal waypoint, which have indices of 1 and 3 in the + // waypoints list (which contains ALL waypoints). + dx.emplace_back(3 * (waypoints[i + 2].X().to() - + waypoints[i].X().to())); + dy.emplace_back(3 * (waypoints[i + 2].Y().to() - + waypoints[i].Y().to())); } } dx.emplace_back(3 * (waypoints[waypoints.size() - 1].X().to() - diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/spline/SplineHelper.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/spline/SplineHelper.java index d270554625..73c6aeef61 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/spline/SplineHelper.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/spline/SplineHelper.java @@ -145,8 +145,11 @@ public final class SplineHelper { if (newWaypts.length > 4) { for (int i = 1; i <= newWaypts.length - 4; i++) { - dx[i] = 3 * (newWaypts[i + 1].getX() - newWaypts[i - 1].getX()); - dy[i] = 3 * (newWaypts[i + 1].getY() - newWaypts[i - 1].getY()); + // dx and dy represent the derivatives of the internal waypoints. The derivative + // of the second internal waypoint should involve the third and first internal waypoint, + // which have indices of 1 and 3 in the newWaypts list (which contains ALL waypoints). + dx[i] = 3 * (newWaypts[i + 2].getX() - newWaypts[i].getX()); + dy[i] = 3 * (newWaypts[i + 2].getY() - newWaypts[i].getY()); } }