hfeist Posted October 13, 2016 Share Posted October 13, 2016 I'm trying to find a way to use the mousedown event to move the camera forward. I've mapped the keys so that T moves forward and want it to fire whenever there is a mousedown event. I have come up with the following but cannot get the keypress function to fire when there is a mousedown. Any suggestions would be most welcome. In the scene setup I've set the forward key to: camera.keysUp = [84]; // T After the scene setup I've added: document.addEventListener("mousedown", function(e) { var code = 84;//T $('document').trigger( jQuery.Event('keypress', { keyCode: code, which: code }) ); $("#show").append("mousedown creates: " + code + "<BR>"); }); document.addEventListener("keypress", function(e) { var key = e.which; $("#show").append("key code:+key+"<BR>"); //If it's the T key if (key == 84) { $("#show").append("T key has been pressed<BR>"); } }); // I would like the mousedown event to fire the keypress event // as if the T key has been pressed there is a div in the body to display the output: <div id="show"></div> Quote Link to comment Share on other sites More sharing options...
adam Posted October 13, 2016 Share Posted October 13, 2016 It sounds like you need to make a custom CameraInput. https://github.com/BabylonJS/Babylon.js/tree/master/src/Cameras/Inputs Here is a start: http://www.babylonjs-playground.com/#VO9FH#0 dbawel 1 Quote Link to comment Share on other sites More sharing options...
hfeist Posted October 13, 2016 Author Share Posted October 13, 2016 thanks for the pointers. certainly looks daunting! but i've added it to my project and can see the camera inch forward ever so slightly at each mouseclick. would much prefer to keep moving as long as the mouse is down. and perhaps move backward if the shift key is down. and i can see no mention of mouseup or mousedown so guess it's an entirely different way of doing things. Quote Link to comment Share on other sites More sharing options...
adam Posted October 13, 2016 Share Posted October 13, 2016 38 minutes ago, hfeist said: looks daunting! All I did was copy/rename FreeCameraMouseInput and strip out unneeded code. https://github.com/BabylonJS/Babylon.js/blob/master/src/Cameras/Inputs/babylon.freecamera.input.mouse.ts and then found the code that moved the camera forward in the keyboard input: https://github.com/BabylonJS/Babylon.js/blob/master/src/Cameras/Inputs/babylon.freecamera.input.keyboard.ts#L90 https://github.com/BabylonJS/Babylon.js/blob/master/src/Cameras/Inputs/babylon.freecamera.input.keyboard.ts#L101 and moved it to the pointerdown section of the new camera input. Quote Link to comment Share on other sites More sharing options...
hfeist Posted October 13, 2016 Author Share Posted October 13, 2016 thanks for going to so much trouble. i'm trying to understand it well enough to find where i might create a loop that keeps you moving as long as the mouse is down just as it does as long as a nav key is held down Quote Link to comment Share on other sites More sharing options...
adam Posted October 13, 2016 Share Posted October 13, 2016 http://www.babylonjs-playground.com/#VO9FH#1 Wingnut 1 Quote Link to comment Share on other sites More sharing options...
Wingnut Posted October 13, 2016 Share Posted October 13, 2016 Hi guys. Interesting usage/challenge, @hfeist! Excellent camera input "wrangling", @adam. I bookmarked this playground 3 times! Good stuff. Quote Link to comment Share on other sites More sharing options...
Kemal UÇAR Posted October 17, 2016 Share Posted October 17, 2016 Hi hfeist ; i found this pg few days ago, maybe help you http://playground.babylonjs.com/#TTLMJ its for mouseOver you can change for mouse pick click etc i hope helpful for you hv nice time Quote Link to comment Share on other sites More sharing options...
hfeist Posted October 17, 2016 Author Share Posted October 17, 2016 Hey Adam--thanks for that. I've added the ability to move backwards by holding shift down window.addEventListener("keydown", function(e) { var keycode = e.which; if (e.keyCode == 16) { speed=-1*speed;; } }); window.addEventListener("keyup", function(e) { var keycode = e.which; if (e.keyCode == 16) { speed=-1*speed;; } }); Just needed to set a flag so the speed gets locked down when the script first runs: if(flag==false){ flag=true; speed = camera._computeLocalCameraSpeed()/4; } Here it is in action: http://64.78.15.229/eyemap3Djs/walk.html 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.