• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Gumgo

Generating random points uniformly distributed over a section of a sphere

6 posts in this topic

I need some way of generating points on the surface of a sphere (actually, vectors, but if I have points on the surface I have vectors) that are uniformly distributed on a "circular section" of the sphere's surface. This diagram should explain what I mean: The diagram is in 2d, but the same applies in 3d. The blue line is the vector given. Theta is the "threshold angle" - the randomly generated vectors are allowed to form an angle of theta or less with the given vector. The red part is the surface of the sphere (in the image, the arc of the circle) that the points are allowed to be on, and must be uniformly distributed on that area. I have one current solution, but it only works for the vector (0,0,1):
theta = fRand( 2*M_PI );
tMin = cos( randomAngle * M_PI_180 ); - randomAngle is the "threshold angle"
t = fRand( tMin, 1 );
phi = acos( t );
sinPhi = sin( phi );
return Vector3( sinPhi * cos( theta ), sinPhi * sin( theta ), cos( phi ) );
Also, it uses a lot of trig functions. Is there any more efficient ways of doing this that work with any vector? Thanks.
0

Share this post


Link to post
Share on other sites
Gumgo,

I'm not sure what you mean by uniformly distributed, but I'm assuming you mean that the point on the surface of the sphere that is randomly generated is the same distance in longitude as it is latitude away from the starting vector.

In either event, I think the best way to go about solving this is to use spherical coordinates.

Spherical coordinates represent a point on a sphere by the ordered pair:
(r, theta, phi)

Where r is the radius, theta is the angle on the YZ-plane (latitude), and phi is the angle on the XY-plane (longitude). To solve this you only need to convert from cartesian coordinates to spherical, add in your random angle, and then convert back into cartesian coordinates.

The equations to convert from cartesian to spherical as follows:

r = sqrt( x^2 + y^2 + z^2)
theta = arccos( z / r )
phi = atan2(y,x)

Now, it's relatively easy to normalize your vectors first, however as you can see above it's not necessary, as the calculations are part of the conversion. But, in the event you're working with a unit circle, r = 1, which means theta is just the arccos(z).

So simplified:

r = 1
theta = arccos( z )
phi = atan2( y, x )

This gives you theta and phi, which is the position of your existing vector in spherical coordinates. Now, compute your random angle with something like

angle = random(-threshold, threshold)

This'll give you a random angle that lies in between the threshold. Now, just add the random angle to your existing spherical coordinates.

theta1 = theta + angle
phi1 = phi + angle

Now convert back to cartesian coordinates using the following:

x = r * sin(theta1) * cos(phi1)
y = r * sin(theta1) * sin(phi1)
z = r * cos(theta1)

As you can see, this is again greatly simplified if you're using a unit circle, as r = 1, however either way it works, and you can cut down on the number of computations by caching intermediate calculations. So the total method would look something like.

float angle = fRand(-threshold, threshold);
float r = sqrt( x^2 + y^2 + z^2);
float theta = acos( z / r ) + angle;
float phi = atan2(y, x) + angle;
float rSinTheta = r * sin(theta);
x = rSintheta * cos(phi);
y = rSinTheta * sin(phi);
z = r * cos(theta);

This requires:
1 square root
1 acos
1 atan2
2 cos
2 sin

If you're using a unit circle (r=1), the above is reduced to:

float angle = fRand(-threshold, threshold);
float theta = acos( z ) + angle;
float phi = atan2(y, x) + angle;
float sinTheta = sin(theta);
x = sinTheta * cos(phi);
y = sinTheta * sin(phi);
z = cos(theta);

Either way this method uses 2x cos, 2x sin, 1x acos, and 1x atan2.

Well, I hope this has been helpful. It's not really any more efficient than your method, but it's straight forward, easy to read, and should work on all 3 axis, regardless of the starting vector.

Cheers and Good Luck!
0

Share this post


Link to post
Share on other sites
Quote:
Original post by Gumgo
[...]
I have one current solution, but it only works for the vector (0,0,1):

