# Rotating a 3D sphere according to geolocation

Hi all,

I’ve been searching the forum for an answer, but I really didn’t manage to pull it of.
My challenge is as follows: I have a 3D sphere with an earth map and I would like to be able to rotate the sphere just so that a given geolocation (longitude & latitude) will be centered.

If I try to rotate the sphere with -60.0f latitude and 0.0f longitude it works (http://sht.tl/aMw), this also applies when I try 0.0f latitude and -30.0f longitude (http://sht.tl/KVcP). But when I try to combine them and try -60.0f latitude and -30.0f it doesn’t work anymore (http://sht.tl/qbLst).

I was already expecting this to happen, but I really can’t think of a way to calculate the correct rotations for both axes.

This is the code where I draw my sphere:

``````

ofPushMatrix();
ofTranslate(current_position.x, current_position.y);

ofPushMatrix();
ofRotateY( -lat );
ofRotateX( -90 - lon );

glPushMatrix();
glEnable(GL_DEPTH_TEST);

glDisable(GL_DEPTH_TEST);
glPopMatrix();

ofPopMatrix();

ofSetColor(255,0,0,140);
ofCircle(0,0,5);

ofPopMatrix();

``````

Is this a very complicated math problem?

I figured it out, wasn’t that hard after all.

``````
ofPushMatrix();
glTranslatef(current_position.x, current_position.y, 0);

ofRotateY( -current_lat );
ofRotateX( -current_lon );

ofPushMatrix();
glEnable(GL_DEPTH_TEST);