Jump to content
  • Advertisement
Sign in to follow this  
fpsgamer

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

This topic is 3995 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


Link to 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


Link to 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


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!