# Randomly "Jittering" the direction of a vector ...

This topic is 3838 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Lets say I have a vector that is pointing in a particular direction. How can I change the direction of that vector slightly along each axis while still maintaining its general direction?

##### Share on other sites
depends. one dumb solution that comes to mind

float frand(float range){    return ((float)(rand()) / (float)RAND_MAX) * range;}float frand(float min, float max){    return min + frand(max - min);}Vector Jitter(const Vector& dir, const float amount=0.01){    Vector jitter = dir + Vector(frand(-amount, amount), frand(-amount, amount), frand(-amount, amount));    jitter.normalise();    return jitter;}

##### Share on other sites
I think the 'spray' would be inside a box shape. To make it circular.

Vector Jitter(const Vector& dir, const float amount=0.01){    Vector dev(frand(-1, 1), frand(-1, 1), frand(-1, 1));    dev.Normalise();    dev *= amount;    Vector jitter = dir + dev;    jitter.normalise();    return jitter;}

That should make the distribution more circular. although I'm not sure about the distribution. I would think it would have a lot more probability around the centre vector, and a lot less chances of deviating too much. Which wouldn't be bad for a gun bullet spread pattern.

There are all sorts of things you can do to get the distribution you want.