• Create Account

## Need help calculating imprecise bullet trajectories in a 2D plane

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

### #1Nübček Pænus  Members

154
Like
0Likes
Like

Posted 16 April 2014 - 09:57 PM

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.

### #2DekuTree64  Members

1168
Like
0Likes
Like

Posted 17 April 2014 - 12:12 AM

This would be much easier working in angles rather than vectors. Use atan2 to convert vector to angle, and sin/cos to convert back to vector.

Sounds like all you need is to add a small random value to the angle of the shot. And for the AI safety, compare the angle between the AI position and the player position with the AI's facing direction. If the difference is greater than the range of the random value you add for inaccuracy, then there's no chance of the player being hit.

Edited by DekuTree64, 17 April 2014 - 12:14 AM.

### #3Álvaro  Members

20235
Like
0Likes
Like

Posted 17 April 2014 - 07:36 AM

I recommend using complex numbers for these computations.

1) You can add a little bit of jittering to a vector by rotating it by some random amount.

2) Why would you need the list of pink tiles? All you need is a check to see if something is in the pink area.

#include <complex>
#include <cstdlib>

typedef std::complex<double> Complex;

double uniform_random(double a, double b) {
return a + std::rand() * (b - a) / RAND_MAX;
}

Complex jitter(Complex direction, double max_angle) {
return direction * std::exp(Complex(0.0, uniform_random(-max_angle, +max_angle)));
}

bool is_in_danger(Complex subject_direction, Complex fire_direction, double max_angle) {
// Normalize input. You can skip this if you adopt the convention
// that all directions need to be normalized.
subject_direction /= std::abs(subject_direction);
fire_direction /= std::abs(fire_direction);

return std::real(subject_direction / fire_direction) >= std::cos(max_angle);
}

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.