Pan but keep object in centre of camera

Hi All

Is it possible to use a perspective camera and OrbitControls to do a pan (right click mouse) but keep the object in the centre of the camera (user’s view)? In this way, the object will appear not to be moving but its perspective will change. A normal pan moves the target from the centre of the camera which I don’t want to do. I’m trying to create a situation where the left mouse rotates the camera around the object (normal behaviour) and the right mouse does a pan (to create a perspective angle of the object)… but object should remain fixed in the centre of the view for both rotation and pan. To the user, the object appears to stay in a fixed position the whole time.

Can this be done programatically perhaps? Maybe via a matrix / transformation? Any other solutions to consider?

Thanks in advance… any creative ideas to solve this one would be appreciated. :pray:

Quick clarification - OrbitControls don’t move objects. They move only the camera (so for ex. object that was in the scene centre will remain in position 0,0,0 after panning.)

Yep.

(1) OrbitControls offer you 2 events you can listen to - change and end (also start, but it won’t matter now.)

(2) OrbitControls save the center point of the camera is looking at in a target property.

So, combining both of those, you can simply set the mesh position to a new target position whenever a change event occurs:

controls.addEventListener('change', () => {
  mesh.position.copy(controls.target.clone());
});

Here’s a small example.

@mjurczyk thank you for the quick reply.

This is almost what I’m looking for… I switched off the autorotate on your example and I can see the cube stays in the centre of the camera on pan which is great and half the problem solved. :+1:

However, what is still needed is when I pan (right-click) the cube itself should still behave as though the pan was taking place… so the net effect of the pan (perspective change of the cube) should be apparent on the cube as I right-click… but at the moment, the cube does not change at all (likely because the camera is obviously not moving like a normal pan would).

Does that make sense?

So it is possible to apply the pan effect (calculated?) to the cube even though the camera is not moving to the left or right? I know… tricky one… but hoping it is possible in some way.

Thank you for you input here as its already helpful…

Please read my reply again. Camera is moving, in my example as well. The only difference my example makes is - we move the cube WITH the camera ( so after camera is panned, we align cube to be once again in the centre.)

What you need to do is to raycast and test whether the cursor is over the cube. If it is - just disable the re-aligning with the camera.

1 Like