Crazy ball movement and collision

Hi,

So i’m new to processing and coding as i just started talking a Comp. Sci class this year. Anyways, for the end of the first semester, we were tasked with making a game. Doesn’t matter what kind or how, just as long as you make one. Anyways, i made what i have currently, it’s really messy because i had to move everything from there own separate function into Draw(); to make things easier on me(and i also just suck at making legible things). However, after doing this, and getting everything to work, I realized that the “Puck” no longer works properly. Instead of bouncing off the wall like it should, it instead just repeatedly goes back and forth bouncing along the wall. I’ve been staring at the computer, trying everything for about 3 hours today. I checked to see if it was order, if i accidentally changed or used the wrong variable and nothing seems to change.
Another thing i need help on is getting the 2 balls to collide. I’ve looked through all of the forums and i couldn’t find something that worked. Any help would be greatly appreciated.

Here is my code:

float bx, by, bw, bh, bvs, bhs;
boolean Lside, Rside, down, up, begin, reset;
float px, py, pw, ph, psx, psy, psxv, psyv;
float angle = random(TWO_PI);
int score = 0;
int lost = 0;

void setup() 
{
  size(650, 700);
  frameRate(60);
  ellipseMode(CENTER);
  bx = width/2;
  by = height/2;
  bw = 100;
  bh = 100;
  bvs = 4;
  bhs = 4;
  
  px = 325;
  py = 100;
  pw = 60;
  ph = 60;
  psx = 6 + (int)random(5);
  psy = 6 + (int)random(5);
}

void draw() {
  
  background(0);
  if(begin != true || reset == true)
  {
    begin = false;
    reset = false;
    lost = 0;
  psx = 0;
  psy = 0;
  {
   rectMode(CENTER);
   fill(255,0,0);
   ellipse(bx, by, bw, bh);
  }
  {
  textSize(100);
  fill(0,255,255);
  text("Paddle Ball", 60, 150);
  }
  {
  textSize(25);
  fill(0,255,255);
  text("Bouncer of Balls", 220, 200); 
  }
  {
  textSize(50);
  fill(0,255,255);
  text("Press T to Start", 125, 500);
  }
  }
  if(begin || reset){
  {
  psx = 6 + (int)random(5);
  psy = 6 + (int)random(5);
    textSize(100);
    text(score, 40, 100);
    if(lost == 1){
 psx = 0;
 psy = 0;
 textSize(100);
 text(score, 40, 100);
 textSize(100);
 text("FAILURE!", 120, 225);
 textSize(40);
 text("Press P to Restart", 160, 300);
    }


    fill(255);
    ellipse(bx, by, bw, bh);
    
    

{
  if(up)
  by = by - bvs;
  if(down)
  by = by + bvs;
  if(Lside)
  bx = bx - bhs;
  if(Rside)
  bx = bx + bhs;
  }
{
  if(by - bh/2 < 325)
  by = by + bvs;
  if(by - bh/2 > 600)
  by = by - bvs;
  if(bx - bw/2 < 0)
  bx = bx + bhs;
  if(bx - bw/2 > 550)
  bx = bx - bhs;
}
{
  if(py > 670) {
    py = 100;
    px = 325;
    psx = 6 + (int)random(5);
    psy = 6 + (int)random(5);
  }
}
{
  {
    {
  fill(255,0,100);
  ellipse(px, py, pw, ph);
  }
  if(px > 620)
  psx = -psx;
  
  else if(px < 20)
  psx = -psx;
  
  
  if(py < 20)
  psy = -psy;
  
  px = px + psx;
  py = py + psy;
  if(py > 670){
    lost = 1;
  }
  if(py < 0){
    score++;
  }
  }
  }
  }
  }
}


void keyPressed() {
  if(key =='w' || key == 'W')
  up = true;
  if(key =='s' || key == 'S')
  down = true;
   if(key =='a' || key == 'A')
  Lside = true;
   if(key =='d' || key == 'D')
  Rside = true;
  if(key =='t' || key == 'T')
  begin = true;
  if(key =='p' || key == 'P')
  reset = true;
}
void keyReleased() {
    if(key =='w' || key == 'W')
  up = false;
  if(key =='s' || key == 'S')
  down = false;
   if(key =='a' || key == 'A')
  Lside = false;
   if(key =='d' || key == 'D')
  Rside = false;
}
1 Like

It’s not a whole lot of code so I’d suggest tidying it up and the solution should become clearer. Already I see a lot of curly brackets that have no use. You only need to use a curly bracket for opening and closing functions (like if()), that’s it. and a lot of the if statements have no curly brackets and are kinda confusing.

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

Another tip I’d give is to use words like ‘speed’ and ‘position’ and ‘puck’ when declaring variables because it is much easier to tell what is going on.

Also your ball may be going crazy because the collision with the wall may be innaccurate? I looked at it and I’m not sure why it fails. It may be because you handle the position and veloicity correction in two different areas of the code.

As for collision I’d check out this page, many easy to follow examples

2 Likes

Unrelated comment from me but thank you so much Benny I’ve been looking for a resource like this for a long time.

I know right??? It’s so useful and I’m surprised it took me so long to find

I even found many articles about intersection stuff all wrapped up in one page! :joy:

http://www.realtimerendering.com/intersections.html

1 Like