Amazing problem

Now, I really want to have a world which is a sphere. I (obviously) spawned a sphere in my scene, and I created a box which represents a player.

I created a latitude/longitude to cartesian coordinate system, using this site as reference: http://www.codeproject.com/KB/graphics/Sphere.aspx

Now, as this player moves around on the globe, the players "legs" should obviously face the circle at all time, just as we walk around on our feet.

This can be done by altering the z(by -longitude) and the y(by the latitude) axis rotation of the players object, but using this approach, the same side of the player constantly faces the point (0, 0, height) //(latitude, longitude, height).

This is all cool and the player moves around perfectly, and everything is good, but I want to be able to alter the y axis, as this represents the players orientering - the way he's looking. (Instead of facing (0, 0, height), we should really face a never ending point.)



I know its possible to do it by altering the x and the z axis, but I've no idea what the real pattern is, and I used two days on this so far. xD (I'm really hate asking for help :/)



This is what I have so far:



I use this method for translating the coordinates.


   private void translateCoordinates(Spatial node) {
      float snt = FastMath.sin(longitude * FastMath.DEG_TO_RAD); //Sinus to theta.
      float cnt = FastMath.cos(longitude * FastMath.DEG_TO_RAD); //Cosinus to theta.
      float snp = FastMath.sin(latitude * FastMath.DEG_TO_RAD); //Sinus to phi.
      float cnp = FastMath.cos(latitude * FastMath.DEG_TO_RAD); //Cosinus to phi.
      float x = height * snt * cnp;
      float y = height * cnt;
      float z = -height * snt * snp;
      System.out.println("z: " + z);
      node.setLocalTranslation(new Vector3f(x, y, z));      
   }



And those methods for setting the rotation:

   /**
    * The nodes rotation should be modded when we change position.
    * @param node Reference to the scene node for which this "coordinate" applies.
    */
   private void rotate(Spatial node) {
      
      System.out.println("Latitude: " + latitude + " Longitude: " + longitude);

      node.setLocalRotation(new Quaternion().fromAngles(0, FastMath.DEG_TO_RAD * getXAngle(), FastMath.DEG_TO_RAD * getZAngle())); // {x-axis rotation, y rotation, z rotation} (in radians)

   }

   private float getZAngle() {

/*      
      if(latitude > 180) { //Doesn't work :s
         float mult = longitude / 90;
         float hax = latitude / mult;
         float start = longitude;
         return start + hax;
      } else {
         float mult = longitude / 90;
         float hax = latitude * mult;
         float start = -longitude;
         return start + hax;
      }*/
      
      if(latitude == 0) {
         return -longitude;
      //} else if(latitude == 45) { //Doesn't work either.
         //   return -longitude / 2;
      } else if(latitude == 90) {
         return 0;
      } else if(latitude == 180) {
         return longitude;
      } else if(latitude == 270) {
         return 0;
      }
      return 0;
   }

   private float getXAngle() {
      /*
       * The closer longitude is to 0 or 180, the less should
       * this method return.
       */
      if(latitude == 0) {
         return 0;
      } else if(latitude == 90) {
         return -longitude;
      } else if(latitude == 180) {
         return 0;
      } else if(latitude == 270) {
         return longitude;
      }
      return 0;
   }



The values in those methods works perfectly, but the obvious path spotted is wrong :(.

Thanks in advance!
- Mikkel