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

From CDOT Wiki
Jump to: navigation, search
(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 …')
 
Line 1: Line 1:
 
<source lang="JavaScript">
 
<source lang="JavaScript">
 
/*
 
/*
 
 
   FSOSS 2010
 
   FSOSS 2010
 
 
   Andor Salga
 
   Andor Salga
 
 
   Example of timing
 
   Example of timing
 
 
*/
 
*/
  
 
// pixels/second
 
// pixels/second
 
 
// 250 will go from 0 to 500 in 2 seconds
 
// 250 will go from 0 to 500 in 2 seconds
 
 
float velocity = 250.0f;
 
float velocity = 250.0f;
 
 
float position = 0.0f;
 
float position = 0.0f;
 
 
int deltaTimer = 0;
 
int deltaTimer = 0;
 
 
boolean wasteCycles = false;
 
boolean wasteCycles = false;
  
 
void setup(){
 
void setup(){
 
 
   size(500, 500);
 
   size(500, 500);
 
 
}
 
}
  
 
void draw(){
 
void draw(){
 
 
   background(#663399);
 
   background(#663399);
 
 
 
  
 
   // displacement = velocity * seconds
 
   // displacement = velocity * seconds
 
 
   position += velocity * getDelta()/1000.0f;
 
   position += velocity * getDelta()/1000.0f;
  
 
   // Wrap the object
 
   // Wrap the object
 
 
   if(position > 500){
 
   if(position > 500){
 
 
     position = 0;  
 
     position = 0;  
 
 
   }
 
   }
 
 
 
  
 
   // 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++){
 
 
       for(int j = 0; j < 10000; j++){
 
       for(int j = 0; j < 10000; j++){
 
 
       }
 
       }
 
 
     }
 
     }
 
 
   }
 
   }
 
 
 
  
 
   rect(position, height/2.0f, 40, 40);
 
   rect(position, height/2.0f, 40, 40);
 
 
}
 
}
  
 
/*
 
/*
 
 
   Returns the number of milliseconds which  
 
   Returns the number of milliseconds which  
 
 
   have elapsed since last draw
 
   have elapsed since last draw
 
 
*/
 
*/
  
 
float getDelta(){
 
float getDelta(){
 
 
   float millisElapsed = millis() - deltaTimer;
 
   float millisElapsed = millis() - deltaTimer;
 
 
   deltaTimer = millis();
 
   deltaTimer = millis();
 
 
   return millisElapsed;
 
   return millisElapsed;
 
 
}
 
}
 
 
</source>
 
</source>
 
[http://studio.sketchpad.cc/kSYgu9ZvV8 Run me]
 
[http://studio.sketchpad.cc/kSYgu9ZvV8 Run me]

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