theta = fRand( 2*M_PI );
tMin = cos( randomAngle * M_PI_180 ); - randomAngle is the "threshold angle"
t = fRand( tMin, 1 );
phi = acos( t );
sinPhi = sin( phi );
return Vector3( sinPhi * cos( theta ), sinPhi * sin( theta ), cos( phi ) );


I don't think that distribution is uniform. [EDIT: Oh, actually it is. See Erik Rufelt's pseudo-code below for a version with fewer trigonometric operations.]


Quote:
Also, it uses a lot of trig functions. Is there any more efficient ways of doing this that work with any vector? Thanks.


I would concentrate on getting it right for (0,0,1) first. You can then apply a rotation to the result.

[Edited by - alvaro on January 7, 2010 11:54:36 AM]
0

Share this post


Link to post
Share on other sites
As you said, you can think of each point as a vector from the centre of the sphere to the edge. Each random point on the area on the surface can be specified by an angle made with the centre vector (the vector from the centre of the sphere to the centre of the circular area on the surtface), and a rotation around the centre point on the surface of the sphere.

If you just use a random value for each of these angles, the distibution is not uniform, as you'll tend to get more points towards the centre. The second value, the rotation around the centre vector can be an ordinary random value, but for the first angle - the angle made with the centre vector, you need to make the probability of picking a given value decrease proportionally to the circular area that angle describes on the surface.

For small angles, the area on the surface can be approximated to a flat circle, so the probability should just increase proportional to 1/(sqrt)r, where r is the distance from the centre point on the surface. If the angle is larger, then you'll have to take into account the curved surface - the formula for a circular area on a curved surface I don't know off the top of my head, but it shouldn't be too hard to find.

[Edited by - BattleMetalChris on January 7, 2010 9:11:34 AM]
0

Share this post


Link to post
Share on other sites
Because it's not perfectly obvious (and needs some short scrolling) in the article I linked to, here the quotes I ment:

Quote:

Uniform Distribution

A simple way to randomly (uniform) distribute points on sphere is called the "hypercube rejection method". To apply this to a unit cube at the origin, choose coordinates (x,y,z) each uniformly distributed on the interval [-1,1]. If the length of this vector is greater than 1 then reject it, otherwise normalise it and use it as a sample.

[...]

Orion Elenzil proposes that by choosing uniformly distributed polar coordinates theta (0 <= theta < 360) and phi (0 <= phi <= pi/2) but the using the sqrt(phi) results in points uniformly distributed on the surface of a hemisphere. If the poles lie along the z axis then the position on a unit hemisphere sphere is
x = cos(sqrt(phi)) cos(theta)
y = cos(sqrt(phi)) sin(theta)
z = sin(sqrt(phi))

A whole sphere is obtained by simply randomising the sign of z.

Both approaches can rather easily be adapted to work only on sections. (In the first by deleting those not matching the respective angles as well, in the second more direct by limiting the angles.)
0

Share this post


Link to post
Share on other sites
Quote:
Original post by alvaro
I would concentrate on getting it right for (0,0,1) first. You can then apply a rotation to the result.


I second this. It's easy to find uniformly distributed points on a sphere, so if you create a uniform distribution around 0,0,1, and then pick a random point on the sphere, and rotate all the points to center on that instead, you have a uniform distribution around that point.

So what you need to do is only to find uniformly distributed points on parts of the z > 0 hemisphere, where z > Z, and Z can be calculated from the acceptable angle from 0,0,1.

(There might be some mistake in the following, but I think it should be correct)

Uniform distribution of random points on a unit-sphere:
u = rand [-1, 1]
a = rand [0, 2pi]
x = sqrt(1 - u*u) * cos(a)
y = sqrt(1 - u*u) * sin(a)
z = u


For uniform distribution of points on part of the sphere where z > Z, you just need to pick u = rand [Z, 1], and the rest is exactly as above.

So the complete algorithm would be:
0. Calculate Z from the angle (a cos or sin)
1. pick N random points on z > Z
2. pick one random point on the sphere
3. rotate the points from step 1, as to align 0,0,1 on the random point chosen in 2


EDIT: I was thinking the points should be centered around a random point too.. if you already have that point you can skip step 2. =)
0

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  
Followers 0