Jump to content
  • Advertisement
Sign in to follow this  
knarkowicz

Generate random perpendicular unit vector

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

Hi,

 

I have a unit vector v1 and would like to generate a random perpendicular unit vector v2 with uniform or close to uniform distribution.

 

Potential solution is to generate a random unit vector and cross it with v1 to get v2. Now this has two issues - non uniform distribution and for some inputs it generates degenerate vectors, so it requires running in a loop until a valid vector is returned.

 

My current solution is to generate a random angle. Build a rotation matrix M using v1 and two perpendicular vectors. Those vectors are generated by crossing v1 with axis of it's smallest component and by crossing result with v1. Finally transform vector [ cos(randomAngle), sin(randomAngle), 0 ] using rotation matrix M.

 

It works, but I feel that I'm doing excessive amount of calculations and there is a much simpler way to solve it.

Share this post


Link to post
Share on other sites
Advertisement

My current solution is to generate a random angle. Build a rotation matrix M using v1 and two perpendicular vectors. Those vectors are generated by crossing v1 with axis of it's smallest component and by crossing result with v1. Finally transform vector [ cos(randomAngle), sin(randomAngle), 0 ] using rotation matrix M.

If you call your two perpendicular vectors v2 and v3, isn't it equivalent to just do: (v2 * cos(randomAngle)) + (v3 * sin(randomAngle)); ?

Not much less work I suppose, but a little quicker. Edited by C0lumbo

Share this post


Link to post
Share on other sites
You can avoid sin and cos by generating two random numbers x and y in the range -1 to +1, rejecting if the two are outside the unit circle or both zero, then normalising and multiplying by the perpendicular vectors.

Share this post


Link to post
Share on other sites

You can avoid sin and cos by generating two random numbers x and y in the range -1 to +1, rejecting if the two are outside the unit circle or both zero, then normalising and multiplying by the perpendicular vectors.

 

Rejection sampling will cause you to reject (4 - pi) / 4 = 21% of samples taken, and will thus introduce branching in the computation (as well as an extra normalization). It is probably more efficient to just use sin and cos, depending on your platform.

Share this post


Link to post
Share on other sites

You can avoid sin and cos by generating two random numbers x and y in the range -1 to +1, rejecting if the two are outside the unit circle or both zero, then normalising and multiplying by the perpendicular vectors.

 
Rejection sampling will cause you to reject (4 - pi) / 4 = 21% of samples taken, and will thus introduce branching in the computation (as well as an extra normalization). It is probably more efficient to just use sin and cos, depending on your platform.
No doubt it could be made branchless, if running in a loop. If not in a loop, it's unlikely to be a hotspot.

Really, I was just pointing out that it can be done without trig functions. The OP never mentioned a performance issue.

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!