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 & HelpPrograms › movement not smooth on screen
Page Index Toggle Pages: 1
movement not smooth on screen (Read 1964 times)
movement not smooth on screen
Mar 30th, 2010, 11:48pm
 
I have a very simple sketch (-just some balls flying and bouncing of the screen edges-), but when I run it, it does not seem to get to a smooth motion. Every now and then (~once in 5-10sec) it seems to "jitter" a bit.
Do others see the same effect when running my code, or is it due to processor speed? I run processing on an Intel Core2 Duo P8600 (2.4 Ghz) with 4 GB RAM on a 64-bit Vista and wouldn't have expected speed problems here.
Any comments? Something obviouse I can't see in the code?

Code:

ArrayList balls;
int nrBalls=4;
ball B;

void setup()
{
 frameRate(60);
 size(800,600);
 smooth();
 balls = new ArrayList();
 for (int i=0;i<nrBalls;i++)
     {
       B= new ball(random(0,width),random(0,height));
       B.speed = new PVector(4,3);
       balls.add(B);
     }
 stroke(255,255,255,150);
}
void draw()
{
 background(0);
 for (int i=0;i<balls.size();i++)
   {
     B = (ball) balls.get(i);
     strokeWeight(B.mass*2);
     point(B.pos.x,B.pos.y);
     
    // move ball
      B.pos.add(B.speed);
      constrain(B.pos.x,B.mass,width-B.mass);
      constrain(B.pos.y,B.mass,height-B.mass);
    // check boarder
      if (B.pos.x<=B.mass)        B.speed.x=abs(B.speed.x);
      if (B.pos.x>=width-B.mass)  B.speed.x=abs(B.speed.x)*(-1);
      if (B.pos.y<=B.mass)        B.speed.y=abs(B.speed.y);
      if (B.pos.y>=height-B.mass) B.speed.y=abs(B.speed.y)*(-1);
   }
}

class ball
{
 PVector speed;
 PVector pos;
 float mass;
 ball(float x, float y)
    {mass=int (random(10,50));speed=new PVector(0,0);pos=new PVector(x,y);}
}
Re: movement not smooth on screen
Reply #1 - Mar 31st, 2010, 1:18am
 
Yes, screen update isn't as good as one could expect, that's an old issue.
The flicker you see is because screen update isn't synchronous with screen refresh, so part of the screen displays the old frame while the other part displays the new one.

I thought I have read a long time ago that Processing does double buffering out of the box, but I am not sure anymore. Unless it is handled automatically in Java2D mode?
I tried P2D mode. Funnily, there, pixels are square... And still flicker, no surprise.
Same in P3D (square pixels, made of two triangles...).
And surprisingly, the issue is the same in OpenGL. But it might be related to the fact you use point() instead of ellipse() or rect().
Re: movement not smooth on screen
Reply #2 - Apr 1st, 2010, 12:47am
 
Hey, I actually suspect you are encountering garbage collection.  It will probably help to separate your timing from your rendering -- update your objects at a consistent rate according to the current millis(), and render them at their positions each frame.  This is the only way I know to guarantee the rate of motion on different machines, or avoid garbage collection stutter.
Re: movement not smooth on screen
Reply #3 - Apr 1st, 2010, 1:35am
 
BenHem, I thought about this too, as GC is a common cause of animation pausing.
But here, the issue isn't pausing, but flickering, or tearing of the shapes.
Beside, the draw() loop doesn't create any object (even behind the scene: Processing creates rarely objects itself), so GC shouldn't be triggered (or rarely, and it shouldn't make a visible pause if there are not much objects to collect).
Re: movement not smooth on screen
Reply #4 - Apr 1st, 2010, 3:28pm
 
PhiLo, oh, ok.  I couldn't reproduce that error...
Re: movement not smooth on screen
Reply #5 - Apr 2nd, 2010, 3:18am
 
Quote:
I thought I have read a long time ago that Processing does double buffering out of the box, but I am not sure anymore.


I think it does.

I tried removing the smooth() and framerate() calls, and even added an offscreen-buffer but it still flickers. Ouch. Just noticed that it stops flickering with smaller resolutions. Still flickers at 400x400, but looks OK at 200x200.

I hate flickering. Really.  Angry
Re: movement not smooth on screen
Reply #6 - Apr 3rd, 2010, 2:26am
 
so is there any solution for this?
I am a beginner in processing and I see the same problem with my simple sketches.

Page Index Toggle Pages: 1