Jump to content

[SOLVED] - How To Handle User Input


MackeyK24
 Share

Recommended Posts

I can't seem to find ANY info on user input... Only camera stuff... How are supposed to handle user input that does NOT have to do with the camera... I still need to be able to get what keys what are pressed, what horizontal and vertical axis from gamepads and arrows keys, rotation from the mouse. I don't see ANY of that stuff in the API... Only camera... I get that the camera is important but if we don't attach a camera, how do we get all that user input... Or am i just supposed to make up my own (that would suck, i don't know all ins and outs of user inputs to be able to handle any input for my toolkit... Right now I'm just handle basic horizontal and vertical movement)

But the camera seems LOADED with user input, how come it was designed to ONLY give access to those API on the camera... That seems funny :)

Now i think i have to re-implement USER INPUT for my toolkit from scratch :(

 

Link to comment
Share on other sites

Its Funny... This code works on the canvas element BEFORE a scene is load ... Once a scene is load, the canvas NO LONGER gets monkey down... AND I DONT HAVE CAMREA ATTACH CONTROL... What is the engine doing to the canvas that keydown events no longer works... OR Are WE NOT SUPPOSED to use the canvas to get keyboard events...

See thats what seems funny to me... the whole engine has NO user input documentation at all... Other than just say "AttachControl"... What are rules and regs for getting user input... are supposed to use canvas... the window.... document.documentElement... the body... What???

Is the canvas being used by default by the camera user input because the canvas is cross platform and will work for mobile too... I just don't know and i can't find anything that doesn't ultimately just say "attach input to camera".

Either David(s) or anyone else... Please explain to me WHY the ALL the framework code for user input is on the camera (and nested like 4 or 5 layers deep in the camera api)... And would one use that api to lets say move a player cube around instead of the camera...?

Now i created my own little api for basic movement using the window.addEventListener... but mine seems CHEESY compared to all that good stuff on the camera inputs that seems like its just a waste sitting there down on the camera for folks who don't need AUTO-MAGIC to move the camera.

Link to comment
Share on other sites

To move a car around ( which required multiple key presses - space to accelerate and A and D to turn) I used actionManager  as in http://www.babylonjs-playground.com/#102TBD#31

 lines 136 to 150 and then in registerAfterRender

2 hours ago, MackeyK24 said:

But the camera seems LOADED with user input, how come it was designed to ONLY give access to those API on the camera... That seems funny :)

I am not clever enough to know whether this statement is true or not, perhaps it was not purposely designed that way, just an outcome of someone concentrating on the camera. Perhaps there is access to the camera API for meshes but it is not just documented well. In any of these cases we need to remember that BJS (and its documentation) is an open source project developed by people, in their free time, adding to it as they see gaps. You addition of a BJS plugin to Unity seems to be well appreciated by many people on this forum.

If you see a need for an improved API to deal with user input for meshes then any development you make, PR and document would I am sure go down a storm.

Link to comment
Share on other sites

I can just go in and change or add to that stuff??? Dont you have to get some kind of approval to add code at that level???

I know i can change what i download, but i don't want to maintain a separate version...

I think i will just DOWNGRADE that feature and at least try get Horizontal and Vertical Movement (gamepad and mouse look movement would be great)

Link to comment
Share on other sites

