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?

  • \$\begingroup\$ May I ask why you chose to delete this question? \$\endgroup\$ – DMGregory Aug 31 '18 at 17:15
  • \$\begingroup\$ @dmgregory honestly I was ashamed how easy the solution was and I couldn't figure it out... any ways undeleted. \$\endgroup\$ – adam west Aug 31 '18 at 18:01
  • \$\begingroup\$ That's OK. "Lowball" questions can help future users too. If it wasn't obvious to you, there will probably be other beginners who also find it non-obvious. :) To avoid down-votes for lack of research though, it usually helps to include some details about the research you've done so far — like including a quotation from a source you read, and pointing out which parts you need help understanding. \$\endgroup\$ – DMGregory Aug 31 '18 at 20:20
  • \$\begingroup\$ @DMGregory I'll keep that in mind \$\endgroup\$ – adam west Aug 31 '18 at 21:30

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;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.