How to apply force so then I can only brake?

how to apply force so then I can only brake?
I mean I don’t need the ‘reverse’ force I want to separate to this: when I brake I just brake
and other key to reverse the car so what to do with force?

currently I have

if(up){
       this.entity.rigidbody.applyForce(0,0,-20);

 }
if(down){
      this.entity.rigidbody.applyForce(0,0,20);

 }

but this make it reverse and looks a little artificial
I think when it reaches 0 kph so no force or maybe other solution?

So, one solution for this is to apply the reverse force as long as the body moves forward. If the forward velocity is zero or changes direction to backwards facing, then stop applying the force, the car stopped fully.

To find if the car is moving forward or backwards you can get the dot product between the forward and the linear velocity vectors.

Something like this:

var dot = this.entity.forward.dot(this.entity.rigidbody.linearVelocity.normalize());

if( dot > 0){
   // we are moving forward, apply braking force
}
1 Like

ok works but with

if(dot < 0 && down){
            this.entity.rigidbody.applyForce(0,0,20);
            
}

I mean I rotated the car by 180 degrees because it points in opposite direction not what I want so when I roate there drives in direction what I want but this modify the vector so what approaches would I take?
and also that’s why I have 20 force when braking (mean positive) and -20 (negative) when accelerating

You will have to apply a force to the direction of movement, not in world units.

Something like this:

var brakePower = 20;
var force = new pc.Vec3().copy(this.entity.forward).scale(-1 * brakePower);

this.entity.rigidbody.applyForce(force);
1 Like