# Cube to Sphere (Need help) : Update

## Recommended Posts

Deftoned    115
Hey gamedevers, I am currently trying to map a cube to sphere by the method described on http://mathproofs.blogspot.com/2005/07/mapping-cube-to-sphere.html unfortunately my maths is kind of dodgy at best and when it is saying using a unit cube from -1 to 1, is it possible to go from say -m_patchSize to m_patchSize or 0 to m_patchSize, i don't quite understand it, m_patchSize is the size of the side of a cube i am drawing. Or do i have to do it from -1 to 1 and scale it later on in the equation? Thanks for any help guys. PS. Sorry here is the code i am using. With this equation X seems to get some correct values, but Y and Z all i get is -1.#INF
Vector3 FractalMesh::SpherizePoint(Vector3 point)
{
//====================================================================
//Maths refernced from
//http://mathproofs.blogspot.com/2005/07/mapping-cube-to-sphere.html
//====================================================================

float x = point.getX();
float y = point.getY();
float z = point.getZ();
float xPrime, yPrime, zPrime;
float yzp, zxp, xyp;

//cout << "In X: "  << x << " Y: " << y << " Z: " << z << endl;

//Mapping the point to a sphere
//Getting the altered X Y and Z co-ordinates

xp = (x * x) / 2;
yp= (y * y) / 2;
zp = (z * z) / 2;

yzp = ((y * y) * (z * z)) / 3;
zxp = ((z * z) * (x * x)) / 3;
xyp = ((x * x) * (y * y)) / 3;

xPrime = x * (sqrt(1 - (yp - zp + yzp)));

yPrime = y * (sqrt(1 - (zp - xp + zxp)));

zPrime = z * (sqrt( 1 - (xp - yp + xyp)));

point.setX(x);
point.setY(y);
point.setZ(z);

return point;
}


UPDATE: Now i have the values working now so that it is a plane, but the offset to make it bluge out into a sphere is now my problem, i have tried passing in a radius from -1 to 1, but it doesn't quite work , it gives me a diagonal curve...sort of... [Edited by - Deftoned on August 17, 2008 2:25:15 AM]

##### Share on other sites
Hi,

how I do it is actually very simple, and I did it very quickly once i worked it out. First you need a tesslated cube. Mine is made of 6 vertex grids. How the grids get into cube form does not really matter, although I would reccommend using a numerically accurate method.

Once you have the grid the point on the sphere can be computed in several ways. My algorithm works like this:

For Each Grid    For Each Point On the Grid        Compute The Vector from the Center of the Cube to the Point        normalize the vector into a variable (say norm for example)        set the spherical vertex position to be vpos = norm * sphere_radius;    next pointnext grid

this method works fine for me ... in fact I don't even multiply by the sphere radius and
instead use the World Transform Matrix to set the scale when rendering. Hope this helps.