Problem with movement of ball and its speed

Hello, I’m really new to Processing and programming in general so I’m having some hard time to realize my project. I’m kinda lost right now and I’d really appreciate some help.
So, I’m having this code, in which I want to achieve two goals:

a) Make the ball move on the grid at random directions, kinda like following directions from a GPS like:
right, right, up, right, down, left, left, but for each direction I have set some rules:
If right is chosen, then move from current X to new X by moving 1 by 1 pixels. When the ball reaches the new X position then it should follow a new direction order.

To sum up the problem, the ball is disappearing off the screen and I can’t find the error in the code.

b) I want the speed of Movement Class to be different than the rest of the sketch. In simple words I want the ball to move 1 by 1 pixel but do this movement fast. However I want the random parts of the code like random color of the grid, random space between grains and random stroke weight of the grains to be slower.

Sorry for the lame explanation… Any help would be really appreciated. Thanks in advance and have a nice day! :slight_smile:

int space = 50; //space for grid, 
int radius = 20; //radius of ball
Grid grid1;
Movement movement1;

void setup() {
  size(601, 601, P2D);
  frameRate(30);
  grid1 = new Grid();
  movement1 = new Movement(space, 1); //speed/amount of step, delay time
}

void draw() {
  background(0);
  grid1.display();
  movement1.display();
  movement1.move();
 
 //grains
  for (int i = 0; i < width; i = i+int(random(2, 100))) {
    for (int j = 0; j < height; j = j+int(random(2, 100))) {
      strokeWeight(random(1, 2.5));
      stroke(255);
      point(i, j);
    }
  }
}

class Movement {
  
  int stepsize; //stepsize
  int s; // value for switching cases
  int d; // delay time
  int x, y; // position
  
  //constractor
  Movement(int tempStSz, int tempD) {
    stepsize = tempStSz;
    d = tempD;
    x = width / 2;
    y = height / 2;
  }
  
  void move() { 
    boolean b = true; // a way for infinite loop
    while (b == true) {
      s = int(random(4))+1; // 1,2,3,4 translates to up, right, down, left
      delay(d); // delays/stops the loop for t amount of time
      switch(s) {
      case 1: 
        println("up");
        for (int i = 0; i < stepsize; i++) {     
          y += i;
        }
        if (y > height) {
          y *= -1;
        }
        break;
        
      case 2: 
        println("right");
        for (int i = 0; i < stepsize; i++) {       
          x += i;
        }
        if (x > width) {
          x *= -1;
        }
        break;
        
      case 3: 
        println("down");
        for (int i = 0; i < stepsize; i++) {      
          y -= i;
        }
        if (y < 0) {
          y *= -1;
        }
        break;
        
      case 4: 
        println("left");
        for (int i = 0; i < stepsize; i++) {      
          x -= i;
        }
        if (x < 0) {
          x *= -1;
        }
        break;
      }
      return; // return values of x and y to the ellipse
    }
  }
  
  void display() {
    noStroke();
    ellipseMode(CENTER);
    fill(0, int(random(190)), 255);
    ellipse(x, y, radius*2, radius*2);
  }
}


class Grid {
  
  int x, y; // location
  
  //constractor
  Grid() {
    x = 0;
    y = 0;
  }
  
  void display() {
    for (int x = 0; x < width; x += space) {
      for (int y = 0; y < height; y += space) {
        strokeWeight(1);
        stroke(0, int(random(255)), 0);
        line(0, y, width, y);
        line(x, 0, x, height);
      }
    }
  }
}

sorry, possibly you expect us to check your code…
but i had a different idea,
your description text made me think about a general
moving object intro:

