I am trying to understand the OrbitControls, the math behind it to somehow visualize this in my brain… I am confused again.
The camera is rotating around a target which is a Vector3, what I don’t understand when I rotate the camera is why the target changes as well, I thought that the target keeps the same values since only the camera position is changing.
If someone can explain this it will help me a lot to visualize this in my brain…
With orbitcontrols, manually rotating the camera via .rotation.y += 2 doesn’t work.
since OrbitControls will make the camera point back at controls.target on its next update.
However… changing the camera.position Will work, and after you move the camera, on the next orbitcontrols.update() orbitcontrols reads the camera position, then enforces its constraints (min/maxDistance min/maxAzimuth / polar etc ). and then does camera.lookAt( controls.target );
So if you want to do your own stuff to the camera, your options are to disable orbitcontrols temporarily via orbitcontrols.enabled = false
or manually update your camera, and then adjust controls.target so that it won’t undo/mess up the change you did…
camera.position.y += 5;
controls.target.y += 5; //Will move the camera up without changing its rotation.
camera.rotation.y += 2;
camera.localToWorld(controls.target.set(0,0,-10)) //Will rotate the camera, And adjust controls.target to 10 units in front of the camera so that orbitcontrols won’t then re-orient the camera…
Or another way…
camera.worldToLocal( controls.target ); //temporarily convert controls.target to camera local space
camera.rotation.y += 2;
camera.updateMatrix(); //not sure if required…
camera.localToWorld( controls.target ) //Convert controls.target back to world space with new camera transform
Usually tho… I know when I will need the camera to behave differently, and I’ll just disable orbitcontrols if i need radically different behavior…
and switch to my own custom camera controller… Or make some kind of hybrid controller with another instance of OrbitControls and switch between them by toggling .enabled.
Hope this helps?
Yes, it helps I kinda knew this what I am confused about is why the target it’s changing its values when rotating the camera using the controls, I thought this was a fixed point, or better said it would keep its initial values…
About this “Usually tho… I know when I will need the camera to behave differently, and I’ll just disable orbit controls if I need radically different behavior…”, when enabling the controls it will mess up the camera again so how do I update the controls after changing the camera position while the controls are disabled?