having fun with code

Acceleration: slow down problem

Moving objects in computing can be handled easily keeping a speed variable, let’s say in pixels per second (object.speed.x = 10). Then, on every step of the animation, we change the object position with this amount divided by the number of frames per second we are processing (object.x += object.speed.x / fps). Acceleration can be achieved easily to. On every step of the animation we can modify the speed with an acceleration factor (object.speed.x += object.acceleration.x).

Slow down problem

Today at work I’ve been trying to solve this problem: Giving an object moving at 50 px/s I want it to slow down to 0 px/s in exactly 400px. How can we calculate the acceleration for this? I need an amount to subtract to the speed on every step, so by the time the object has moved 400px the speed is 0.

There are a few equations of motion that could be applied. In this case, we don’t care about how much time does it take to stop the object. So the formula is:

a=\frac{v^2 - v_0^2}{2*s}

a=\frac{-50^2}{2*400}=-3.13

In Javascript:

  1. this.acceleration = (Math.pow(this.finalSpeed,2) – Math.pow(this.speed,2)) / (2*this.brakingSpace);

Well, still it doesn’t work perfectly on my demo. The object, a 10px square div, decelerates properly, but stops at 375px instead of 400px. I’ll keep working on it.

The code

  1. var Brake = new Class({
  2.   Extends: Thread,
  3.  
  4.   initialize: function(options) {
  5.     this.parent(options);
  6.     this.item = $('item');
  7.     this.speed = 50;
  8.     this.finalSpeed = 0;
  9.     this.brakingSpace = 400;
  10.     this.currentPos = 0;
  11.  
  12.     this.acceleration = (Math.pow(this.finalSpeed,2) – Math.pow(this.speed,2)) / (2*this.brakingSpace);
  13.     $('acceleration').set('html', 'Acceleration: ' + this.acceleration.toFixed(2) + ' px/s<sup>2</sup>');
  14.   },
  15.  
  16.   execute: function() {
  17.     if (this.speed < 0) {
  18.       this.stop();
  19.       return;
  20.     }
  21.     this.item.setStyles({left: this.currentPos});
  22.     $('position').set('text', 'Position: ' + this.currentPos.toFixed(2) + ' px');
  23.     $('speed').set('text', 'Speed: ' + this.speed.toFixed(2) + ' px/s');
  24.  
  25.     this.speed      += this.acceleration;
  26.     this.currentPos += this.speed;
  27.   }
  28.  
  29. });

If I modify the position before adjusting the speed, the object goes farther than 400px. If I update the speed first (like above), the object stops before reaching 400px.

Problem solved

Turns out I wasn’t calculating the new position properly. The formula to calculate the new position is:

s=v - \frac{1}{2}a

Or in Javascript:

  1. this.currentPos += this.speed0.5*this.acceleration;

Here is the modified code:

  1. execute: function() {
  2.   this.speed += this.acceleration;
  3.   this.currentPos += this.speed0.5*this.acceleration;
  4.  
  5.   if (this.speed < 0) {
  6.     this.stop();
  7.     return;
  8.   }
  9.   this.item.setStyles({left: this.currentPos});
  10.   $('position').set('text', 'Position: ' + this.currentPos.toFixed(2) + ' px');
  11.   $('speed').set('text', 'Speed: ' + this.speed.toFixed(2) + ' px/s');
  12. }

Of course, now the demo is accurate :) Thanks Jacob.

Related Posts:

  • No Related Post

2 Comments to Acceleration: slow down problem

  1. January 21, 2009 at 01:36 | Permalink

    When calculating the new position you need to take the acceleration into account, as it is not constant over the small distance you’re moving the object. Take a look at equation 5 on the Wiki page you linked.

    Try something like
    this.currentPos += this.speed – 0.5 * this.acceleration;

Leave a Reply

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Additional comments powered by BackType

About the blog

This is a blog about development, focused mainly on Javascript but also other languages like python, shell scripts and more.

About the author

Eneko Alonso is a software engineer and UI developer with more than eight years of experience in software and web development. He lives in San Luis Obispo, California and works at LEVEL Studios.

Contact Info

Contact Info

PromoteJS

JavaScript JS Documentation