Jump to content

Babylon way of adding input to control more than just the camera.


BeanstalkBlue
 Share

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

(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.

Link to comment
Share on other sites

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 :o. 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 :D

Link to comment
Share on other sites

  • 1 year later...
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 :D

 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 😅

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...