/*
file: movement_speed.pde
 
 besides just to draw one thing ( example a static picture )
 processing can be used to run dynamic sketches.
 
 -a- for this need the 
 
 void setup(){}
 void draw() {}
 
 structure.
 
 -b- 
 the next big decision is
 want you paint more things above each other...
 or you need to clear the screen every time the draw loop runs
 and redraw all you have designed ( and stored to functions, array or classes )
 
 void draw() {
 background(200,200,0);
 }
 
 -c-
 now this draw loop sequenze you could see as your basic BEAT
 
 and has a default of 60 screens per second
 can be changed and will change depending on the available hardware
 and amount of code / work you give processing to do, check
 https://processing.org/reference/frameRate.html
 
 void setup() {
   size(200, 200);
   frameRate(120);                              // set your draw BEAT
 }
 
 void draw() {
   background(200, 200, 0);
   fill (0, 0, 200);
   text(nf(frameRate, 1, 1)+" FPS", 10, 10);   // show your draw BEAT
 }
 
 -d-
 to show a movement we need first a object
 circle(x,y,diam);
 but better directly make it a function
 
 void draw_circle(float x, float y, float diam) {
   push();
   stroke(200,0,0);
   strokeWeight(2);
   fill(0,200,0);
   circle(x,y,diam);  
   pop();
 }
 
 -e- 
 here comes the real fun: move it
 
 -e1- we want just move it horizontal:
 so 
 y = 50;                    // fix
 x +=1;                     // move right one pix
 if ( x > width ) x = 0;    // wrap it to beginning
 
 
 -e2- 
 we move one pix every draw loop execution
 so the speed is frameRate * 1 pix per sec
 to change that we can
 + + change the frameRate(newspeed);
 + + change the step size 
 x += 0.1;      // slower
 x += 2         // faster
 or to slow down
 -e3-
 use a general draw loop counter
 and use modulo to generate a object specific beat divider
 loopcount++;
 if ( loopcount%3 == 0 ) x++;
 
 -f- now we show this for several objects, means we need several xi
 ( and also yi and divider speeddivi )
 
 */

float x0=0, y0=50,  diam0=20, dx0 = 1, beat0, speeddiv0=1;  // 1 fastest acc frameCount setting, faster only with bigger dx
float x1=0, y1=100, diam1=30, dx1 = 1, beat1, speeddiv1=5;
int loopcount;

void setup() {
  size(200, 200);
  frameRate(120);                              // set your BEAT
}

void draw() {
  background(200, 200, 0);
  loopcount++;
  fill (0, 0, 200);
  text(nf(frameRate, 1, 1)+" FPS", 10, 10);   // show your BEAT
  c0();
  c1();
}

void c0() {
  draw_circle(x0, y0, diam0);
  beat0 = loopcount%speeddiv0;  
  if ( keyPressed ) println("loopcount "+loopcount+" beat0 "+beat0);
  if ( beat0 == 0 ) x0+=dx0;        // but only every n loop  //  x0 +=1;  // move right one pix
  if ( x0 > width +diam0/2) x0 = -diam0/2;                    // wrap it to beginning
}

void c1() {
  draw_circle(x1, y1, diam1);
  beat1 = loopcount%speeddiv1;  
  if ( keyPressed ) println("loopcount "+loopcount+" beat1 "+beat1);
  if ( beat1 == 0 ) x1+=dx1;        // but only every n loop  //  x1 +=1;  // move right one pix
  if ( x1 > width +diam1/2) x1 = -diam1/2;                    // wrap it to beginning
}

void draw_circle(float x, float y, float diam) {
  push();
  stroke(200, 0, 0);
  strokeWeight(2);
  fill(0, 200, 0);
  circle(x, y, diam);  
  pop();
}

Your sketch frameRate is how often draw() draws to the screen. You could:

  1. each frame, take several pixel steps, then draw
  2. each frame, take one pixel step, then draw

Your other changes (like background) will probably happen every several frames, not every frame.

A common way of filtering actions by regular draw frames is if(frameCount%beat==0){}. Now if beat is 17, the action will occur every 17 fames.

1 Like

In your Movement.move() – don’t use delay() like that. That is not what it is for.

https://processing.org/reference/delay_.html

This doesn’t do what you think it does:

       if (y > height) {
          y *= -1;
        }

You probably meant += – I think you are trying to bump the ball back inside the bounds. Instead, if the ball goes to 602, you move it to -602 – so instead of being 1 pixel below the screen, it is now 602 pixels above the screen.

1 Like