# Vector for 1 radius sphere

## Recommended Posts

Ben091986    108
Hi folks, how can i make a vector with radius 1 point to a sphere ?? i know what it is in 2d, but my imagination is limited, in 3d it doesnt work as well i have these formula
//alpha is x,y angle
//beta is x,z angle
float alpha = ::getRandomMinMax( 0.0f, 360.0f );
float beta = ::getRandomMinMax(  0.0f, 360.0f );
float x = cos(alpha) * cos(beta);
float y = sin(alpha) * sin(beta);
float z = cos(alpha) * sin(beta);

and a picture you can click on it. and can someone explain what a vector should look like if alpha and beta are 90 degree... i think that there is the mistake cause sin(90) * cos(90) is always 0 the problem is at 45 and 0 degrees these vectors are just too small, but x = 0,7 * 1 = 0,7 y = 0,7 * 0 = 0 z = 0,7 * 0 = 0 would be (45,0) = (0.7,0,0) and that is ok isnt it ?? [Edited by - Ben091986 on March 23, 2008 6:33:05 PM]

##### Share on other sites
taby    1265
It seems like you would be trying to make a random point on the sphere?

If so, this link is for you:
http://mathworld.wolfram.com/SpherePointPicking.html

The main point of the page is that you cannot use a linear distribution for the angle. If you do that, the points will eventually bunch up at the poles.

To get a good distribution you use two random variables in the range [0, 1]:

theta = 2*pi*var1
phi = acos(2*var2 - 1)

From there you convert the spherical coordinates to Cartesian by:

x = r*cos(theta)*sin(phi)
y = r*sin(theta)*sin(phi)
z = r*sin(phi)

Since you're talking about a unit sphere, r = 1.

Is this something like what you're asking for?

##### Share on other sites
Quote:
 i think that there is the mistake cause sin(90) * cos(90) is always 0

cos 90 = 0 ;)

##### Share on other sites
Ben091986    108
Quote:
Original post by NerdInHisShoe
Quote:
 i think that there is the mistake cause sin(90) * cos(90) is always 0

cos 90 = 0 ;)

and someting multiplied with 0 is 0 ...

		float theta = 2 * 3.141f * ::getRandomMinMax( 0.0f, 1.0f );		float phi   = acos(2*::getRandomMinMax(0.0f, 1.0f) - 1);		float x = cos(theta) * sin(phi);		float y = sin(theta) * sin(phi);//::getRandomMinMax( -1.0f, 1.0f );		float z = sin(phi);

but with your formula it looks like

##### Share on other sites
taby    1265
I'm not even sure what you're doing, or if you're implying that things are "working" yet, or not.

If you are indeed attempting to generate a set of random points along the surface of a unit sphere, then the problem is not with the formula that I gave you. It works, but perhaps for not what you're needing?

The theta/phi generates a good distribution of radian angles, and the x/y/z code just simply converts those to Cartesian coordinates.

Can you post the code for the working 2D analogy so that I can extrapolate from that instead?

##### Share on other sites
taby    1265
If you ever get sick of hand-typing the (approximated) value of pi, it can be generated at runtime by pi = 4.0*atan(1.0).

Marcel Luttgens from sci.physics.relativity showed me this one. He's full of neat and useful math "tricks" such as this. jyk is also very good at these kinds of problems. It's like these guys were doing trigonometry in the womb!

##### Share on other sites
daftasbrush    252
Unless you HAVE to use angles... there's an easier way.

Just generate a random vector (say between -1 to +1 on each axis)
Then Normalize it.
// without a vector classfloat x = ::getRandomMinMax( -1.0f, 1.0f );float y = ::getRandomMinMax( -1.0f, 1.0f );float z = ::getRandomMinMax( -1.0f, 1.0f );float l = sqrt(x*x+y*y+z*z)x = x / ly = y / lz = z / l

edit: just noticed similar is mentioned in the mathworld link.
HTH

##### Share on other sites
alvaro    21246
Quote:
 Original post by daftasbrushUnless you HAVE to use angles... there's an easier way.Just generate a random vector (say between -1 to +1 on each axis)Then Normalize it.*** Source Snippet Removed ***edit: just noticed similar is mentioned in the mathworld link.HTH

This will not give you a uniform distribution. The parts of the sphere that point at the corners of the [-1,+1]^3 box will be picked more often than the rest.

  double x,y,z,l2;  do {    x=random_double(-1.0,1.0);    y=random_double(-1.0,1.0);    z=random_double(-1.0,1.0);    l2=x*x+y*y+z*z;  } while(l2>1.0);  double l=sqrt(l2);  x/=l;  y/=l;  z/=l;

##### Share on other sites
Crisium    100
vector with a radius of 1 ... easy just normalize it

Peter Wraae Marino

##### Share on other sites
taby    1265
Although everyone's trying to be helpful, almost every post after mine could have been avoided by FOLLOWING THE THREAD. It was made specifically clear that if you use a linear distribution with either Spherical-Polar or Cartesian coordinates YOU WILL GET BUNCHING.

How many times do Alvaro and I have to repeat this for it to sink in? There is ONLY ONE WAY TO GET A GOOD DISTRIBUTION OF RANDOM POINTS ON A SPHERE, AND THAT IS TO USE A NON-LINEAR DISTRIBUTION OF INPUT PARAMETERS.

If you read the contents of the thread before posting random spew, you guys might actually learn something that applies to reality.

Perhaps it will help to visualize this very difficult concept:

A SPHERE IS NOT A CUBE. I'll assume that this will take most of you a while to digest, since you haven't figured it out in the tens of years that you've been alive so far.

##### Share on other sites
taby    1265
Quote:
 Original post by Crisiumvector with a radius of 1 ... easy just normalize itPeter Wraae Marino

Very handy.

##### Share on other sites
daftasbrush    252
@taby (mainly)
Alright, KYHO!

The OP's actual question
Quote:
 how can i make a vector with radius 1 point to a sphere ??
makes no reference to creating a uniform random distribution on a sphere...

OP seems to be having enough difficulty getting points to map even vaguely to a sphere, let alone be worried about any skewing of the distribution.

I don't know what he want's to achieve ultimately, but from what he's posted, so far, I think he'd be happy if 2 succesive points actually landed on the sphere.

So let's get him a sphere, first.
And worry about the distribution later, but only if he's worried about it.

It's one thing to "RANT" at people posting things that are definitely worong, or don't answer the OP's question....
It's completely another to have a go at folk, for not answering YOUR idea of what the OP asked.

@Ben091986
How's it going.... have you found any of the above useful at all??

##### Share on other sites
taby    1265
Quote:
 Original post by daftasbrush@Ben091986How's it going.... have you found any of the above useful at all??

The OP's original question makes absolutely no sense whatsoever. So, my first post ASKS:
"It seems like you would be trying to make a random point on the sphere?"

Notice the question mark. I assumed that since the OP did not say "no", but went ahead and implemented what I suggested, that they were indeed trying to make a random point on the surface of a sphere.

Did you look at the screenshot that they provided? It kind of looks like a bunch of randomly generated points on a sphere... but incorrectly generated. Alvaro appears to have also concluded the same thing.

My next solution to get to the heart of the matter was to ask the OP for their working 2D version, so that I could extrapolate to 3D without worrying about German-English translation issues (or annoying people who are simply giving the same "solution" which failed for the OP in the first place). Exactly what were you trying to achieve anyway? In one sentence you say that the question is vague, and yet you give an answer. So you're no "better" than I.

[Edited by - taby on March 25, 2008 11:55:08 PM]

##### Share on other sites
Ben1986    122
well unfortunately my pictures are gone, but to make things clear

I have a particle system based upon policy classes, and now i am at the point where formulas are needed.

My Standard Particle Struct has a
position
velocity (speed)
acceleration (increasement for speed)

so far so good -

for an implosision effect i want to start 300 particles from the same position but with randomly velocity (so that they go away from the center) and if i can normalize that speed to a vector pointing to a sphere radius 1 point, it will looking really smooth.

With accelleration as the starting speed multiplied by -1 they will get slower with time. at a point they will stop & go back so that they go to the center again.

if i loop that animation it looks like . o O and then O o .

you understand?
but if starting speed is wrong it just does not look like a sphere.

as i mentioned policy classes, i am able to change the particle updater from a "all spawn at same time" to "spawn x particle every second" and it looks good in 2d.

Sorry for my vague starting question. and please do not abuse that thread for any offtopic. I am thankful for every good explained hint/solution

##### Share on other sites
alvaro    21246
Quote:
 Original post by Ben1986for an implosision effect i want to start 300 particles from the same position but with randomly velocity (so that they go away from the center) and if i can normalize that speed to a vector pointing to a sphere radius 1 point, it will looking really smooth.

Sorry, you didn't make sense this time either.

##### Share on other sites
Emergent    982
Quote:
 Original post by Ben1986if i can normalize that speed to a vector pointing to a sphere radius 1 point, it will looking really smooth.

Agreed with avlero; this sentence doesn't make sense. But I think I know what you mean:

You want to create an "explosion" of particles from a point. To do this, you want to initialize many particles at this point, with random velocity vectors uniformly distributed on the unit sphere.

That is, if your "explosion" is supposed to happen at a point P=[x,y,z]', and you want to create N particles, then for every i=1,2,...,N you want,
Ri(0) = P
Vi(0) = random_unit_vector()
where Ri(0) and Vi(0) are the initial position and velocity, respectively, of the ith particle.

Is this what you are trying to achieve, Ben?

If this is the case, then taby's post at the beginning of this thread tells you what you need to know. You can get a random unit vector using many methods, but taby's solution is best as it does not prefer one direction over any other.

Cheers.

EDIT:

I see now that you actually want an "explosion followed by implosion" effect, but the above still holds. Just choose an acceleration like (for instance),

(d/dt)Vi(t) = -k*Ri(t)

where k is some positive constant.

Physically, this will be equivalent to attaching all of your particles to P with springs. Naturally, you'll want to stop your animation when,

Ri(t) < epsilon
AND
dot(Ri(t)-P, Vi(t)) < 0

for some i (it doesn't matter which particle you look at). Otherwise, this system will oscillate. :-)
______