Myjestic Posted September 11, 2016 Share Posted September 11, 2016 Hi, if I add the following code to my scene, pressing "q" locks the pointer as wanted. If I press "q" again, the pointer will unlock only after I click the left mouse button. How can I prevent this behavior? window.addEventListener("keydown", checkKeyPressed, false); // If key "Q" is pressed // ----------------------------------------------------------------------------------------- function checkKeyPressed(e) { switch(e.keyCode) { case 81: // key q pressed if (!engine.isPointerLock){ engine.isPointerLock = true; } else { engine.isPointerLock = false; } break; } } Quote Link to comment Share on other sites More sharing options...
Wingnut Posted September 11, 2016 Share Posted September 11, 2016 Hi @Myjestic Generally speaking, any .isSomethingSomething property... is a check of a "state", and is not used as a setter. (At least I think that is truth.) It would be nice to find engine.setPointerLock(true/false)... but I have not found that. So, I tried some things. First, I looked at engine source code... https://github.com/BabylonJS/Babylon.js/blob/master/src/babylon.engine.js#L309 Then, I tried some experiments... put some of the source code in a playground, goofed around... http://playground.babylonjs.com/#15ECGN#2 Complete failure on my part. heh. But perhaps you, or others... can repair my mess. I'll keep testing, too... but I am not doing so well, so far. Sorry. Calling all forum... HELP. Quote Link to comment Share on other sites More sharing options...
Myjestic Posted September 11, 2016 Author Share Posted September 11, 2016 Hey Wingnut, thanks so far. To clarify what I want to do: Babylon.js gives the user (player) the possibility to move the freecamera with the keys and look around with mouse, if left mouse button is pressed. If you "walk" through a large world, the player will suffer finger pain after a while . So I want to give the player the possibility to switch the "look around" mode between locked cursor and unlocked cursor (left mouse button pressed). I played a bit with the Pointer Lock API but get no good results. I did not know that this small task is so complicated Quote Link to comment Share on other sites More sharing options...
adam Posted September 11, 2016 Share Posted September 11, 2016 I found this PG: http://www.babylonjs-playground.com/#QKQHS#227 using the PG search: http://doc.babylonjs.com/playground?q=pointerlock Quote Link to comment Share on other sites More sharing options...
Myjestic Posted September 11, 2016 Author Share Posted September 11, 2016 Ok sry, did not find this example. What displeases me is, that the browser is asking for hiding the cursor. Can I hide it without that message or lock it without hiding? Quote Link to comment Share on other sites More sharing options...
Myjestic Posted September 12, 2016 Author Share Posted September 12, 2016 I get the best result if I set the getter. http://playground.babylonjs.com/#15ECGN#8 If I activate the mouse lock for the first time I have to press "q" two times before it works as expected. Everything else is working in this PG. Quote Link to comment Share on other sites More sharing options...
Wingnut Posted September 12, 2016 Share Posted September 12, 2016 Yeah, sorry, M... I told you wrong information about isPointerLock, apparently. Dummy me. My apologies. Quote Link to comment Share on other sites More sharing options...
adam Posted September 12, 2016 Share Posted September 12, 2016 It looks like the problem is here: https://github.com/BabylonJS/Babylon.js/blob/a071d99a3ab83d0805a0b891da75f24851088ce9/src/Cameras/Inputs/babylon.freecamera.input.mouse.ts#L69 offsetX and offsetY are always 0 Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted September 15, 2016 Share Posted September 15, 2016 Unfortunately I'm afraid this is beyond our control. Seems like the browser upadte the lock only after click (But I did not do a long search though so I can be wrong) Quote Link to comment Share on other sites More sharing options...
adam Posted September 15, 2016 Share Posted September 15, 2016 Here is a way to do it: http://playground.babylonjs.com/#15ECGN#13 I can submit a PR for the fix to the FreeCamera later today. Quote Link to comment Share on other sites More sharing options...
Myjestic Posted September 15, 2016 Author Share Posted September 15, 2016 Hey adam, this is great. Adding the line scene.activeCamera.inputs.attached.mouse.previousPosition = null to the unlocking section do the trick. Thank you very much! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.