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

This topic is 4990 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 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,0if(l<very_small_value)return vec3(0,sin(alpha),cos(alpha));//elsea*=1/sqrt(l);//normalize a - really needed for uniformnessvec3 b=CrossProduct(a,direction);// b should be unit-lengthreturn 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 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 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 vup = cross(direction, v)

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

##### 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 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 on other sites
note that vector with random components in -1..1 range does not give really random direction.(that is, result is not equally distributed)

##### Share on other sites
Quote:
 Original post by kVandaeleAs 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).

1. 1
2. 2
Rutin
22
3. 3
JoeJ
18
4. 4
5. 5

• 37
• 23
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631705
• Total Posts
3001824
×