Jump to content
• Advertisement

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

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

If you intended to correct an error in the post then please contact us.

## 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 this post

##### Share on other sites
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;}

#### Share this post

##### 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.

#### Share this post

##### Share on other sites

• Advertisement

### Announcements

• Advertisement

• ### Popular Contributors

1. 1
2. 2
3. 3
Rutin
20
4. 4
5. 5
khawk
14
• Advertisement

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633655
• Total Posts
3013179
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!