javascript - Character Movement using Babylon.js - Stack Overflow

admin2025-04-22  0

In a game demo I am putting up for school I need to move my character using the W-A-S-D keys and also the arrow keys. I put up a function and set up a switch case to listen for any of the key presses. Here is my code snippet:

//Handles the player's movement
var PlayerMovement = (function () {
    //Constructor
    function PlayerMovement() {
        this.gameObject = null;
        this.movementSpeed = 0;
        this.rotationSpeed = 0;
    }

    PlayerMovement.prototype.awake = function () {
        console.log("Awake");
    };

    PlayerMovement.prototype.update = function () {
        //console.log(Tools.getFps());
    }

PlayerMovement.prototype.onKeyPressed = function (key) {
        switch(key)
        {
            case KeyType.W:
            case KeyType.UpArrow:
                console.log("Moving up");
                this.gameObject.meshObject.position.z += (BABYLON.Vector3.Up() * this.movementSpeed * Tools.getDeltaTime());
                break;
            case KeyType.A:
            case KeyType.LeftArrow:
                //TODO: Do stuff
                break;
            case KeyType.S:
            case KeyType.DownArrow:
                //TODO: Do stuff
                break;
            case KeyType.D:
            case KeyType.RightArrow:
                //TODO: Do stuff
                break;
        }
    }
 return PlayerMovement;
})();

My issue is that my character jumps so far ahead that he vanishes from the screen. Can anyone help me figure out what is wrong with my calculation?

In a game demo I am putting up for school I need to move my character using the W-A-S-D keys and also the arrow keys. I put up a function and set up a switch case to listen for any of the key presses. Here is my code snippet:

//Handles the player's movement
var PlayerMovement = (function () {
    //Constructor
    function PlayerMovement() {
        this.gameObject = null;
        this.movementSpeed = 0;
        this.rotationSpeed = 0;
    }

    PlayerMovement.prototype.awake = function () {
        console.log("Awake");
    };

    PlayerMovement.prototype.update = function () {
        //console.log(Tools.getFps());
    }

PlayerMovement.prototype.onKeyPressed = function (key) {
        switch(key)
        {
            case KeyType.W:
            case KeyType.UpArrow:
                console.log("Moving up");
                this.gameObject.meshObject.position.z += (BABYLON.Vector3.Up() * this.movementSpeed * Tools.getDeltaTime());
                break;
            case KeyType.A:
            case KeyType.LeftArrow:
                //TODO: Do stuff
                break;
            case KeyType.S:
            case KeyType.DownArrow:
                //TODO: Do stuff
                break;
            case KeyType.D:
            case KeyType.RightArrow:
                //TODO: Do stuff
                break;
        }
    }
 return PlayerMovement;
})();

My issue is that my character jumps so far ahead that he vanishes from the screen. Can anyone help me figure out what is wrong with my calculation?

Share Improve this question edited Dec 19, 2014 at 18:06 user3838697 asked Dec 19, 2014 at 17:53 user3838697user3838697 1253 silver badges12 bronze badges 4
  • It sounds like you're asking why: (BABYLON.Vector3.Up() * this.movementSpeed * Tools.getDeltaTime()) is too large? Why not look at each ponent, and see why it's too large? – Gerrat Commented Dec 19, 2014 at 18:02
  • If you've set this.movementSpeed to 0, then obviously, your position isn't going to change at all. ...so there's relevant code you're not showing us. – Gerrat Commented Dec 19, 2014 at 18:34
  • Sorry, I am assigning a 1 to the speed movement and also changed the calculations of what happens when my character moves (or, should be moving): this.gameObject.meshObject.position.z += (BABYLON.Vector3.Up() * this.movementSpeed * Tools.getDeltaTime()); What happens now is that my character jumps ahead and vanishes from the screen – user3838697 Commented Dec 19, 2014 at 18:41
  • Well, write the other two values (BABYLON.Vector3.Up(), and Tools.getDeltaTime()`) to the console, and see which is too large. – Gerrat Commented Dec 19, 2014 at 18:55
Add a ment  | 

2 Answers 2

Reset to default 4

A few things -

  • BABYLON.Vector3.Up() is (0,1,0) . Multiplying this object with any number will return NaN. I guess the object doesn't jump away from the screen, it simply disappears.
  • Z is not up :-) position.y should be changed if you wish to jump up.
  • If you want to translate using vectors (using the BABYLON.Vector3.Up() Vector) use the mesh.translate(vector, distance) function. In your case (assuming this is the right value you want to set):

    this.gameObject.meshObject.translate(BABYLON.Vector3.Up(), this.movementSpeed * Tools.getDeltaTime());
    
  • I assume you did that already, but if not - turn the physics engine on and set gravity for your scene. You can learn about it in the BJS Docs : http://doc.babylonjs./page.php?p=22091

  • A better way to implement a jump would be to apply acceleration in the right direction (up) and letting the physics engine do its magic. Check out "Applying impulse" here - http://blogs.msdn./b/eternalcoding/archive/2013/12/19/create-wonderful-interactive-games-for-the-web-using-webgl-and-a-physics-engine-babylon-js-amp-cannon-js.aspx

It's nearly impossible for us to help you without the rest of the code. Could you provide the entirety of your custom JS file? I'm thinking this is likely an issue with your camera angle, not so much the character movement. Also, is this a first person game or third person game?

Sorry, would have left this "answer" as a ment but I don't have 50 reputation points to do so. Trying to solicit more information to provide an actual answer.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745284525a294309.html

最新回复(0)