Adrin
Adrin

Reputation: 595

How can I implement friction to movement of a object in a game

below is the code that I'm using for movement of a object:

let XX = 0
let YY = 0
let maxSpeed = 100;

if(keyDown.w) {
    XX += Math.sin(angle*Math.PI/180)*moveSpeed;
    YY += -Math.cos(angle*Math.PI/180)*moveSpeed;
}

I'm trying to implement friction in it when object starts moving. like when key is down, speed starts to going up and it takes n seconds until it reaches the maxSpeed.
How can I Do this?

Upvotes: 2

Views: 694

Answers (1)

MBo
MBo

Reputation: 80197

Seems that you need not friction but acceleration. Here is constant acceleration due to constan motor power:

let XX = 0
let YY = 0
let moveSpeed = 0;
let Accel = 2;
let maxSpeed = 100;

if(keyDown.w) {
    moveSpeed = Math.min(maxSpeed, moveSpeed + Accel);
    XX += Math.sin(angle*Math.PI/180)*moveSpeed;
    YY += -Math.cos(angle*Math.PI/180)*moveSpeed;
}

Upvotes: 2

Related Questions