Weird things happening sometimes after detaching and reattaching camera controls

I’m not sure why this happens but basically in my app, when user chooses an option, i stop autorotation and also all the camera rotation etc controls like this:

camera.useAutoRotationBehavior = false;
camera.inputs.attached['pointers'].detachControl(canvas);

and when button is clicked again, I bring them back:

camera.useAutoRotationBehavior = true;
camera.inputs.attached['pointers'].attachControl(canvas);

what is weird is that often, most times it all works well. But sometimes when coming back, the camera rotation and controls just won’t work well, when I try to rotate, it will zoom instead, and very weird things will happen, and the only way to make things right again is to refresh the entire app

any tips are very welcome :slight_smile:

Sounds odd. Can you give us a playground with this problem in it? Playgrounds are always WAY more helpful in trying to help debug issues.

Thanks!

1 Like

@PirateJC I would love to and I have already created a few playgrounds in questions I have posted and I do it whenever is possible. But in this case my react app has grown really large by now and its simply not possible to extract the real scenario I have with all its complexity. But anyway I will see if i can create a small simple playground just with the basic part that I am describing, I doubt though that the same behaviour will happen. I also understand that for you is very difficult to help me if i don’t have a playground for this specific case, I totally understand that, definitely (I guess i was just posting this question in case any of you had seen similar issues happening and maybe had some quick suggestion to try something but I get it that this it not the case)

1 Like

It’s a tough “Chicken and Egg” problem because we also totally get that it’s not always easy to repro things in the playground…which then makes it harder to help. hahaha.

Thanks for giving it a shot to try and create a playground for us. The behavior you’re experiencing definitely doesn’t sound normal. Sorry I can’t be more immediately helpful.

1 Like

Hey @javismiles I went through number of problems since I started using babylonjs last week. Event though it might seem impossible to reproduce, I would encourage you to try. Reproducing actual scenario in isolation might really help to identify the actual issue with the code.

It doesn’t have to be playground. I have started mocking babylon methods in my tests to make sure I am calling them as expected in strictly defined scenarios. It already helped few times not only to find the issue but also keeping me sane (as oppose to going through long list of console logs inspecting hundreds of properties :crazy_face: ).

Good luck! :smiley: …and please share if you find the problem.

1 Like

@PirateJC @MarkBevels thank you both :wink:
yes I agree with all you said. In this specific case though, I am having a lot of issues to reproduce the situation, it happens quite randomly. As an engineer I am familiar with this kind of complex pseudorandom events, so hard to track but as soon as I manage to isolate in better ways when the issue happens I will definitely try to create a playground, thank you for all the feedback and support, this is truly a super helpful community :slight_smile:

1 Like

I’m not sure you need to attach/detach camera controls all the time. If you have really complex inputs on a specific camera, may be you should try a 2 cams approach… and then yes the PG with just the basics would certainly help (us and you) find the answer.

I am also not sure I understand

Do you mean you simply want to freeze the camera on scene when opening i.e. a settings panel. Or is it so that the user can choose a number of options for the camera behavior?

@mawa I explain:

  • when user clicks button A, it will be able to draw on top of a model. For this I must stop camera autorotation, and also scene rotation with click and drag

that is the objective. Using two camera seems overkills to me (and not sure how to do it anyway, plus i just want to prevent the camera click behaviours so that user can draw on top of model, I dont want to change anything else regarding the camera). Attaching deattaching really works perfect. It’s just that sometimes i got those random weird things happening, but it happens pretty rarely i must say.

Just fixed a problem with my code where I was experiencing double “weird” behaviour. Started console logging to realise each time I save the file my console logs double on mouse press. It was related to unintended creation of additional subscriber on each webpack refresh which results in a race condition between handlers.
Instantly thought of your problem @javismiles. Probably not the case but thought of sharing above here in case you haven’t consider it.

1 Like

@MarkBevels yes this may well be the kind of things that is happening, hard to track but its a possibility, thank you for sharing Mark

