views:

608

answers:

3

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:
1) The plane which it intersects
2) The x/y coordinates I should look up in my 2d array to get the height.

My current solution is this (using XNA):
1) Construct a ray pointing out from [0,0] along the direction vector supplied. Loop through each surface and do a ray/plane intersection (which is a method supplied by the XNA framework) to get the distance to the intersection point. Select the closest plane (shortest distance to intersection)
2) Take the 3D point, and convert it to a 2D point which can be used as an array lookup to find the radius (this is the bit I cannot work out the maths for, or find any references to through google).

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?

+7  A: 

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.

SPWorley
Aha, that sounds like a somewhat simpler technique than the one I'm already using!I'll have a go at implementing this as see what happens. Thanks!
Martin
A: 

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.

Nosredna
+3  A: 
# 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))
ilya n.
Not the simplest code I've ever seen ;)However thankyou, I now know exactly how it works :D
Martin