Quadratic Bezier Curve Demo

Quadratic Bezier Curve Demo by pyman

I’m enjoying some of the maths that goes in to scripting and thought I’d branch out a bit by trying my hand at mathematically formed curves.  I made this in GameMaker and you can play a demo of it in the link above.

The code’s not actually too tricky once you can visualise the inner workings of it.  The image to the left demonstrates this.

Essentially the curve is created within an encapsulating triangle.  On two of each of the legs there is a variable (or object) that runs along its length. On the image above these are represented by Q0 and Q1

If we’re looking at the above image, one of the variables runs from P0 to P1, the other from P1 to P2. Both variables are the exact same distance between each of their destinations, so if Q0 is 10% of the distance to its destination, so is Q1.

A line is then drawn between Q0 and Q1 and a third variable (B in the image) is placed which moves between Q0 and Q1.  This variable is also always the exact same distance from its destination as Q0 and Q1.

As Q0 and Q1 move to their destinations, B is used to draw a smooth curved line between P0 and P2.

Why is this useful for game design?

The ability to draw curved lines based on mathematical algorithms in games can have a number of uses for games and game designers.  They can be used for projectile calculations, in an artillery game for example.  They can also be used for AI pathfinding and the creation of physical paths themselves for example in city building games like Cities: Skylines.