Jump to content
  • Advertisement
Sign in to follow this  
L. Spiro

Best Way to Randomly Adjust a Vector

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

Let's say I have a rapid-fire weapon and to simulate recoil I want to fire the bullets spreading slightly out randomly by a given amount.
I have seen picking sphere points at random but this is not so useful.

These routines do not accept an input vector.
I have a vector that my player or bot is facing and I want to adjust within tolerance from there.

I am looking for a routine that accepts an input 3D vector and a maximum range and outputs a vector representing the input vector adjusted by a random amount within the tolerance.

Thank you,
Yogurt Emperor

Share this post

Link to post
Share on other sites
Thank you for the direction, but I don’t know if this is for what I was searching.
I have seen code for this somewhere and it is quite short and simple.

Yogurt Emperor

Share this post

Link to post
Share on other sites
A simple (but not necessarily best) way would be: Compute a 2D vector from a random angle in [0,360)
a := random( 0, 360 );
and a random spreading range (err, correct wording?) in [0,D)
d := random( 0, D );
so that
v := [ cos(a) sin(a) 0 ]t * d

Add this to the local shooting direction vector, so that
v' := [ 0 0 1 ]t * R + v
where R denotes the distance at which the spreading should have the extent D.

Normalize this vector
v" := v' / ||v'||
and use this as new shooting direction. It is in local space, of course, and computed with z to be the standard shooting direction. So you have to transform it into the space where needed as usual.

Share this post

Link to post
Share on other sites
If you want a uniform distribution on a spherical cap centered around a given vector, you can do this:
* Pick a random number z between cos(alpha) and 1. The parameter alpha controls the size of the cap.
* Compute r = sqrt(1-z*z).
* Pick a random angle theta.
* Build the vector (r * cos(theta), r * sin(theta), z)
* Apply a rotation to this vector that would turn (0, 0, 1) into your original vector.

You can get other distributions by picking z differently in the procedure above.

If you are not particularly concerned with the distribution you get, you can use a much simpler procedure: add a little random vector to your original vector (it doesn't matter much from what distribution; I would start with a normal) and then normalize the result.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!