Rotating a FreeCamera around a point in space programatically

Hi again! :slight_smile:

I’d very much like to rotate my FreeCamera (not ArcRotateCamera) around a point in space programatically, so in scene.beforeRender, not using an animation.

Let’s say the point is 0, 0, 0 and the camera is at position 0, 50, -55.

Anybody willing to help me out with the math please?

camera.setTarget(Vector3.Zero());
scene.onBeforeRenderObservable.add(() => {
    // pls mr.camera rotate around the target >:(
});

Thanks and much love!

demo created for you :slight_smile:
https://www.babylonjs-playground.com/#HM51UZ

considering only x and z rotation arround a mesh

2 Likes

Genius, a literal god, magnificent.

Thanks a lot! Works perfectly :slight_smile:

1 Like

Hmmmm, what if I wanted to rotate the camera around a different point other than 0, 0, 0?

https://www.babylonjs-playground.com/#HM51UZ#1

Don’t know why this does not work? maybe another godess can help you! :slight_smile:
I suppos that setTarget needs a relative vector?

https://www.babylonjs-playground.com/#HM51UZ#2

Strange things happens when vectors are assigned to the mesh.position or camera.target positions.
If you use directly the position of the box for the target the code works.

2 Likes

Thanks! Now it all works :slight_smile: