Wrestling with Splines

This forum is for discussing shmup development, tools, engines and techniques.
User avatar
Posts: 5
Joined: Fri Mar 27, 2015 9:27 am
Location: Finland

Wrestling with Splines

Postby skoll » Sat Mar 28, 2015 2:57 pm

Hi all,

New to the forums.

I'm working on my first sidescrolling Shmup and I'm currently wrestling with enemy movement. I have understood, that splines is the way to do it. (And it's certainly a better way than my previous approach, which was to assign different acceleration values to the enemies at different points in time.)

The problem I'm facing is, that with catmull-rom splines, the path always goes past the checkpoints. Not sure how to explain it, so please have a look at the picture - the red dots are the control points.

Image
Scenario A shows how the algorithms I have tried behave and B is the path I would like to get. To a degree I can work around this by setting the control points before the point where the enemy should resume movement in straight line (Scenario C), but that's not intuitive and it's difficult to get the enemy to moving in a perfectly straight line.

Have you seen this as an issue? How have you solved it?

Cheers,

-Skoll

Site Admin
User avatar
Posts: 240
Joined: Thu Jul 26, 2012 2:32 pm
Location: Melbourne

Re: Wrestling with Splines

Postby monoRAIL » Sat Mar 28, 2015 3:27 pm

I believe you just need a new point at the midpoint of the curve like this...

Image

Oh, and welcome to the forum!

User avatar
Posts: 5
Joined: Fri Mar 27, 2015 9:27 am
Location: Finland

Re: Wrestling with Splines

Postby skoll » Sun Mar 29, 2015 6:23 am

Thanks for the suggestion. The midpoint does make the "corners" a bit smoother, but the dip still occurs. The best way I have found to work around this is to have two control points near the turning points, instead of one.
Image

Another thing that helps, that I haven't found in other implementations of catmull rom, is the concept of tension as explained at http://www.charlespetzold.com/blog/2009 ... light.html.

Site Admin
User avatar
Posts: 240
Joined: Thu Jul 26, 2012 2:32 pm
Location: Melbourne

Re: Wrestling with Splines

Postby monoRAIL » Sun Mar 29, 2015 3:07 pm

Here's a trick I liked to use when I started making shmups, and I didn't know any curves math. I was using the Torque2D engine which had a 'spring' function that basically worked like a linear interpolate that increased with distance.

Image

I'd have an invisible 'target' moving linearly along a path defined by points (the blue line).
Then the enemy would move a small percentage of the way from its current position to the target position every frame. This percentage, or 'spring force' should increase as the target gets further away. This results in the enemy moving along the red path.
Speed up the target to get the enemy to move faster.
Reduce the interpolate or 'spring' amount to make the enemy move more smoothly.

A downside to this technique is that the curve is like a bézier curve and not a catmull-rom, meaning the path does not travel through all the points. However the advantage is that there's no 'pre-empting' the next point, where the path curves away slightly before changing direction, and it's also very easy to get a large variety of enemy motion by just varying the spring amount.

This trick also works nicely for enemies that enter the screen and stop, because they will decelerate smoothly as they approach their target rather than stopping suddenly. I used this system on all the enemies in DroneSwarm:


User avatar
Posts: 5
Joined: Fri Mar 27, 2015 9:27 am
Location: Finland

Re: Wrestling with Splines

Postby skoll » Mon Mar 30, 2015 6:24 am

Thanks for sharing.

I'm using libGDX, which supports similar interpolation. Eventually I will probably need to support several different techniques for managing enemy paths, but for the time being the splines will have to do. Next I'll have to create some kind of editor to manage the splines in game screen. Getting the desired result by entering coordinates to a text file is surprisingly difficult.

Return to Development

Who is online

Users browsing this forum: No registered users and 7 guests