Why one of the balls does not move?

edited July 2016 in Questions about Code
float x [] = {10, 20, 30};
float xs [] = {2, 3, 5};
float y [] = {10, 20, 30};
float ys [] = {2, 3, 5};

void setup() {
  size(500, 500);
}
void draw() {
  background(0);
  for (int i = 0; i< 3; i++) {
    ellipse(x[i], y[i], 30, 30);
    x[i]= x[i]+xs[i];
    y[i]= y [i]+ys[i];


    if ((x[i]>width-15)||(x[i]<15)) {
      xs[i] = -xs[i];
    }

    if ((y[i]>height-15)||(y[i]<15)) {
      ys[i] = -ys[i];
    }
  }
}

Screenshot at Jul 30 23-46-49

Tagged:

Answers

  • edited July 2016 Answer ✓

    it stutters in the return zone because of * -1

    better to use abs() and -abs() separately:

    float x [] = {10, 20, 30}; 
    float xs [] = {2.3, 3.3, 5.2}; 
    float y [] = {10, 20, 30}; 
    float ys [] = {2.01, -3, 5.7};
    
    void setup() { 
      size(500, 500);
    } 
    void draw() { 
      background(0); 
      for (int i = 0; i< 3; i++) { 
        ellipse(x[i], y[i], 30, 30); 
        x[i]= x[i]+xs[i]; 
        y[i]= y [i]+ys[i];
    
        if ((x[i]>width-15)) {
          xs[i] = -abs(xs[i]);
        }
    
        if (x[i]<15) {
          xs[i] = abs(xs[i]);
        }
    
        if ((y[i]>height-15)) {
          ys[i] = -abs(ys[i]);
        }
    
    
        if (y[i]<15) {
          ys[i] = abs(ys[i]);
        }
      }
    } 
    
  • use ctrl-o to indent code when posting to the forum.

    if ((x[i]>width-15)||(x[i]<15)) {

    you wouldn't write a sentence without spaces, so why write codeallsquashedtogetherlikethis?

    if ((x[i] > width - 15) || (x[i] < 15)) {

    so much more readable

Sign In or Register to comment.