Whats the best way to rotate a vector randomly within a limited angle?
For example, imagine a FPS game where you aim at a certain direction, but when firing a bullet it will shoot at the general aim direction but with a random offset up to 15 degrees angle from the original direction.
The easiest and fail safe solution is to create a Matrix, like for example
Matrix rot = Matrix.CreateRotationZ( randomAngle) and then transform the vector with Vector3.Transform(rot);
However, you can also use some trigonometry and create normal vectors that you add to your base vector to skew it to the given direction. Remember to normalize and multiply with the original length afterwards.
Mathematically you could visualize the problem as cone which represents the spread of fire. A bullet can be thought of as travelling any ray that is contained inside the cone. Even this is pretty unrealistic but may make for fun mechanics.
Use the random class to get a floating point, multiply by 2 and subtract 1 to get a value from -1 to +1.
var rnd = random.Sample()*2-1;
then multiply this with the angle to get an angle from -15 to +15 and add that to your target vector.
var offsetAngle = rnd*15;
But will be unrealistic because the offset is distributed evenly between -15 to 15. To get a more bell-like distribution concentrated to the center, multiply rand with itself a few times.
rnd = rnd * rnd * rnd;
var offsetAngle = rnd*15;
If you choice to use an odd number of multiplications (ex: rnd = rnd * rnd;) you need to preserve the sign. (Math.Sign() maybe?)
Now, if you are going for that cone effect, you need one more random number to rotate the offset 360 degrees around.
Thanks for the tips everybody, I actually wanted the ability to have different random on different axis, I though maybe there’s a simple way to do it. Anyway ended up doing something like this: