Making dogfight AI in Defold (SOLVED)

Hello everyone
I was am currently making a dogfight game and the major problem that is troubling me is the AI.
I even checked GBRausers by @britzl and was amazed :astonished:
Still, I was actually thinking :thinking: about creating an AI like this Dogfight 2.
I am never good at programming AI, so I request to please help me out.

1 Like

The AI seems to be fairly simple. The enemy planes will obviously always move/fly in the direction they are facing and they will always try to rotate towards the player.

I have an example of how zombies rotate and move towards a player here: https://github.com/britzl/publicexamples/tree/master/examples/simple_ai

The code should be directly translatable to your project. Relevant snippet of code: https://github.com/britzl/publicexamples/blob/master/examples/simple_ai/simple_ai/zombie.script#L27-L38

1 Like

The example is really cool(including the zombies!) . But the major problem that I come across is that when you have more than 1 plane, they all tag behind one another creating a nice crowd of planes.I really liked how it was solved in Dogfight 2 and wanted to implement it in my game.

1 Like

Not sure I got the right game (I didnā€™t want to install flash, so watched this instead.

One way to avoid crowds is to check if other planes are nearby.

You can add a collision object specifically for this, and make it a bit bigger than the plane, and make it detect other enemy planes.
And when they start to move too close to each other, you can make them either keep their distance, or move them further apart etcā€¦

This is a good suggestion. I played around with this solution a bit this evening. It can probably be tweaked for better results:

CODE: https://github.com/britzl/publicexamples/tree/master/examples/swarm_ai
DEMO: http://britzl.github.io/publicexamples/swarmai/index.html

2 Likes

Many many Thanks to @britzl and @Mathias_Westerdahl for your help. After thinking over it for a month, at last I got what I desperately needed.
Just one more doubt-How do I increase the radius in which the swarm ai ships are rotating(could be helpful when dealing with multiple ship types.).
P. S. Thanks again

That would be a combination of rotation speed:

And movement speed:

High rotation speed and low forward speed and youā€™ll turn on the spot. Low rotation speed and high forward speed and youā€™ll need a huuuge radius to turn.

1 Like

Made it. The AI is now exactly as I needed. THANKS a lot again.
P.S.-
Here is the ss-

5 Likes

Nice! Please share a playable when you have the time!

2 Likes

This is actually something Iā€™ve worked on in C++. I donā€™t a DLL for my ā€œnav controllerā€ object, but maybe I should.

a) Is there a place to get a ā€œtemplate library dependency projectā€ somewheres?

b) It can also help if you modify a planeā€™s turn speed slightly (adding fractions of a unit, like a random number 0-40 X 0.001 or something when the planes are created) so that they donā€™t all fly exactly the same way. Does your dogfighting plane have turn speed?

Creating a library project is only a matter of specifying which folder(s) you wish to export in game.project. Check the manual on library projects for more info.

@britzl While playing around with your AI, I noticed one thing though. If the enemy is killed, all the other enemies on the screen stop and the console throws an error instance null not found while calculating distance to. So, do I have to do something like table.delete on nearby table or probably something else am I missing?
But I must add that your AI is awesomeā€¦

Well, I would suggest that your code sends a message to each AI agent when their target dies. And if they donā€™t have a target they can roam randomly or continue to travel in their current direction.

1 Like