Rotate camera to point on sphere

How do I compute ArcCamera rotation to look at point on sphere?

cc @PolygonalSun who might have any clue ?

1 Like

I’m not 100% sure that I know what you’re asking but I’m assuming that you want to take a given point on the sphere and rotate your ArcRotateCamera to look at that point. The easiest way that comes to mind would be to take value from your vecToLocal function and subtract your camera’s target to get your direction. From there, you would want to normalize your given vector, multiply the vector by your camera’s radius value, and add the camera’s target as an offset. Then, all you need to do is to just use camera.setPosition(your new vector) and it should adjust your alpha and beta values to look at the desired point.

const lookAtPoint = (camera, point) => {
    const direction = BABYLON.Vector3.Normalize(point.subtract(camera.target));
    const newPosition = direction.multiplyByFloats(camera.radius, camera.radius, camera.radius).add(camera.target);
    camera.setPosition(newPosition);
}

You could also just feed your direction value that you used in the building of your ray as the value for point above. I don’t know if this answers your question but hopefully it’s a step in the right direction.

2 Likes

Hi, thanks for the quick reply. Using your provided function and a second camera I was able to accomplish the goal. I wanted to animate the camera alpha and beta between its current location and the point on sphere. I guess my real question is as follows: How would I calculate the new alpha and beta without actually moving the 2nd camera and getting updated values? Here’s a revised playground with second camera: https://playground.babylonjs.com/#SRZRWV#1251

If you just want the values, you could probably accomplish this in by first getting the new position (minus the target’s position). If you look at my previous post, you can use the same math for newDirection and omit the .add(camera.target). From there, it’d just be some trigonometry to figure out the alpha and beta values:

// assuming you have some value called newDirection as described above
camera.alpha = Math.acos(newDirection.x / Math.sqrt(Math.pow(newDirection.x, 2) + Math.pow(newDirection.z, 2)));
camera.beta = Math.acos(newDirection.y / camera.radius);

This is pretty much how we do it in our rebuildAnglesAndRadius function in the ArcRotateCamera code.