2D & 3D Spline (think smooth curve through n points) function

Thanks. I think i get how it works now. When you call getPosition(float param) it returns the point that is (param / pointCount) along the spline.

I tried using this to add nice curvy paths to the path finding code (http://www.java-gaming.org/index.php/topic,19539.0.html), and it works but it sometimes makes weird little knot loops that look a bit strange. I’ll look into how I can get rid of these. Thanks for posting these changes/optimisations 8)

spline.getPosition(3.5f) ends up roughly between point 3 and 4

It’s very fast to calculate, but not really useful.

spline.getTripPosition(123.0f) returns the point after you traveled 123 units.

You have to step through the spline, which is a very heavy operation, so I cache as much as possible, and look a binary search to find the right entry in the cache.

Those ‘weird knots’ are caused by the influence of nearby controlpoints. Could you add a screenshot / demo?

Ah thanks Riven, that getTripPosition method’s much better then.

Sorry for the late reply, I’ve been moving house. Here’s a demo of how I’m using it: http://keithphw.freehostia.com/PathFinderSpline2D/AStarPathFinder.jnlp
source: http://keithphw.freehostia.com/PathFinderSpline2D/src.zip

The player doesn’t follow the path of the spline yet but I made the program draw the spline from the points and it looks like it would make a nice natural-looking path. If you can think of a neat way to smooth out those knots it’d be cool to know.

Thanks for the code!
keith

[quote]If you can think of a neat way to smooth out those knots it’d be cool to know.
[/quote]
I’m pretty sure you have 2 (or more) control points at the same position per knot.
Like
… <0,0><1,0><1,0> …

Good idea. But they’re not at the same position, just very close. The walls in the maze are thin rectangles. Maybe if i scan for points that are too close I can average them into one point. Thanks hansdamph

The problems indeed seems to be that there are control points very near to eachother – probably the diff is sometimes very close to 0.0f, as the spline gets very instable (jumping around) at some points. Remember that the accuracy of float is maybe not sufficient here. Try rewriting the darn thing to use doubles, and your spline will be less jumpy in the ‘problem areas’, although it would be much better to simply fix the problem of control points that are probably within the same pixel.

@Riven Do you still have the code for this? The link is broken.

@Riven Do you still have the code for this? The link is broken.