Difference between revisions of "FSOSS 2010/processing.js/timing"

From CDOT Wiki
Jump to: navigation, search
 
Line 30: Line 30:
 
   // Regardless of lag, garbage collection or rendering speed,   
 
   // Regardless of lag, garbage collection or rendering speed,   
 
   // position will increase by the correct velocity.
 
   // position will increase by the correct velocity.
 
 
   if(wasteCycles){
 
   if(wasteCycles){
 
     for(int i = 0; i < 10000; i++){
 
     for(int i = 0; i < 10000; i++){
Line 45: Line 44:
 
   have elapsed since last draw
 
   have elapsed since last draw
 
*/
 
*/
 
 
float getDelta(){
 
float getDelta(){
 
   float millisElapsed = millis() - deltaTimer;
 
   float millisElapsed = millis() - deltaTimer;

Latest revision as of 15:11, 27 October 2010

/*
  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