Search code examples
algorithmpseudocodepath-finding

how to figure out cursive paths for an enemy to follow


The Problem

I am making a game where enemies appear at some point on the screen then follow a smooth curvy path and disappear at some point. I can make them follow a straight path but can't figure out the way to make them follow the paths depicted in the image.

Attempts

I started with parabolic curve and implemented them successfully. I just used the equation of parabola to calculate the coordinates gradually. I have no clue what is the equation for desired paths supposed to be.

What I want

I am not asking for the code.I just want someone to explain me the general technique.If you still want to show some code then I don't have special preference for programming language for this particular question you can use C,Java or even pseudo-code.

enter image description here


Solution

  • First you need to represent each curve with a set of points over time, For example:
    -At T(0) the object should be at (X0, Y0).
    -At T(1) the object should be at (X1, Y1).

    And the more points you have, the more smooth curve you will get.

    Then you will use those set of points to generate two formulas-one for X, and another one for Y-, using any Interpolation method, like The La-grange's Interpolation Formula:

    enter image description here

    • Note that you should replace 'y' with the time T, and replace 'x' with your X for X formula, and Y for Y formula.

    I know you hoped for a simple equation, but unfortunately this is will take from you a huge effort to simplify each equation, and my advise DON'T do it unless it's worth it.

    If you are seeking for a more simple equation to perform well in each frame in your game you should read about SPline method, In this method is about splitting your curve into a smaller segments, and make a simple equation for every segment, for example:

    Linear Spline:

    enter image description here

    • Every segment contains 2 points, this will draw a line between every two points.
    • The result will be some thing like this: enter image description here

    Or you could use quadratic spline, or cubic spline for more smooth curves, but it will slow your game performance. You can read more about those methods here.

    I think linear spline will be great for you with reasonable set of points for each curve.

    • Please change the question title to be more generic.