After experimenting with Matrix to determine the correct orientation, I found that the results were a bit peculiar. Switching to a spherical coordinates system proved to be a more effective approach.
To begin, we must obtain the coordinates of two points on the sphere: one being the point on the sphere surface closest to the camera (denoted as P), and the other being the point where we clicked on the sphere surface (denoted as Q).
We utilized raycaster
to acquire the Cartesian Coordinates of points P and Q, which were then converted to Spherical Coordinates denoted as (r,θ,φ).
Next, we calculated the angular displacement from point Q to point P and incorporated this displacement into the sphere's rotation.
The following code snippet demonstrates this process:
// Code snippet omitted for brevity
At the conclusion, an animation library was leveraged to ensure a smooth rotation.
To address previous issues related to incorrect rotations when tilting the earth up and down, adjustments were made in the OnDocumentClick
function. By incorporating Sphere matrix calculations and conversion of world space axises to object space coordinates, we achieved the desired rotation outcome.
The modified function is outlined below:
// Code snippet for updated function omitted for brevity
An updated demonstration showcasing these enhancements can be viewed here: rotate earth new version.