You know... Im such a DUMB ASS sometimes :(

I wish i could delete or restart this post... I don't think i need to get all that deep into the camera input API...

I just need (for small type games that won't use that automatic fps camera type control) to move a mesh vertical and horizontal (which i got with the keyup/down and wasd)... But how do i turn mousedown Mouse X and Y into up, down, left and right rotation on mesh... I can't tell what really going on FreeCameraInputs (there seems like a ton of other stuff besides just rotating something left, right, up or down)... Anybody got some simple info on handling rotation of a mesh based on mouse?

Note: I will also need this vertical horizontal for movement and left, right, up and down for look with a gamepad as well... I think (from looking at gamepad code) i can get moment from Right stick values and rotation from Left stick values... Any thoughts there would be cool.

Im sorry I'm such a newbie and this stuff seems trivial to babylon folks... But I'm just trying to get basic movement going so i can throw in the toolkit to handle those non automatic camera control games (like the first few tutorials) where you are supposed to get the input and move or rotate something and its NOT the camera.

This is what i got so far... A few function from my SceneManager to handle BASIC user input:

        // ********************************** //
        // *   Scene Input Helper Support   * //
        // ********************************** //

        public getUserInput(input:BABYLON.UserInputAxis):number {
            var result:number = 0;
            if (this._input) {
                switch(input) {
                    case BABYLON.UserInputAxis.Vertical:
                    case BABYLON.UserInputAxis.Horizontal:
                        result = (input == BABYLON.UserInputAxis.Horizontal) ? BABYLON.SceneManager.horizontal : BABYLON.SceneManager.vertical;
                        break;
                    case BABYLON.UserInputAxis.ClientX:
                    case BABYLON.UserInputAxis.ClientY:
                        result = (input == BABYLON.UserInputAxis.ClientX) ? BABYLON.SceneManager.clientx : BABYLON.SceneManager.clienty;
                        break;
                    case BABYLON.UserInputAxis.MouseX:
                    case BABYLON.UserInputAxis.MouseY:
                        result = (input == BABYLON.UserInputAxis.MouseX) ? BABYLON.SceneManager.mousex : BABYLON.SceneManager.mousey;
                        break;
                }
            }
            return result;
        }

        public resetUserInput():void {
            BABYLON.SceneManager.mousex = 0;
            BABYLON.SceneManager.mousey = 0;
            BABYLON.SceneManager.clientx = 0;
            BABYLON.SceneManager.clienty = 0;
            BABYLON.SceneManager.vertical = 0;
            BABYLON.SceneManager.horizontal = 0;
        }

        public enableUserInput(preventDefault: boolean = false, useCapture:boolean = false):void {
            if (!this._input && window) {
                window.addEventListener("keydown", this.inputKeyDownHandler, useCapture);
                window.addEventListener("keyup", this.inputKeyUpHandler, useCapture);
                window.addEventListener("mousemove", this.inputMouseMoveHandler, useCapture);
                this._prevent = preventDefault;
                this._input = true;
            }
            // TODO: Handle Gamepad User Input
        }

        public disableUserInput(useCapture:boolean = false):void {
            if (this._input && window) {
                window.removeEventListener("keydown", this.inputKeyDownHandler, useCapture);
                window.removeEventListener("keyup", this.inputKeyUpHandler, useCapture);
                window.removeEventListener("mousemove", this.inputMouseMoveHandler, useCapture);
                this._prevent = false;
                this._input = false;
                this.resetUserInput();
            }
            // TODO: Handle Gamepad User Input
        }

        private inputKeyDownHandler(e:KeyboardEvent):any {
            switch(e.keyCode) {
                case 39: // Right
                case 68: // D-Key
                    BABYLON.SceneManager.horizontal = 1;
                    break;
                case 37: // Left
                case 65: // A-Key
                    BABYLON.SceneManager.horizontal = -1;
                    break;
                case 38: // Forward
                case 87: // W-Key
                    BABYLON.SceneManager.vertical = 1;
                    break;
                case 40: // Back
                case 83: // S-Key
                    BABYLON.SceneManager.vertical = -1;
                    break;
            }
            if (this._prevent) e.preventDefault();
            return true;
        }

        private inputKeyUpHandler(e:KeyboardEvent):any {
            switch(e.keyCode) {
                case 39: // Right
                case 37: // Left
                case 68: // D-Key
                case 65: // A-Key
                    BABYLON.SceneManager.horizontal = 0;
                    break;
                case 38: // Forward
                case 40: // Back
                case 87: // W-Key
                case 83: // S-Key
                    BABYLON.SceneManager.vertical = 0;
                    break;
            }
            if (this._prevent) e.preventDefault();
            return true;
        }

        private inputMouseMoveHandler(e:MouseEvent):any {
            //if (!this._scene.getEngine().isPointerLock) return true;
            var clientx:number = e.clientX || 0;
            var clienty:number = e.clientY || 0;
            if (clientx < 0) clientx = 0;
            if (clienty < 0) clienty = 0;
            BABYLON.SceneManager.clientx = clientx;
            BABYLON.SceneManager.clienty = clienty;
            var mousex:number = e.movementX || e.mozMovementX || e.webkitMovementX || e.msMovementX || 0;
            var mousey:number = e.movementY || e.mozMovementY || e.webkitMovementY || e.msMovementY || 0;
            if (mousex < 0) mousex = 0;
            if (mousey < 0) mousey = 0;
            BABYLON.SceneManager.mousex = mousex;
            BABYLON.SceneManager.mousey = mousey;
            if (this._prevent) e.preventDefault();
            return true;
        }

then used in update loop like this:

        private updatePlayerMovement(): void {
            var horizontal:number = this.manager.getUserInput(BABYLON.UserInputAxis.Horizontal);
            var vertical:number = this.manager.getUserInput(BABYLON.UserInputAxis.Vertical);
            this.mesh.physicsImpostor.applyImpulse(new BABYLON.Vector3(horizontal * this.speed, 0.0, vertical * this.speed), this.mesh.getAbsolutePosition());
        }

My SceneManager.getUserInput is kin to Unity Input.GetAxis

But I'm not too sure about my inputMouseMoveHandler and how to turn clientX/Y or mouseX/Y into rotation left, right, up or down :(

Link to comment
Share on other sites

1 hour ago, MackeyK24 said:

I can just go in and change or add to that stuff??? Dont you have to get some kind of approval to add code at that level???

From this quote I am making an assumption that you do not know about the following. If you do and I am teaching my grandmother to suck eggs please forgive me.

BJS and its documentation are held in github - https://github.com/BabylonJS

GitHub is a code hosting platform for version control using Git. Code is held in repositories. You can make changes to the code in a repository under certain conditions including which is the approval of the repository owner.

To do all this you need to sign up to github, fork the BJS repository which will create a copy into a new repository that you now own. Github recommends Github Desktop for working locally but I couldn't get on with it. I use git and tortoisegit to transfer files locally to github. Once you have made the any changes to your repository you can then send a Pull Request to the original repository which if accepted will update the BJS repository with the changes you made.

OK it is a little more tricky than that, you will need to do some reading about Git and Github first. There are also some contribution guidelines to read for BJS  (scroll down to readme file on Babylonjs/babylon.js github page) . After a few false starts I set up the guide for BJS I am writing on github and can transfer my local files to my repository successfully. There is still an awful lot I do not know about Git and Github.

Useful reading

http://pixelcodr.com/tutos/contribute/contribute.html

So the full answer is yes you can change or add to that stuff provided you do get approval.

Should you wish to contribute the best idea is to demonstrate your new code in the playground, Deltakosh the originator of BJS will see it and if you get a woop and I want a PR then you can get a contributor mention when the PR is complete. By the way changes are made in the upcoming beta version not the current version, but it is the beta version that the playground uses.

Given your work with unity and BJS I can see that you have the potential to contribute to BJS. So as I said before if you can add to user input control API for meshes go for it.

Hope I haven't overloaded you and I am telling you something new. If either is false please forgive me.

1 hour ago, MackeyK24 said:

Im sorry I'm such a newbie and this stuff seems trivial to babylon folks

Non of the stuff you are asking seems trivial to me.

Link to comment
Share on other sites

Thanks for the info... I know a little (learning more every day) about github. I am a developer, for about 20 years now... But i am a enterprise application developer. I make medical practice management software for hospitals and clinics. Its always a closed source (proprietary company software) kinda thing so i am new to the open source.

I have actually made a few PR to the babylon.js framework... but they were SMALL changes... The input stuff would be major and i didn't think one would be allowed to just go messing with that stuff (not without forking your own changes in your own codebase... not for the community).

One main reason i am making the toolkit is to make it easier for me to learn and create light weight web games. I still don't know all "industry standard" type things that are pretty much the same in all engine... What i was hoping to do was create the toolkit and use Unity as "Game Concept" learning tool for creating games,  but just do it with the babylon framework instead of unity for runtime... Because the actual 'concept' for the game should be the same... They way you implement it is the only difference... I would think :)

 

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