Sign in to follow this  

(SOLVED) random vector in plane perpendicular to (other) vector

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

Given: vector (direction) Wanted: vector (up), perpendicular to given vector (direction). Notes: the direction vector is normalised, the up vector will be normalised after this, so no need to worry about that. As i see it, there's two ways to get this (but i haven't been able to get a formula for either one): 1) you get any random vector (_NOT_ parallel with direction vector), then project it on the plane perpendicular to the direction vector 2) you get the plane perpendicular to the direction vector, then get a random vector that's part of that plane. despite being about particles and not a vector, this is the only article i've found that seems related: http://www.hut.fi/~vhelin/particles.txt I have read it but only understand parts, not even enough to see if that's what i need or not. [Edited by - kVandaele on November 16, 2004 9:08:29 AM]

Share this post


Link to post
Share on other sites
to do 1 you need to make random 3D direction vector. It's not so simple, need to do more sines and cosines.
I'd just do something like

vec3 RandomNormal(vec3 direction){
double alpha=random_float()*2*pi;
vec3 a=CrossProduct(vec3(1,0,0),direction);
float l=DotProduct(a,a);// squared length
// check for special case when our direction is very close to 1,0,0
if(l<very_small_value)return vec3(0,sin(alpha),cos(alpha));
//else
a*=1/sqrt(l);//normalize a - really needed for uniformness
vec3 b=CrossProduct(a,direction);// b should be unit-length
return a*sin(alpha)+b*cos(alpha);// and result must be quite close to unit-length, but if you want,renormalize - there's roundoff errors, etc...
}







[Edited by - Dmytry on November 15, 2004 12:23:21 PM]

Share this post


Link to post
Share on other sites
Given (x,y,z) you want (x',y',z') such that x*x'+y*y'+z*z'=0. If x=y=z=0 then any vector will work, but otherwise at least one component is not zero. Say that x is non-zero so x'=-(y*y'+z*z')/x. So you can choose y' and z' to be any value you please and x' is determined by those choices.

Share this post


Link to post
Share on other sites
you say that you need a random vector. why not just grab one and try it... lets call it v.


v = (0,1,0)
if direction == v:
v = (1,0,0) #same direction, set another v
up = cross(direction, v)



any reason this shouldnt work? unless direction happen to be 0.

Share this post


Link to post
Share on other sites
You can create a random 3d vector in space where x, y and z are just random values between -1 and 1. Take the cross product of this random vector with your direction vector. If the resulting vector is of length 0 (or less than a certain tolerance) repeat above step with a new random vector.

This will give you a random vector in the plane perpendicular to your direction vector.

Share this post


Link to post
Share on other sites
all good ideas, thanks. SpaceDude, since i already have a (completely) random vector assigned to the up vector (which is wrong), all i have to do is cross and check it, so yours seems simplest.

As the direction vector is normalised, doesn't that imply that it's never 0? If it doesn't (imply that), it's not (0, ever).

As for your example rollo, while certainly correct, perhaps you misunderstood what i meant with 'random'. What i mean is using the rand() function generate a vector that is different each time. In your case, if direction is the same, the up vector will be the same as well.

Share this post


Link to post
Share on other sites
Quote:
Original post by kVandaele
As the direction vector is normalised, doesn't that imply that it's never 0? If it doesn't (imply that), it's not (0, ever).


The length of your direction vector is always 1 I assume, thats what normalised means. That doesn't mean that the cross product of your direction vector and a random vector can never be of length 0. If two vectors are parallele to each other, then the cross product will be (0,0,0).

Share this post


Link to post
Share on other sites

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