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:
- The plane which it intersects
- The x/y coordinates I should look up in my 2d array to get the height.
My current solution is this (using XNA):
- 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)
- 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?
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.