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

Started by
1 comment, last by oliii 16 years, 9 months ago
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?
Advertisement
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;}

Everything is better with Metal.

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.

Everything is better with Metal.

This topic is closed to new replies.

Advertisement