0
\$\begingroup\$

Below is the code that I'm using to move an 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 acceleration in moving. How can I do that?

\$\endgroup\$
0

1 Answer 1

0
\$\begingroup\$

Acceleration is a change in speed over time, so you can implement it by incrementing your movespeed over time.

movespeed += acceleration_constant;

If you want to limit speed, then you can clamp it like this:

movespeed += acceleration_constant;
movespeed = (movespeed < maxSpeed) ? movespeed : maxSpeed;
movespeed = (movespeed > minSpeed) ? movespeed : minSpeed;
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .