How to prevent a camera`s target getting out of cube?

In my project I use ArcRotateCamera and also I have 50 meshes and a big cube, that is a parent for those 50 meshes. So I need to prevent my cameras target from getting out of the cube. Is there any possibility to do this? I tried to change the targets position when its out of the cube, but then I have a visual problem, my camera is jumping. I know why but I don`t need this “side effect”.

Welcome aboard!

Are you able to share your code in the Playground?

It will be a lot easier for people to help you.

Maybe one thing you can try is enabling collisions?

1 Like

cc @PolygonalSun just in case you have experienced this already

For this, I guess it would be important to know how you have things set up. For example, how are you assigning/updating your camera’s target? Also, what do you mean when you say your camera is “jumping”? Is it erratically moving, teleporting, or is it being pushed in a specific direction? The easiest way for us to provide insight into what’s going on would be to see it in action in a playground or at least to see a code snippet so if you could post something in the thread, that would help us to know what’s going on.

1 Like

Here is my code on PG https://playground.babylonjs.com/#ZSQ5DZ#147
A grey sphere in the center of a cube is my cameras target. An almost transparent cube is a parent for whole red spheres that inside it. About "jumping camera", cameras target is always in the center of the camera, but when I move the target like on 200 px to the right, its not in the center anymore, thats way the camera chages its position and thats way I have this 'jumping" effect. But its not my main issue. My main issue is to prevent the traget to getting out of parent cube while i`m zooming or panning.

Hey! Sorry we didn’t see this before. @PolygonalSun do you know any nice ways to keep the camera’s target constrained inside an area?

EDIT: Oh, I just saw another answer of mine about a very similar situation that could be useful here: Restrict camera position and apply elastic stop at edges - #2 by carolhmj. It concerns the camera’s position, but you could modify it to apply to the target instead.

It might be odd to say and not what you are looking for but…Your targets are meshes, is it? And you want to constrain your meshes inside the cube, right? Then why not just enable and check the collision with your cube… Something like this, may be:

Edit: Except you would need to invert the normals on the cube and remove your trick for repositioning the sphere outside of bounding box.