Help Attaching a Perspective Camera with Orbit Controls on a Moving Object

hello threejs!

I’ve spent a few hours trying to best to “attach” a camera and orbit controls to a moving object. I have seen a few similar posts, but none seem to work for me.

I imagine the camera and orbit controls, attached to the rocket, will circle a planet and seem as if the rocket is still and the planet is moving too and from the object.

I have the “up” vector with respect to the rocket’s perspective and think the camera should also share this up vector, stay with the object, and maintain orbit controls.

I have tried a few ways, including moving and rotating all the objects in the scene so my rocket stays at world (0,0,0), using a second dummy camera, and attaching the camera to my rocket. None seem to work.

If there is any simplifications for my problem, the rocket and planet will stay in a 2D plane. In the example below, they stay in the ZY plane.

Any help will be greatly appreciated. Thanks!


demo:

Actually think I got it. Although, I’m not sure if it’s the prettiest solution.

This example from three-mesh-bvh has a simple and sleek solution to this

1 Like

this is cool and great example. I wonder if the camera/controls would still work apply if the character was upside compared to world space… Going to take a deeper look later. thanks!

I usually just do something like:

camera.position.sub(controls.target);

and:
controls.target.copy( theTargetObject.position ) //slightly cheaper

or

theTargetObject.parent.localToWorld(controls.target.copy( theTargetObject.position )) //more robust

then:
camera.position.add(controls.target);

before I call "controls.update()"

Things get weird if you try to actually parent the camera to things.

2 Likes