Reputation: 12403
I'm currently working on building a game which is on a planet, the way in which I'm planning to store the data is in 6 2dimensional arrays, which are heightmaps around the sphere (on the faces of a cube). The problem I have is this, given a normalised vector which points outwards from the centre of the sphere how can I determine these two things:
My current solution is this (using XNA):
A helpful constraint is that the sphere/cube system is around the origin.
So, the problem which needs solving is this: Given a direction vector, how do I determine where it intersects the surrounding cube. Using this result how do I then get the correct value in a 2D array which is "painted" on the face of this cube?
Upvotes: 2
Views: 2610
Reputation: 18826
# edges are called X_AXIS_POS, X_AXIS_NEG, Y_AXIS_POS, Y_AXIS_NEG, Z_AXIS_POS, Z_AXIS_NEG
if (x*x >= y*y) && (x*x >= z*z) :
return ( (x>0) ? X_AXIS_POS : X_AXIS_NEG, y/abs(x), z/abs(x))
if (y*y >= z*z) && (y*y >= x*x) :
return ( (y>0) ? Y_AXIS_POS : Y_AXIS_NEG, x/abs(y), z/abs(y))
return ( (z>0) ? Z_AXIS_POS : Z_AXIS_NEG, x/abs(z), y/abs(z))
Upvotes: 3
Reputation: 86306
If I'm understanding you correctly, you're trying to pull a pixel off the square and onto the surface of sphere. So I assume you've successfully found the pixel on the cube. Now you scale the vector so it's touching the sphere. So you have an x,y,z on the sphere.
Spheres are usually texture mapped from a sheet. You can either find a reference to how it's done in XNA (or look at the texture sheet and try to figure it out), or you can do this: make a texture sheet that has increasing red gradient along the x axis and increasing green gradient along y. Map into onto the sphere. See what happens.
Upvotes: 0
Reputation: 11878
Look at the magnitude of each of the 3 components of the direction. The one with the largest magnitude tells you which face of the cube you hit (and its sign tells you if it's the + or - face.)
The other two coordinates give you your 2D mapping values. We need to normalize them, though. If your XYZ direction has X as the highest magnitude, then your 2D face coordinates are just U=Y/X and V=Z/X. These both range from -1 to 1.
Be careful of flips from positive to negative sides, you may need to flip the 2D U and/or V values to match your coordinate system.
Upvotes: 7