Spline Curve, getLength() returns zero

So a small but confusing issue: I have a dynamically calculated spline curve, but no matter how often it changes (points are added to it), I get 0 as the calculated length using spline.getLength(). The starting point is always the player’s current position. Any ideas why, or rather what’s the limiting aspects of getLength()s logic?

For reference, here’s a sample of points that fail / return 0

[
  {
    "x": 288,
    "y": 1659.9444444444443
  },
  {
    "x": 230.11012115563835,
    "y": 1573.5729937009567
  },
  {
    "x": 255.98449207828514,
    "y": 1480.9237290992507
  },
  {
    "x": 263.80048462255354,
    "y": 1373.3203437458656
  },
  {
    "x": 205.35947809878837,
    "y": 1266.2955687181432
  },
  {
    "x": 125.16682199440822,
    "y": 1204.4923486705077
  }
]

To expand: even more weird, I execute spline.getLength() on mouseMove, to get the current length of the spline, then one more time when I mouseUp. IF I remove that mouseMove invocation, the mouseUp getLength() returns the actual length. Include the mouseMove invocation, and it always returns 0. Is there some mutation going on here, the length is baked-in after the first execution (when there wouldn’t be any length 'cos there’s only one point?)

I guess you do spline.updateArcLengths().

Perfect that worked; but given it re-calculates the lengths, is that an expensive operation? I’ve amended the pointermove logic to only add a new point to the spline when ~128 pixels away, but still… bit worried I’ll kill performance.

I wonder would it be faster to simply do a reduce() on the spline’s points,summing up straight line distances between each point

You should update arc lengths only when the curve has changed.

1 Like