Another day, another shared code. (Although I actually have to finish and polish all my other little projects... I know... I know...)

According to Mathworldthe definition of a spline is: "A piecewise polynomial function that can have a locally very simple form, yet at the same time be globally flexible and smooth. Splines are very useful for modeling arbitrary functions, and are used extensively in computer graphics."

Basically, you define a number of points in 2D or 3D space, and using these points to create a "spline", a curve which smoothly goes through all points. What you can use this for, I will leave to your own imagination.... (hint: the missile-movement in this game is created using splines) Using this code, you can define a "spline", and get any point on that line (between [0..1].. where 0 is the start and 1 is the end point of the line)

Easy, no? for 2D points, just replace Spline3D with Spline2D and add/get Vector2f points. In the missiles example, I create a Spline with 3 points, one at the origin (the space ship), one behind the space-ship (a bit randomized), and one at the target. After that, every time the enemy ship moves, I re-calculate the spline, while the missile continues along the spline's trajectory.

Yes, I could, but in my game I use my own "Vector2D" class instead of LWJGL's Vector2f class. Its a little different... I will rewrite my game code using the Vector2f class (was planning to do this anyway) and post the solution here. (my own vector2d class has more utility functions, but I can easily make them static functions in a "VectorTool" class or something)

Good stuff, but does it only provide calculating a position from a given t value (0, 1)? Just smoothly interpolating t will give you a bursty movement with variable speed which isn't much good for missiles etc

Curves really need a calcPositionAtLength() to be practical, unfortunatly thats a right pain to write.

I gave the code a big overhaul to support calculating the position at a certain distance from the origin, following the spline.

The initial version was "quite fast" but after some intelligent caching I managed to make the code 200x faster. Note that I'm not caching the calculated values, as they are not likely to be reusable, but I cache the values in the intermediate steps. In a reasonably large spline, containing 16 control-points measuring 3750 units, a position lookup for a random distance takes only 0.09ms on a 2.4GHz machine! (was 24.0ms when walking along the spline)

2. 600% faster: random distance calc, now 15us, not ms, even 6.2us with server VM on 2.4GHz P4, first (uncached) calculation takes 36.3ms. After traversing a very long spline very slowly with high accuracy, only 512 float values are cached over time, so no need to worry about RAM consumption.

3. Changed constructor:

public Spline(Vector3f[] vecs, float accuracy, float margin)accuracy works very smooth when using 0.001-0.010. the higher the rougher, cheaper in first calc, same speed (!) when cached.margin defines the margin-of-error the actual answer. measured in units.

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.

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.

Hi, appreciate more people! Σ ♥ = ¾Learn how to award medals... and work your way up the social rankings!

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org