I'm programming the A.I. for a 2D soldier that has to shoot another soldier. I use ray casting to calculate the bullet trajectory. For example in diagram A the shooter is at (10,13) and the target is at (5,1) so the bullet will be traveling at -0.4167 units per tick on the X axis and -1.000 units per tick on the Y axis. The algorithm I use to get those number is as follows:
// get the distance on each axis tx = abs(pta.x - ptb.x) ty = abs(pta.y - ptb.y) // make the longer axis 1.0 and the shorter axis a smaller number if (tx == ty) tx = ty = 1.0; elseif (tx > ty) tx = 1.0; ty = ty / tx; else ty = 1.0; tx = tx / ty; endif // invert any axis that is negative if (pta.x > ptb.x) tx = -tx; if (pta.y > ptb.y) ty = -ty;
This is fine, but now I want to skew the soldier's accuracy a little so that each bullet is fired at a slightly different angle, as shown in diagram B. I have two questions:
1) I probably need to change the angle by a few hundredths or so every shot to simulate inaccuracy, but my technique is based on a grid instead of angles. What would be the easiest way to modify the firing angle in this way?
2) How can I retrieve a list of the pink tiles in diagram B? I could run the algorithm on every possible angle but that could be slow and my program needs to be as fast as possible. I need the list because, as I stated earlier, this is an A.I. and the guy needs to know if there is risk of friendly fire.
Thanks in advance. By the way, in case you haven't noticed already, I suck at math. Just saying.
Edited by Nübček Pænus, 16 April 2014 - 09:59 PM.