BeanstalkBlue Posted March 12, 2017 Share Posted March 12, 2017 I have read all of the camera inputs articles in the docs and I have tried to find a demo, but I can't seem to figure out how to add inputs to control objects other than the camera in Babylon. I have tried to manually add a keyboard event listener following Temechon's Infection code, but that doesn't work, as it interferes with camera input (either camera input will work or my inputs will work, but not both at the same time). Here is part of Temechon's code from https://github.com/Temechon/infection/blob/master/ts/Player.ts // Keyboard events this._handleKeyDownCall = this._handleKey.bind(this, 1); this._handleKeyUpCall = this._handleKey.bind(this, 0); window.addEventListener("keyup", this._handleKeyUpCall); window.addEventListener("keydown", this._handleKeyDownCall); Should I instead just implement my own ICameraInput and then put all of my game controls inside of this? It feels strange to have all game input inside of the camera. The controls don't control only the camera. But is this what Babylon experts usually do? It seems like this could create a problem when you switch from one camera to another. Is this meant to be done with the ActionsManager? Quote Link to comment Share on other sites More sharing options...
aWeirdo Posted March 12, 2017 Share Posted March 12, 2017 Hi @BeanstalkBlue Could you please create a simple playground demo for us, for better understanding of what you wish to achieve? http://babylonjs-playground.com cheers Quote Link to comment Share on other sites More sharing options...
JohnK Posted March 12, 2017 Share Posted March 12, 2017 Maybe this helps (or maybe not) In the playground below I can control the camera with the mouse or the car with the keys but not both at the same times. This is because (I think) the car use actionManager and the camera uses the default BJS system. What may be helpful is that I use A to turn left D to turn right SPACE to accelerate and you can turn and accelerate at the same time. To do multi-keypresses I use a key map to hold the keys currently down see lines 137 to 151. Lines 178, 192 and 210 produce the actions if SPACE, A and/or D are down respectively. Note : Click on the car before pressing any keys. http://www.babylonjs-playground.com/#102TBD#33 Now in the next playground I have added the key Z to allow me to rotate the camera, which I can now do at the same time as accelerating the car ( new lines 178 to 180). http://www.babylonjs-playground.com/#102TBD#41 While I was using an arcRotateCamera I changed the alpha. Of course if you are using a different type of camera then you will need to check what properties and methods are available for updating. You might also need to detachControl of the camera while doing this. If you think it is worth a try then just experiment with a simple PG. Quote Link to comment Share on other sites More sharing options...
BeanstalkBlue Posted March 12, 2017 Author Share Posted March 12, 2017 Quote In the playground below I can control the camera with the mouse or the car with the keys but not both at the same times That is the problem. I need to control camera with the mouse, and handle input events from the keyboard at the same time. This seems like a really basic requirement for many games, so Babylon must have this, I think? Here is a simple playground displaying the problem: http://www.babylonjs-playground.com/#1MK1Y4#1 Pressing "w" key moves the mesh. You can't move the camera with the mouse at the same time as moving the mesh. Edit: Actually is this maybe just my laptop? I don't have a mouse with me at the moment. Can you guys move the mesh while controlling the camera with mouse or is it just me that can't with my touchpad? Quote Link to comment Share on other sites More sharing options...
JohnK Posted March 13, 2017 Share Posted March 13, 2017 (1) I hope you read more than the first paragraph. (2) You did not specify in your original question the camera had to be moved using a mouse. (3) A mixture of the mapping method with your own custom mouse control could work. (4) You could try the onPick triggers http://doc.babylonjs.com/tutorials/how_to_use_actions#triggers on a mesh that is parent to a camera to achieve (3). (5) Wait for a better answer. Quote Link to comment Share on other sites More sharing options...
aWeirdo Posted March 13, 2017 Share Posted March 13, 2017 @BeanstalkBlue It works perfectly fine, you're just doing it wrong You were only running the translation once per keyup, instead, you should run it continiously while the key is being pressed and stop when it isn't. http://www.babylonjs-playground.com/#1MK1Y4#2 GameMonetize 1 Quote Link to comment Share on other sites More sharing options...
BeanstalkBlue Posted March 13, 2017 Author Share Posted March 13, 2017 Actually a coincidence of two things happened, making me think there was a problem that wasn't really there: 1) I had a bug in my original code, where keyboard presses really did interfere with mouse movement. 2) I fixed that bug, but I was travelling with my laptop and stopped using a mouse (was using a trackpad instead). When I did this, I didn't realize that my trackpad driver disables the trackpad temporarily when you press a non-modifier key on the keyboard . It's a feature to prevent resting your palm on the trackpad causing unwanted cursor movement while typing. So basically my code works now and it attaching eventListeners to the window works fine. Maybe at least this will help someone else if they ever google this haha Quote Link to comment Share on other sites More sharing options...
En929 Posted September 2, 2018 Share Posted September 2, 2018 On 3/13/2017 at 5:20 PM, BeanstalkBlue said: So basically my code works now and it attaching eventListeners to the window works fine. Maybe at least this will help someone else if they ever google this haha Yup, your question definitely helped me. I found this by google. I am just now starting. So, I am glad that you posted this haha ? GameMonetize 1 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.