How can one generate a random vector that diverges from an arbitrary vector to a given range (angle)?

In case I'm not clear, I want something like a particle emitter that has a certain 'spread' angle.

So, given are:

-vector x,y,z (or plane normal) -spread angle

I've read something about turning the vector into a quaternion (how?), then rotating a vector by a (random?) number of degrees, and something similar with a (rotation?) matrix, but I don't have a clue how/if this works.

Thanks!

roel
—
2007-05-18T18:28:48Z —
#2

edit: Sorry, I didn't read carefully enough; I thought that you asked for an exact angle, not a range.

Reedbeta
—
2007-05-18T19:25:37Z —
#3

Try starting with the equations for a random point on a sphere; you can easily modify the equations to generate a random vector within a cone by scaling the polar angle (denoted phi on that page) to the range you want. The cone will be centered the north pole of the sphere (usually (0, 0, 1)), but as roel said you can then rotate it to position the cone along whatever vector you want.

However, this gives you a uniform distribution over the cone and you might rather want the distribution to be biased more towards the center and fade out around the edges. You can easily acheive a cosine-weighted distribution by removing the arc-cosine in the sphere-point-picking equations. You could also apply a Gaussian function or something to the polar angle for more control over the spread.

Remdul
—
2007-05-21T14:25:15Z —
#4

I guess it all comes down to converting the vector (plane) to quaternion/matrix, then use that to rotate an up-vector by a random value (which lies within a range).

What is the easiest way to convert a vector into a quaternion? I can compute a 3x3 rotation matrix from the vector with a few cross products (lookat), then convert the matrix to quaternion. Is there a shortcut?

Nils_Pipenbrinck
—
2007-05-21T19:12:03Z —
#5

Hi Remdul.

I just wrote some code that somewhat works the way you thought up.

I built an orthogonal-basis (coordinate system) from the normal, calculated a random point on a circle in that coordinate system and just applied some basic trigonometry to build the new vector.

If you precompute the orthogonal basis and the sin/cos of the spread angle it boils down to some muls, adds and a square-root.

Works with spread-angles up to, but not reaching 180°. Afterwards it will break down.

So, the point is that you provide a direction vector for the particle (dir[3]) and then change it with a random factor. There are no specific angle of spread easily calculated, but you dont need such in most cases like these.

45degree distribution would be dirrandom = length_of_dir3

Tuomo

juhnu
—
2010-08-14T11:54:35Z —
#9

There are no specific angle of spread easily calculated, but you dont need such in most cases like these.

A dot product along the principal axis will do - simply reject/ignore all the vectors that are outside the preferred angle. For orientation of the whole emitter one can use normal matrices for coordinate transformations (transforming from emitter to world space).