Implementing Flak


Towards the end of my previous post I started talking about submarine combat. Firing a torpedo isn’t just a case of point, shoot and hit. The target is tracked, its course is predicted and the torpedo is essentially programmed to explode in the targets path to cause the damage. It’s not only submarines which do this, but also certain kinds of anti-air flak cannons. Below is a flak avoidance training video from world war 2 which shows how these anti-air guns worked.



Flying among all those explosions must have been terrifying and very stressful. I thought why not add some flak cannons to the mother ship in Slingshot? There wasn’t much information on-line about how to implement flak, so here is how I tackled the problem.

I had three main goals with the flak in slingshot: 1) The effect needed to look cool, 2) I wanted players to be able to watch the flak training video and gain an advantage from it, and 3) I wanted it to be more deadly the closer you got to the mother ship.

To achieve the first and second goal I’d need to model the flak cannons somewhat realistically. The problem comes down to plotting an intercept course, a very simple one. We know the players position, P1, and velocity, PV. If we call the position of the flak cannon F1 and the firing velocity of the flak FV. Then…

P2 = P1 + t*PV

where t is time. If we want the flak to hit the player at P2 then…

P2 = F1 + t*FV

which leads to…

P1 + t*PV = F1 + t*FV


Flak Cannons

The flak shell needs to explode at P2 which means t is the length of the fuse we want to set in the flak shell. I decided to change t depending on the distance, R in the diagram, from the player to the mother ship. Clearly a smaller t would give the player less time to change course to avoid the shell. So the closer the player is the lower the value of t, which means the closer you are to the mother ship the more deadly the flak. Once an equation is set up to work out t at a given value of R we simply calculate it and use

FV = (P1+t*PV-F1)/t

to find the launch velocity. A value for the fuse length on the flak shell is set and we fire it at the launch velocity. If the player is within the explosion effect then do some damage. Hopefully that made sense…

flak

I’m quite happy with how the effect turned out. In slingshot I’ve made the gravitational pull of all the planets and sun effect the motion of the flak. This means you get some interesting results and effects. I’ve also set the range of the flak quite large but the vision of the mother ship low, but I’ve implemented communication between the AI. Smaller ships can effectively spot for the mother ship.

If you want to see the effect in action you can play the game right now. It is a javascript game so you can play it in your browser, just fly towards the red dot on the map, and as always let me know what you think!

Slingshot is a roguelike ‘like’ space shooter with realistic orbital mechanics, developed for HTML5 using Phaser. You can play the game here To keep up to date follow me @DrSeanWalton and Barry @Half_Hit_Points on twitter.
Share on Reddit0Tweet about this on Twitter0Share on Google+0Share on Facebook0Digg thisShare on LinkedIn0Share on StumbleUpon0Pin on Pinterest0

Post a comment


2 × = eight

You may use the following HTML:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>