How to add friction to my force movement (p2.js)

Hey again,

Does anyone know how I can add a friction to my force based movement so that the player does not feel like its in space? Heres an example of my code:

if (x !== 0 || y !== 0) {
        x *= dt * this.playerSpeed;
        y *= dt * this.playerSpeed;

        this.entity.script.p2Body.body.force= [x, y];
    }
    else
    {
        this.entity.script.p2Body.body.force = [0, 0];
    }

I want to be able to have a friction on this so that the player slows down, but right now it just looks like its in space (wheee)

Thanks,
Chhes
– no solution from me –

Hi @Chhes,

Can you try increasing the damping value in your player p2Body script?

Check how the car slows down in the Vehicle example on the p2.js integration project:

image

https://playcanvas.com/project/446127/overview/p2js-integration

Hey Leonidas,

I tried that along with a number of things already, but nothing seemed to make the force have a friction. I think its okay because it turns out that was a really inefficient way to move my players anyway because they needed to have a velocity because of the gameplay style.

Thanks for your help but it turns out this was no longer needed for me

Chhes :cheese:

1 Like

To close off the thread, you can create a friction material to do this: p2.js/friction.html at master · schteppe/p2.js · GitHub and Friction and bounce in p2.js | paulsalaets.com

1 Like