Loading...
Logo
Processing Forum
Heya, 
I'm having trouble setting boundaries for a moving red ball inside the top area of my program. Which is above the black line. 
I know that I need a condition which changes the direction of the ball once the radius of the ball goes near the edges of the program, but I'm not exactly sure how to implement it. 
After that I need to add a black ball, on the bottom middle which needs to be "shot" straight up after any key is pressed and if it hits the red ball then text "You Win!" is displayed, and if it misses then text "Game Over" is displayed; hence I need to code to know when there's a collision or not. For that part I have a vague idea of using the dist function to compare the sum of the two balls radiuses, and if it's less than the sum then it's a collision(display win text), otherwise not(display game over text). 
But I'm trying to solve one problem at a time, so boundaries first. :) 


So far I've got the red ball moving freely, and a line. 
Copy code
  1. float dia, centerX, centerY, speedX, speedY;
  2. PFont font; 


  3. void setup() {
  4.   size(200, 200);
  5.   background(255);
  6.   smooth(); 
  7.   font = createFont("Arial",16); // to fix text delay later when text is added in
  8.   textFont(font);
  9.   dia = 40;
  10.   centerX = random(dia/2, width-dia*2);
  11.   centerY = random(dia/2, height-dia*2);
  12.   speedX = random(1, 3); 
  13.   speedY = random(1, 3);
  14.   
  15. // condition needs to go here
  16. }

  17. void draw() {
  18.   background(255);
  19.   stroke(0);
  20.   line(0,60,200,60);
  21.   fill(255, 0, 0);
  22.   noStroke();
  23.   ellipse(centerX, centerY, dia, dia);
  24.   centerX+=speedX;
  25.   centerY+=speedY;
  26. }

Replies(5)

I had a look, and they're both very different to from what I was thinking. Is there a way to set it up without using classes or methods? 
Use the links for the remainder, it is the same idea with different variable names
Copy code
  1. float dia, centerX, centerY, speedX, speedY;
  2. int leftRight, upDown;
  3. PFont font;

  4. void setup() {
  5.   size(200, 200);
  6.   background(255);
  7.   smooth(); 
  8.   font = createFont("Arial", 16);
  9.   textFont(font);
  10.   dia = 40;
  11.   centerX = random(dia/2, width-dia/2);
  12.   centerY = random(dia/2, 60-dia/2);
  13.   speedX = random(1, 3); 
  14.   speedY = random(1, 3);
  15.   leftRight = 1;
  16.   upDown = 1;
  17. }

  18. void draw() {
  19.   background(255);
  20.   stroke(0);
  21.   line(0, 60, 200, 60);
  22.   fill(255, 0, 0);
  23.   noStroke();
  24.   ellipse(centerX, centerY, dia, dia);
  25.   centerX += speedX*leftRight;
  26.   centerY += speedY*upDown;
  27.   if (centerX <= dia/2 || centerX >= width-dia/2) leftRight *= -1;
  28.   if (centerY <= dia/2 || centerY >= 60-dia/2) upDown *= -1;
  29. }
Thanks for that :) I'll try and figure out how to do the rest from the links. If I run into any problems ill ask back here
Also take a look at the Technical FAQ... The first articles (minus the very first one) can help.