Sign in to follow this  
adam17

Random vector within range

Recommended Posts

adam17    227
im trying to write a particle engine but im stuck on this one part. i need to create a function that can create a random vector, within a certain range, to initialize a particle. say for example there is a spread of 90 degrees, around the normal, that the particle's direction can point. i cant figure out how to get a vector to initialize within a certain angle. once i find that random vector i just make that the velocity (speed and direction) of the particle. make sense?

Share this post


Link to post
Share on other sites
as31415rin    122
maybe you could translate the normal to some spherical representation and then add random values inside a certain range to the angles, before transforming it back to cartesian coordinates ?

Share this post


Link to post
Share on other sites
Illco    928
This thread was also about generating unit vectors, only this time the vectors were also allowed to point downwards (i.e. vectors in the unit sphere). However you can simply discard the negative directions. It also mentions how to do it angle-based.

Greetz,

Illco

Share this post


Link to post
Share on other sites
adam17    227
ok ive been playing with my engine alot and i decided to go with using random angles and then convert them to vectors. im using this code:

float sp = speed;

//random angle
float xy = rand()%(int)spread-(spread*0.5); //spread on XY plane
float zy = rand()%(int)spread-(spread*0.5); //spread on ZY plane

float x = sin(xy);
float y = cos(xy);
float z = sin(zy);

if(y<0)
y*=-1;

Vector3 vect = Normalize(x, y, z);

pArray[part].dir.x = vect.x * sp;
pArray[part].dir.y = vect.y * sp;
pArray[part].dir.z = vect.z * sp;


the problem is when i set the spread to any angle between 1 and 179.5 i always get the same effect. the particles dont constrain to the range and exist everywhere above the xz-plane. does anybody see the problem in my code?

p.s. im aware that "float sp" is redundant but i plan to add speed variation later and im going to put it there.

Share this post


Link to post
Share on other sites
jyk    2094
If this is stupid, I apologize, but are you converting to radians before calling the trig functions? I just ask because you give the range in degrees in your post.

Also, is there any particular reason you don't just use the spherical representation directly, i.e.:

float x = sin(xy)*cos(zy);
float y = cos(xy)*cos(zy);
float z = sin(zy);

Just curious (you may very well have a good reason for doing it the way you're doing it).

Share this post


Link to post
Share on other sites
adam17    227
im only inputting degrees and there is no conversion to radians. could that be the problem?

EDIT: not converting the degrees into radians WAS the problem. woohoo!!! thanks for the help. soon i think ill post some screens of my particle engine!

Share this post


Link to post
Share on other sites

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