Sign in to follow this  

Best Way to Randomly Adjust a Vector

This topic is 2543 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.
http://mathworld.wolfram.com/SpherePointPicking.html

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

This topic is 2543 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this