Hi all,

I dabble in a bit of XNA from time to time. I'm creating a 2d top down tank game. My AI bot that controls the enemy tank, works like this simple pathfinding to target until in range, turn turret to target and fire.

The problem I have is, it's really hard to defeat the AI tank as it's so accurate and also not very realistic as it hardly ever misses.

I'm not even sure what to search for in this case.

Any links or thoughts on how to approach this problem would be welcome.


Hi all,

simple pathfinding to target until in range, turn turret to target and fire.

Might be hard to give advice if we don't know how you're doing the above.

When aiming and firing, if you're using coordinates, distances, etc. maybe take where you are going to aim, X, make a range of values around that, Y, where X-5 < Y < X+5 (for instance...you can use distances, radius around target, etc.) and then pick a random number from that range.

So put in a random number generator that decides whether the AI hits or misses.
Draw a number between 0 and 1 (most random number generators produce a floating point number).
If you want a 50% chance to hit, have it hit if the number drawn is >0.5, if you want a 25% chance to hit, have it hit if the number is >0.75.
That way you can even tune the AI accuracy, making the game easier or harder as you want even at runtime.

Another thing to consider is giving the AI only partial information about player positions.
You can't see AI forces hidden by terrain (I assume), so make it such that the AI can't see you either is you're hidden from it by terrain.

Sorry if i didn't give enough info. The bullet fired from the tanks detect collisions through pixel based collision and i'm using an A star path finding algorithm.

The turret rotation works by if in range then calculate angle to target, if angle is less than rotation speed aim at target, else turn towards target at rotation speed.

I'm not sure in there where to add a fudge factor. I suppose taking your advice (donaldw) I could add a fudge factor (mmmm fudge) to the target position. That way when the bot hits its target, the target may not be the player tank.

jwenting - I think that method would work for most rts's where accuaracy is based on percentages, but as I went with CD it would look weird if a bullet flew through a tank without hitting it becasue it failed the random roll. My fault for not explaining the CD part. You're right about giving the bot limited info. I've given it a line of sight radius, if no player tank is in that radius then it follows a search pattern like a scout then signals the main force to come and deal with the threat if detected and substantial.

no, it wouldn't look weird.
The shell might be determined to fall short, go high, bounce off armour, the gun might misfire, be creative.

You're thinking way too linear here "if it fires in the right direction it always hits". Don't do that.
The random factor might introduce an inaccuracy into the firing solution, throwing off the aiming angle, gun elevation, etc., maybe having an influence on the quality control used in manufacturing the shell causing it to have a faulty propellant charge of firing pin, whatever can cause a weapon to fail.

You're thinking way too linear here

Ahhhhh GCSE science flash back.

I like your thinking, thanks for the ideas, i'll try to put all the suggestions into practice.

Cheers guys