Friday, December 15, 2017

2d - How do I implement deceleration?


I say "deceleration" because I'm not using acceleration at the moment; what I mean is moving the velocity back towards zero, eventually stopping.


I'm new to vectors and not so great with physics and such. How is "deceleration" normally handled?




What I have now works, but it seems kind of hackish.


update:function(Game, t, dt) {
var speed = Game.Input.isKeyDown('shift') ? 8 : 4;


if (Game.Input.isKeyDown('a')) {
this.velocity.i -= speed;
}
else if (Game.Input.isKeyDown('d')) {
this.velocity.i += speed;
}
else {
if (Math.abs(this.velocity.i) > 3) {
this.velocity.i += (this.velocity.i > 0) ? -speed : speed;

}
else {
this.velocity.i = 0;
}
}

if (Game.Input.isKeyDown('w')) {
this.velocity.j -= speed;
}
else if (Game.Input.isKeyDown('s')) {

this.velocity.j += speed;
}
else {
if (Math.abs(this.velocity.j) > 3) {
this.velocity.j += (this.velocity.j > 0) ? -speed : speed;
}
else {
this.velocity.j = 0;
}
}


this.updateVectors(dt);
}

I used 3 because anything lower exhibits weird behaviour, I'm guessing if I raised the speed then it would need to be changed.



Answer



Something as simple as


this.velocity.i *= 0.9;

works nicely.



No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...