EDIT: I may not have clearly explained why I'm having so much trouble. Working out longitude when the north pole of my planet is straight up in my view. ie x = 0, y = 1, z = 0 works fine using the code below. The problem is that the ATAN2(x,z) function doesn't work when the north pole of my planet is rotated away from straight up. ie to x = 0.6, y = 0.26, z = 0.75 (see the forth screen shot).
I'm sure that I need to use the north vector and the forward vector together in a complex trig calculation to work out what the longitude of my pointer is. My pointer is fixed at x = 0, y = 0, z = 1.
Is there anyone out there with the trig skills to help me out?
I'm working on Android Studio Java OpenGL 2.0.
I need the user to be able to select the Latitude and Longitude of a planet by rotating the planet beneath a stationary pointer.
I have worked out the Latitude component, but the Longitude is not working.
Code below is the current calculation:
static String getLocationString(){
String text;
float top = (OrbitDraw.northX * 0f) + (OrbitDraw.northY * 0f) + (OrbitDraw.northZ * 1f);
float bottom = (float)(Math.sqrt((OrbitDraw.northX * OrbitDraw.northX) + (OrbitDraw.northY * OrbitDraw.northY) + (OrbitDraw.northZ * OrbitDraw.northZ)) * Math.sqrt((0 * 0) + (0 * 0) + (1 * 1)));
int latitude;
latitude = 90 - (int)Math.toDegrees(Math.acos(top/bottom));
if (latitude >= 0) {
text = String.valueOf(Math.abs(latitude)) + "N";
} else {
text = String.valueOf(Math.abs(latitude)) + "S";
}
float longitude = (float)Math.toDegrees(Math.atan2(OrbitDraw.forwardX,OrbitDraw.forwardZ));
if (longitude < 0) {
text += " / " + String.valueOf((int) -longitude) + "E";
} else {
text += " / " + String.valueOf((int) longitude) + "W";
}
return text;
}
The following screen shots are the results, and should explain my problem:
No rotation applied. Longitude works
Rotated East. North Pole still up. All good.
Rotated West. North Pole still up. Also good.
North Pole now shifted. Pointer should be in the West. Here my math fails
Pointer close to South Pole. Should be in the East. Math fails
The Screen shots contain the Matrix rotation vectors. The information is grabbed using the following code:
rightX = mAccumulatedRotation[0];
rightY = mAccumulatedRotation[1];
rightZ = mAccumulatedRotation[2];
northX = mAccumulatedRotation[4];
northY = mAccumulatedRotation[5];
northZ = mAccumulatedRotation[6];
forwardX = mAccumulatedRotation[8];
forwardY = mAccumulatedRotation[9];
forwardZ = mAccumulatedRotation[10];
And the following code is what is used to rotate the planet using swipe gestures:
// tanslate and scale the model matrix
Matrix.setIdentityM(SolarSystemObjects.mPlanetModelMatrix, 0);
Matrix.translateM(SolarSystemObjects.mPlanetModelMatrix, 0, objectX, objectY, objectZ);
Matrix.scaleM(SolarSystemObjects.mPlanetModelMatrix, 0, planetScale, planetScale, planetScaleZ);
// Set a matrix that contains the current rotation.
Matrix.setIdentityM(mCurrentRotation, 0);
Matrix.rotateM(mCurrentRotation, 0, planetRotationX, 0f, 1f, 0f);
Matrix.rotateM(mCurrentRotation, 0, planetRotationY, 1f, 0f, 0f);
// rotation to the result.
Matrix.multiplyMM(mTemporaryMatrix, 0, mCurrentRotation, 0, mAccumulatedRotation, 0);
System.arraycopy(mTemporaryMatrix, 0, mAccumulatedRotation, 0, 16);
// Rotate the planet taking the overall rotation into account.
Matrix.multiplyMM(mTemporaryMatrix, 0, SolarSystemObjects.mPlanetModelMatrix, 0, mAccumulatedRotation, 0);
System.arraycopy(mTemporaryMatrix, 0, SolarSystemObjects.mPlanetModelMatrix, 0, 16);
Renderer.drawTriangles(SolarSystemObjects.mPlanetPositions, SolarSystemObjects.mPlanetColors[GameData.playerSolarSystemOrbit], SolarSystemObjects.mPlanetNormals, SolarSystemObjects.mPlanetTextureCoordinates, SolarSystemObjects.mPlanetTextureDataHandle, SolarSystemObjects.mPlanetModelMatrix, 6 * SolarSystemObjects.planetBlockCount);
I can tell that the 3 vectors (right, north, forward) contain all the data required to work it out, but my math is just not good enough.
Some pseudo code, or excel formulas would really help me out!
Thank You!