Open main menu

CDOT Wiki β

FSOSS 2010/processing.js/timing

Revision as of 15:10, 27 October 2010 by Asalga (talk | contribs) (Created page with '<source lang="JavaScript"> FSOSS 2010 Andor Salga Example of timing: // pixels/second // 250 will go from 0 to 500 in 2 seconds float velocity = 250.0f; float …')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
/*

  FSOSS 2010

  Andor Salga

  Example of timing

*/

// pixels/second

// 250 will go from 0 to 500 in 2 seconds

float velocity = 250.0f;

float position = 0.0f;

int deltaTimer = 0;

boolean wasteCycles = false;

void setup(){

  size(500, 500);

}

void draw(){

  background(#663399);

  

  // displacement = velocity * seconds

  position += velocity * getDelta()/1000.0f;

  // Wrap the object

  if(position > 500){

    position = 0; 

  }

  

  // Regardless of lag, garbage collection or rendering speed,  

  // position will increase by the correct velocity.

  if(wasteCycles){

    for(int i = 0; i < 10000; i++){

      for(int j = 0; j < 10000; j++){

      }

    }

  }

  

  rect(position, height/2.0f, 40, 40);

}

/*

  Returns the number of milliseconds which 

  have elapsed since last draw

*/

float getDelta(){

  float millisElapsed = millis() - deltaTimer;

  deltaTimer = millis();

  return millisElapsed;

}

Run me