Bézier curve

October 25, 2012

Last week a fellow student showed me his most recent project, a Tower Defense game in Java with some special effects regarding light refraction. I then remembered that I too once had a plan to make a simple TD game, and maybe one day I'll be able to put some spare time into a project.

However, as he showed me what was working already, he mentioned Bézier curves, and that he used them for tracking enemies (his TD will be ray based, so towers don't "shoot" bullets, but establish a connection to the creeps to damage them).

I then recalled that I tried already once to solve the problem of shooting bullets at moving enemies (because I always liked bullet-based tower defense games :) ) and now thought of the Bézier curve as a solution.

Bézier curves are quite easy to calculate (at low orders), for example with n=2 (quadratic Bézier curve):

where P_0 is the starting point (from where the bullet starts), $$P_1$$ is its first target and P_2 is the point it will end at. B(t) will be the position of the bullet at time t.

I then tried to model a first simple bullet path with the quadratic Bézier curve:

In my opinion it doesn't look so good if the target moves fast (if the distance from P_0 and P_1 is large).

The bullet is somewhat to far behind the moving enemy:

It tries to catch up, but it seems like it is moving slowly in the beginning and has to rush towards the end.

I then looked for a way to smoothen the line and to maybe make it look a bit better. I just replaced the two ^2 by a parameter:

If you remove the trace and watch the bullet paths at different speeds, I think the blue line seems to be a good tradeoff. It somehow flies towards the moving enemy but still has the near future in mind and begins quite early to move more towards the pre-calculated endpoint.