Ball bouncing on a grid

Hello,

I recently started learning processing and I have been trying to write a code in which a ball is bouncing off the four corners of a grid.
Can someone plz help me with this?

float x = 150;
float y = 150;

void setup() {
size(800, 800);
}

void draw() {
background(0, 0, 255);
stroke(255);

//VERTICAL LINES
for (float x = 150; x < 700; x = x + 50) {
line(x, 150, x, 650);
}
//HORIZONTAL LINES
for (float y = 150; y < 700; y = y + 50) {
line(150, y, 650, y);
}

//Ball
noStroke();
ellipseMode(CENTER);
ellipse(x, y, 20, 20);
x = x + 5;

if (x > 650) {
x = 650;
y = y + 5;
}
if (y > 650) {
x = x - 10;
}

if (x < 150) {
x = 150;
y = x + 5;
}
}

What do you mean the ball bounces from the four corners? Please provide more details.

What does your code do at the moment?

Kf

Hello thank you for your reply,

Yes you are correct. I made a grid and then on that grid right now the ball starts moving from x and y axis 150 towards x axis 650 and then from 650 it bounces and starts moving down and then once it reaches 650 on y axis, here the ball slightly goes off the grid and starts moving towards the left, after that the ball stops.

I have looked at all the tutorials on youtube but I am not being able to figure the movement. The movement which I am trying to achieve is right, down, left and up and this keeps going on in a loop.

Studio.ProcessingTogether.com/sp/pad/export/ro.9DEx177ceWoBc

Thank you so much for the link, this is the movement which I am trying to achieve but the code looks a bit complicated. I am still at the beginners level. Would you know if there is a basic code to achieve this movement?

That’s the only code I’ve come up w/ in replying to this very old forum thread: :woozy_face:

Well, you can simply ask which parts of the code you’re confused about. :sunglasses:

1 Like

some things are just needed, like the limit check,
other things, like PVector are optional, but you are here for learning?

int start = 150, stop = 650, x, y;
int br = 10, step = 5, dirx = 1, diry = 1;

void setup() {
  size(800, 800);
  ellipseMode(CENTER);
  stroke(255);
  x = (int)random(start,stop);
  y = (int)random(start,stop);
}

void draw() {
  background(0, 0, 255);
  for (float x = start; x <= stop; x = x + 50)  line(x, start, x, stop);    //VERTICAL LINES
  for (float y = start; y <= stop; y = y + 50)  line(start, y, stop, y);    //HORIZONTAL LINES
  ellipse(x, y, 2*br, 2*br);                                                //Ball
  move();
}

void move(){
   x += step*dirx;
   y += step*diry;
   if ( x <= start+br+step ) dirx = 1;
   if ( y <= start+br+step ) diry = 1;
   if ( x >= stop-br-step  ) dirx = -1;
   if ( y >= stop-br-step  ) diry = -1;
}

1 Like

Okay, let me go through the link which you sent. I will try to fix this by myself first and still if I don’t get the movement right, I will get back to you. :slight_smile:

Hi, thank you for your reply. Its been few weeks that I started learning Processing, I am gonna go through the code which you sent and try to work on it again. I have been looking at tutorials on youtube and still trying to figure a better platform to learn. :slight_smile:

You can use the 1st version 1.0 from the forum thread link, which doesn’t have isClockwise. So it’s easier to understand. :angel:

1 Like