This question already has an answer here:
I'm implementing a touch screen interface whereby the user dragging their finger on the screen will rotate the camera in a sphere around the central point.
My code works well for the horizontal swipe (rotation around the y axis), but only covers a smaller angle from the vertical swipe (rotation around the x axis) instead of the full 360. I used the article - Spherical coordinate system form Wikipedia (http://en.wikipedia.org/wiki/Spherical_coordinate_system), specifically the Cartesian Coordinates section, to do this. I've posted my code below. Can anyone help get this working through the full 360 degrees on both axis? See how Google Earth works when you swipe the globe for what I want.
This must have been done a billion times, but Google searches confuse with the phone built in camera, gps and the like. Thanks!
//Get current position oldX = getPosition().x; oldY = getPosition().y; oldZ = getPosition().z; //convert to inclination/azimuth camRadius = Math.sqrt((oldX * oldX) + (oldY * oldY) + (oldZ * oldZ)); inclinationTheta = Math.acos(oldZ / camRadius); azimuthPhi = Math.atan2(oldY,oldX); . . . //use touch screen changes (deltaTouchX and Y) inclinationTheta = (inclinationTheta + deltaTouchScreenX) % (2 * pi); azimuthPhi = (azimuthPhi + deltaTouchScreenY) % (2 * pi); //get new co-ordinates newX = camRadius * Math.sin(inclinationTheta) * Math.cos(azimuthPhi); newY = camRadius * Math.sin(inclinationTheta) * Math.sin(azimuthPhi); newZ = camRadius * Math.cos(inclinationTheta); //set new position setPosition(newX, newY, newZ);