Jump to content

What is the best way to handle movement of a mesh?


WingWorldWeeks
 Share

Recommended Posts

I've been prototyping a local co-op game that involves multiple players moving around on the same screen.

Where my trouble has come, is with inputs and handling multiple key presses and movement.

I've tried using the action manager to apply forces, impulses, or setting linear velocity on key down, but all of them seem to feel very buggy, especially when trying to press multiple keys.

A sample of my code looks like the following:

    scene.actionManager.registerAction(new BABYLON.ExecuteCodeAction(BABYLON.ActionManager.OnKeyDownTrigger, function (evt) {
        console.log(evt.sourceEvent.key);

        var velocity = player.physicsImpostor.getLinearVelocity();

        if (evt.sourceEvent.key == "w" || evt.sourceEvent.key == "W") {
            //player.applyImpulse(new BABYLON.Vector3(0, 0, 10), player.position);
            //player.physicsImpostor.applyForce(new BABYLON.Vector3(0, 0, 100), player.position);
            player.physicsImpostor.setLinearVelocity(new BABYLON.Vector3(velocity.x, velocity.y, 20));
        }
        if (evt.sourceEvent.key == 'a' || evt.sourceEvent.key == 'A') {
            //player.applyImpulse(new BABYLON.Vector3(-10, 0, 0), player.position);
            //player.physicsImpostor.applyForce(new BABYLON.Vector3(-100,0,0), player.position);
            player.physicsImpostor.setLinearVelocity(new BABYLON.Vector3(-20, velocity.y, velocity.z));
        }
        if (evt.sourceEvent.key == 's' || evt.sourceEvent.key == 'S') {
            //player.applyImpulse(new BABYLON.Vector3(0, 0, -10), player.position);
            //player.physicsImpostor.applyForce(new BABYLON.Vector3(0, 0, -100), player.position);
            player.physicsImpostor.setLinearVelocity(new BABYLON.Vector3(velocity.x, velocity.y, -20));
        }
        if (evt.sourceEvent.key == 'd' || evt.sourceEvent.key == 'D') {
            //player.applyImpulse(new BABYLON.Vector3(10, 0, 0), player.position);
            //player.physicsImpostor.applyForce(new BABYLON.Vector3(100, 0, 0), player.position);
            player.physicsImpostor.setLinearVelocity(new BABYLON.Vector3(20, velocity.y, velocity.z));
        }
    }));

    scene.actionManager.registerAction(new BABYLON.ExecuteCodeAction(BABYLON.ActionManager.OnKeyUpTrigger, function (evt) {
        console.log(evt.sourceEvent.key + " up");

        var velocity = player.physicsImpostor.getLinearVelocity();

        if (evt.sourceEvent.key == "w" || evt.sourceEvent.key == "W" || evt.sourceEvent.key == 's' || evt.sourceEvent.key == 'S') {
            player.physicsImpostor.setLinearVelocity(new BABYLON.Vector3(velocity.x, velocity.y, 0));
        }
        else if (evt.sourceEvent.key == 'a' || evt.sourceEvent.key == 'A' || evt.sourceEvent.key == 'd' || evt.sourceEvent.key == 'D') {
            player.physicsImpostor.setLinearVelocity(new BABYLON.Vector3(0, velocity.y, velocity.z));
        }

    }));

 My question is, what is the best practice to handle movement of a simple object (I'm currently just using a cube), and have it work with multiple key presses? Keep in mind this will be repeated for multiple players, so I'd like to to be as modular as possible

Link to comment
Share on other sites

Hello, you should look how we handle keys on FreeCamera for instance:

https://github.com/BabylonJS/Babylon.js/blob/master/src/Cameras/Inputs/babylon.freeCameraKeyboardMoveInput.ts

 

Basically:

- every time a key is pressed the associated boolean is turned to true

- on every frame, we check the list of booleans and apply the associated transform: https://github.com/BabylonJS/Babylon.js/blob/master/src/Cameras/Inputs/babylon.freeCameraKeyboardMoveInput.ts#L88

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