Rigidbody Won't Respond to applyImpulse

Hello community!
I’m having some trouble getting a rigidbody to move on a keypress. Originally I was using translateLocal, but once I decided to change the jumping mechanics to raycasting, I had to change the entity to a dynamic rigidbody. Right now, the code looks essentially like this:

CharacterControl.prototype.move = function(dx, dy, dz) {
this.entity.rigidbody.applyImpulse(dx, dy, dz);
};

And then the keypress looks like this:

CharacterControl.prototype.update = function(dt) {
let keyboard = this.app.keyboard;
this.speed = 7;
if( keyboard.isPressed(pc.KEY_W) ) {
this.move(0, 0, this.speed * dt)
};
};

I’m new to Javascript (only been using it for a couple of weeks) so forgive me if it looks messy and unoptimized.

Hi @cyron_007 and welcome!

If you’re currently using the same value as you did for translateLocal(), the force is probably too low.

Also, if this is for forward movement, I suggest using applyForce(). For jumping, applyImpulse() will be the best option.

Hi, thank you for the advice!
I changed this.speed to 30000 and there was still no movement. I tried doing

this.entity.rigidbody.teleport(this.entity.getPosition().x+dx, this.entity.getPosition().y+dy, this.entity.getPosition().z+dz);

which worked, but I still can’t jump. Using applyImpulse() for the jump still doesn’t work. Thanks though!

I would try to check if your move function received the correct information. You can add something like below and use the console of your browser to read the information.

console.log(dz);

Also check the rigidbody sittings of the entity. For example if Linear Damping is set to 1, the rigidbody won’t move.

1 Like

It looks like it’s recieving the correct information, and both linear and angular dampening were set to zero. I tried doing this, and it returned “move() failed”:

MoveBox.prototype.move = function(dx, dy, dz) {
   console.log(dz);
   let result = this.entity.rigidbody.applyForce(dx, dy, dz);
    if(result) {
        console.log("move() successful");
        return result;
    }
    else {
        console.log("move() failed");
    };

};

Also, this is my code for the jumping system:

(in initialize function) {
   this.isGrounded = true;
   this.velocityY = 0;
}; 
(in update function) {
if(keyboard.wasPressed(pc.KEY_SPACE) && this.isGrounded) {
        this.velocityY = this.jumpForce;
        this.isGrounded = false;
    }
this.velocityY += this.gravity * dt;
this.entity.rigidbody.applyImpulse(0, this.velocityY * dt, 0);
};

this.jumpForce is set to 1000. The program is registering the keypresses, sending information to correct recipients, but something about the applyForce() and applyImpulse() functions aren’t working correctly, because when I change them to translateLocal() or rigidbody.teleport(), it works fine.

Make also sure the Linear Factor of the rigidbody is not set to 0.

If you share the editor link of your project, I can take a look when I have some time.

1 Like

Of course! I have both the Linear and Angular factors of the rigidbody set to 1. Here is the link to my project: PlayCanvas | HTML5 Game Engine

The collision component is missing. This is the volume for the rigidbody.

Ah, thank you. Am I supposed to be changing the friction / restitution to make it stop bouncing around so much?

Yes, it can be challenging to find the right balance between all values.