Random position ellipse smear/freeze

Hi I am doing chapter 7-10 in learning processing. And want to do a jiggeling UFO. Somehow in my code it makes the graphic not update correctly. It smears freezes (paints over one an other) Hope someone can help me what I am doing wrong here?

My Code:

float x = 200;
float y = 200;
float theSize = 200;
float offset = theSize/4;

void setup() {
size (400, 400);

}

void draw() {
jiggleUFO(2);

// Draw Main UFO Body
ellipseMode(CENTER);
fill(0);

ellipse(x, y, theSize, theSize/3);
ellipse(x, y-(offset/2), theSize/3, theSize/3);
fill(255);

ellipse(x-offset, y, theSize/8, theSize/8);
ellipse(x, y, theSize/8, theSize/8);
ellipse(x+offset, y, theSize/8, theSize/8);

}

void jiggleUFO(float speed) {

x = x + random(-1, 1)*speed;
y = y + random(-1, 1)*speed;
// Constrain UFO to window
x = constrain(x, 0, width);
y = constrain(y, 0, height);
}

Hi
I think you need background



float x = 200;
float y = 200;
float theSize = 200;
float offset = theSize/4;
float speed= 200;
void setup() {
size (400, 400);

}

void draw() {
background (255);
jiggleUFO(2);

// Draw Main UFO Body
ellipseMode(CENTER);
fill(0);

ellipse(x, y, theSize, theSize/3);
ellipse(x, y-(offset/2), theSize/3, theSize/3);
fill(255);

ellipse(x-offset, y, theSize/8, theSize/8);
ellipse(x, y, theSize/8, theSize/8);
ellipse(x+offset, y, theSize/8, theSize/8);

}

void jiggleUFO(float speed) {

x = x + random(-1, 1)*speed;
y = y + random(-1, 1)*speed;
// Constrain UFO to window
x = constrain(x, 0, width);
y = constrain(y, 0, height);
}

Hello @Tilman,

These should help you to understand what is happening:

:)

perfect! Thank you @jafal ] @glv

:mag_right: :eyeglasses:
*

“It is common to call background() near the beginning of the draw() loop to clear the contents of the window, as shown in the first example above. Since pixels drawn to the window are cumulative, omittingbackground() may result in unintended results.”

1 Like

Thanks again. Maybe you can help me further :slight_smile:
Now I run into another problem with completing the exercise. I want to make these UFOs Jiggle in a patch where I call several instances. So as you see above I get it to work for one instance now.

it seems the void jiggleUFO(float speed) “function?” here does have no effect. I try to understand why. I have not yet learned about dbugging.

// code goes here

float x = 100;
float y = 100;
float theSize = 200;
float offset = theSize/4;
float speed= 200;

void setup() {
  size(640, 360);
}

void draw() {
background(255);
jiggleUFO(2);
spaceship (482, 159, 223);
spaceship (126, 89, 93);
spaceship (422, 286, 84);
spaceship (294, 49, 48);
spaceship (162, 220, 151);

}
void spaceship (float x, float y, float size) {
// Draw Main UFO Body  
ellipseMode(CENTER);
fill(0);
ellipse(x, y, theSize, theSize/3);
ellipse(x, y-(offset/2), theSize/3, theSize/3);
fill(255);
// Draw Main UFO Windows
ellipse(x-offset, y, theSize/8, theSize/8);
ellipse(x, y, theSize/8, theSize/8);
ellipse(x+offset, y, theSize/8, theSize/8);
}
  
void jiggleUFO(float speed) {
x = x + random(-1, 1)*speed;
y = y + random(-1, 1)*speed;
// Constrain UFO to window
x = constrain(x, 0, width);
y = constrain(y, 0, height);
}

// code ends here

@Tilman,

I fixed the link to the “Processing Overview” tutorial.

:)