We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Image array speed
Page Index Toggle Pages: 1
Image array speed (Read 739 times)
Image array speed
Apr 12th, 2010, 11:17pm
 
Howdy,

Is there a way to have an image array that moves at a different frame rate from the one defined in  void draw? the only way I can think of is duplicating the same frame so it appears to

Any suggestions or comments will be GREATLY appreciated  Smiley
Re: Image array speed
Reply #1 - Apr 13th, 2010, 1:24am
 
Not sure I got your question.
You have several options:
1) You can change the frame-rate of your sketch, but that would cause ALL drawings to change speed.
2) You act on one of your images only ever x-th frame, so that the value of x slows the movement.
3) The "speed" (the offset) you are moving your image with each redraw is different for your objects.

Sketch 1: 2 balls moving left-right with same speed. Pressing +/- changes the framerate.

Quote:
PVector Pos1;
PVector Pos2;
void setup()
 {
   size(600,100);
   stroke(255);
   strokeWeight(10);
   Pos1 = new PVector(20,30);  // just a starting position
   Pos2 = new PVector(20,80);  // just a starting position
 }
void draw()
{
  background(0);
  point(Pos1.x,Pos1.y);
  point(Pos2.x,Pos2.y);
  Pos1.x+=5;  // shift position 5 pixels to the right by adding 5
  Pos2.x+=5;  // shift position 5 pixels to the right by adding 5
  if (Pos1.x>=width) Pos1.x-=width;  // warp at right boarder
  if (Pos2.x>=width) Pos2.x-=width;  // warp at right boarder
}

void keyPressed()
 {
   if (key=='+') frameRate(min(30,frameRate+5));
   if (key=='-') frameRate(max(1,frameRate-5));
 } 


Seketch 2: Two balls at different speed by adding different "velocity"

Quote:
PVector Pos1;
PVector Pos2;
void setup()
 {
   size(600,100);
   stroke(255);
   strokeWeight(10);
   Pos1 = new PVector(20,30);  // just a starting position
   Pos2 = new PVector(20,80);  // just a starting position
 }
void draw()
{
  background(0);
  point(Pos1.x,Pos1.y);
  point(Pos2.x,Pos2.y);
  Pos1.x+=5;  // shift position 5 pixels to the right by adding 5
  Pos2.x+=1;  // shift position 1 pixel to the right by adding 1
  if (Pos1.x>=width) Pos1.x-=width;  // warp at right boarder
  if (Pos2.x>=width) Pos2.x-=width;  // warp at right boarder
}

void keyPressed()
 {
   if (key=='+') frameRate(min(30,frameRate+5));
   if (key=='-') frameRate(max(1,frameRate-5));
 } 


Sketch 3: Different speed by acting every 5th frame only.
Quote:
PVector Pos1;
PVector Pos2;
void setup()
 {
   size(600,100);
   stroke(255);
   strokeWeight(10);
   Pos1 = new PVector(20,30);  // just a starting position
   Pos2 = new PVector(20,80);  // just a starting position
 }
void draw()
{
  background(0);
  point(Pos1.x,Pos1.y);
  point(Pos2.x,Pos2.y);
  Pos1.x+=5;  // shift position 5 pixels to the right by adding 5
  if (frameCount%5==0) Pos2.x+=5;  // shift every 5th update only
  if (Pos1.x>=width) Pos1.x-=width;  // warp at right boarder
  if (Pos2.x>=width) Pos2.x-=width;  // warp at right boarder
}

void keyPressed()
 {
   if (key=='+') frameRate(min(30,frameRate+5));
   if (key=='-') frameRate(max(1,frameRate-5));
 } 
 

Re: Image array speed
Reply #2 - Apr 13th, 2010, 1:39am
 
Presumably you're talking about an array of images that forms an animation...  In which case you'd be interested in the speed at which you move from one image to the next to control the framerate of the animation.

I haven't tried any of the animation libraries but you may find one of them suits your needs.  Otherwise the best approach would actually be to use a time-based approach where you count millis() and trigger a new frame based on the time ellapsed...  Unless you're relying on the Processing framerate to control something else it's often recommended to set it to some arbitraryily high number (e.g. 999) to force the sketch to run as fast as possible - since at the default I guess there's the chance that the framerate of the sketch doesn't coincide with the framerate of the animation meaning a frame might get triggered a few milliseconds later than it should have; though TBH I'm not convinced this would be visible unless the sketch was running at a very slow rate...

Just make sure the images are preloaded in setup rather than trying to load each one in draw Wink
Re: Image array speed
Reply #3 - Apr 13th, 2010, 2:25am
 
I am aware of speed changing speed of moving objects, but not in regards to an image array without affecting everything in the sketch.

Thanks for the responses
Re: Image array speed
Reply #4 - Apr 13th, 2010, 3:16am
 
@Pauline: Maybe it's best you post/share some basic-script and explain what you want to achieve?
If it is about "cycling" through an image array at different speeds you could use the sketch #3 method of cycling through one image array only every x-th frame...
Re: Image array speed
Reply #5 - Apr 13th, 2010, 10:01am
 
Bejoscha wrote on Apr 13th, 2010, 3:16am:
@Pauline: Maybe it's best you post/share some basic-script and explain what you want to achieve
If it is about "cycling" through an image array at different speeds you could use the sketch #3 method of cycling through one image array only every x-th frame...


The only potential problem with that is that the sketch framerate is not necessarily consistent, so if you push the processor too hard you may encounter slowdown in your animation when using this approach.  Using a time-based approach you have the option to drop frames if the sketch can't keep up...
Page Index Toggle Pages: 1