Convert vector3 direction to alpha/beta equivalent for ArcRotateCamera

Whenever I update the “upVector” for my ArcRotateCamera, the camera seems to be looking still at the same target, but from a different direction/angle. The original perceived direction can be restored by updating the alpha a beta properties manually with my mouse movement, however, I’m hoping to seamlessly update the upVector, alpha, and beta altogether so that after the upVector is changed, the camera is still facing in the same exact direction.

Is there a way to convert vector3 direction to alpha/beta equivalent for an ArcRotateCamera?

I don’t believe this is possible with the ArcRotateCamera as the camera is always pointing at a target location.

It might be easier to use one of the other types of cameras to create a similar type of functionality, but one that you can update with your own function based on the upvector. Just a thought.

Thanks, but maybe I wasn’t clear. I mean this respectfully: It can’t not be possible because the “camera is always pointing at a target” specifically.

I’m not looking have the camera face away from the target to a direction.

I’m looking to programmatically update the alpha and beta so the camera rotates around the target to the right point where the desired camera forward direction is achieved.

If I can’t find a way to do that, however, I appreciate your idea and will explore that route. Thank you!

Ah I see. Thanks for the clarification.

You can definitely lerp between the alpha and beta angles easily, but I see that the tricky part here would be to determine the correct alpha and beta angles based on a provided vector.

We’re quickly out of my Math depth here, so maybe @Evgeni_Popov can lend us his thoughts?

I don’t know if it’s what you need, but the setPosition() method of the ArcRotateCamera will compute the alpha/beta/radius parameter so that the camera is located at the new given position and still looks at the current target.

2 Likes

(hack)

For some strange reason, the alpha and beta won’t update if you give the camera the exact same position it had right before you set the upVector. However, if you slightly change the position in any way, the alpha and beta will be recalculated How strange.

Thanks @Evgeni_Popov and @PirateJC for helping me through this.

        //does NOT work
        const resetPosition = camera.position.clone();
        camera.upVector = upwards;
        camera.position = resetPosition;

        //does NOT work
        const resetPosition = camera.position.add(new Vector3(0,0,0));
        camera.upVector = upwards;
        camera.position = resetPosition;

        //WORKS
        const resetPosition = camera.position.add(new Vector3(0.0000001,0,0));
        camera.upVector = upwards;
        camera.position = resetPosition;
1 Like

You need the hack because the setPosition method does not do anything if the new position is the same than the old one:

public setPosition(position: Vector3): void {
    if (this._position.equals(position)) {
        return;
    }
    this._position.copyFrom(position);

    this.rebuildAnglesAndRadius();
}

To avoid your hack, what you can do is calling rebuildAnglesAndRadius(). So, this code should work:

camera.upVector = upwards;
camera.rebuildAnglesAndRadius();
1 Like

Works like a dream thank you very much!