@MarkBevels @mawa @PirateJC
Ive been testing more this issue because it keeps happening to me and pretty often. I have more info about it in case it suggests anything else.

  • Basically normally with an ArcRotateCamera like the one I have, clicking and dragging the mouse will rotate the view around the model.

  • When the problematic issue happens, suddenly clicking+dragging will zoom in and out of the scene, instead of rotating around the model, it will just zoom in and out, and the only way I find to reset it to the normal behaviour is to reload the entire app

  • These issues are definitely related to the use of:
    camera.inputs.attached[‘pointers’].detachControl(canvas);
    and camera.inputs.attached[‘pointers’].attachControl(canvas);
    which I need to use to stop temporarily this default behaviour of click+drag so that user can draw over the model using click+drag

  • I have tried to use an alternative by making use of:
    camera.angularSensibilityX = Infinity;
    camera.angularSensibilityY = Infinity;
    which you suggested, but it seems to hurt performance

  • sorry I cannot create a playground because of the rest of complexity in the app, if this new description suggests any new clues let me know thank you :wink:

@MarkBevels @mawa @PirateJC
actually I found out that this problem that I have happens not only in the case I described above, sometimes it also happens right after I load a model, nothing related to what I described before, but the issue is the same,with the arcrotatecamera, in which the click+drag behaviour on laptop, instead of rotating around model, only zooms in and out, and the only way to solve it is to restart the whole thing; again I cannot identify an scenario to build a playground, but seeing this now happen in different places sounds like there is definitely something there… will report if i find anything else about it

is the click+drag behavior for zoom in-out one of your input for another camera or viewport, or you haven’t set anything alike? How many cameras/viewports do you have in the scene? I personally hate it when I bounce into this random type of error that may or may not occur. May be there are just multiple causes. Hope someone will be able to give us a light…

here is a simple playground where you can click a button to detach and re-attach the camera https://playground.babylonjs.com/#XCPP9Y#5432

Perhaps you can build this into something more like your project to show the problem.

Other examples of attaching and detaching a camera to do something with a mesh are
https://www.babylonjs-playground.com/#UZ23UH#0 https://playground.babylonjs.com/#KT9EE7#3

Perhaps they might help

@mawa @JohnK thank you both, yes this is driving me a bit nuts as its my last issue to solve :wink: so mawa I only have 1 camera and only 1 scene and only 1 viewport

and John, what surprised me is that this zoom in-out instead of rotate behaviour of the arcrotatecamera has happened as well outside of the attach/unattach moment, very odd, and once it kicks in, once it happens once, there is no way to reset it unless I reload the whole thing;

thank you very much for creating that playground; if i see a possibility to build it towards my scenario I will do it but at the moment it doesn’t make sense as my app is just too large and complex to reduce it to a few lines,

I keep investigating, thank you again

Yeah, well GL. I’m just happy this one is not mine :relieved: :wink: I suppose you tried a variety of browsers (not just Chrome). I suppose you tried simplify by removing *[pointers] and simply attach and detach controls to check if any different…

@mawa yeah, I tried a few things, will try more, by the way I am only interested in making this work in chrome and ios safari, dont care about the other browsers, so far only tested in chrome, will test safari as soon as I can, but in theory it should all be compatible between both right?

I guess at this point, you should check Safari or any other browser, just to make sure. I had some bad experiences with the updates of Google’s policy and sudden (or announced but disregarded;) changes in the handling of behaviors (and some other stuff too…:wink: I guess what you will want now is try eliminate 1 by 1 all the possible causes…

or could it be anyhow related to this recent topic:

Just bounced into this and thought, may be…

@mawa making new tests it seems clear that the problem is related to this
camera.inputs.attached[‘pointers’].attachControl(canvas);
camera.inputs.attached[‘pointers’].detachControl(canvas);

I have to try all the alternatives to those, I already tried the camera sensitivity to 1000 but that hurts performance, what other ways do i have to stop the click+drag rotation behaviour of the arcrotatecamera and reenable it later?