# Can you help me fix problem in the code

**URL:** <https://discourse.processing.org/t/can-you-help-me-fix-problem-in-the-code/35512>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [March 1, 2022, 11:56pm UTC](https://discourse.processing.org/t/can-you-help-me-fix-problem-in-the-code/35512 "2022-03-01T23:56:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cuber1](https://avatars.discourse-cdn.com/v4/letter/c/d6d6ee/32.png) [@Cuber1](https://discourse.processing.org/u/Cuber1)\
**Post date:** [March 1, 2022, 11:56pm UTC](https://discourse.processing.org/t/can-you-help-me-fix-problem-in-the-code/35512/1 "2022-03-01T23:56:35Z")

</div>

\<\> please format code with \</\> button \* [homework policy](https://discourse.processing.org/faq/#homework) \* [asking questions](https://discourse.processing.org/t/2147)  
When my ball goes to the top of the rect, It will be put in the center rect. However, I want it bouncing. I don’t know how to fix it. Someone can help me fix it. Thank you.

> 

float ballX;  
float ballY;  
float ballSpeedX = 4;  
float ballSpeedY = 4;  
float ballDia = 20.0;  
float ballR = ballDia/2;

void setup() {  
size(800, 600);  
ballX= random(10, 370);  
ballY= random(10, 570);  
}

void draw() {  
background(0);

// Draw the wall  
fill(255);  
noStroke();  
rect(390, 300, 20, 300);

// Color of the ball changes gradually and continuously as it moves  
fill(ballX,ballY,ballX%ballY);

// Draw the ball  
ellipse(ballX, ballY, ballDia, ballDia);

// Update ball location  
ballX=ballX+ballSpeedX;  
ballY=ballY+ballSpeedY;

// If ball hit the right or left change direction of X  
if (ballX\> width- ballR || ballX \< ballR) {  
ballSpeedX=ballSpeedX\*-1;  
}

// If ball hit top or bottom change direction of Y  
if (ballY\> height-ballR || ballY \< ballR) {  
ballSpeedY=ballSpeedY\*-1;  
}  
// Setup variable to true if shapes are over lapping, false if not  
boolean collision = hitWall(390, 300, 20, 300, ballX, ballY, ballR);

// Change ball direction when ball hit  
//and bump it back  
if (collision ==true) {  
ballSpeedX=ballSpeedX\*-1;  
ballX +=ballSpeedX;  
}  
}  
boolean hitWall(int rx, int ry, int rw, int rh,  
float cx, float cy, float cr) {

float circleDistanceX= abs(cx - rx - rw/2);  
float circleDistanceY= abs(cy - ry - rh/2);

if (circleDistanceX \> (rw/2 + cr)) {  
return false;  
}  
if (circleDistanceY \> (rh/2 + cr)) {  
return false;  
}  
if (circleDistanceX \<= rw/2) {  
return true;  
}  
if (circleDistanceY \<= rh/2) {  
return true;  
}

float cornerDistance = pow(circleDistanceX - rw/2, 2) +  
pow(circleDistanceY - rh/2, 2);  
if (cornerDistance \<=pow(cr, 2)) {  
return true;  
} else {  
return false;  
}  
}

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 2, 2022, 8:04pm UTC](https://discourse.processing.org/t/can-you-help-me-fix-problem-in-the-code/35512/2 "2022-03-02T20:04:46Z")

</div>

I got rid of the random and these values **show exactly what you mean**

```auto
float ballX=399-24*4;
float ballY=300-24*4;

float ballSpeedX = 4;
float ballSpeedY = 4;

float ballDia = 20.0;
float ballR = ballDia/2;

void setup() {
  size(800, 600);
  //ballX= random(10, 370);
  //ballY= random(10, 570);
}

void draw() {
  background(0);

  // Draw the wall
  fill(255);
  noStroke();
  rect(390, 300, 
    20, 300);

  // Color of the ball changes gradually and continuously as it moves
  fill(ballX, ballY, ballX%ballY);

  // Draw the ball
  ellipse(ballX, ballY, ballDia, ballDia);

  // Update ball location
  ballX=ballX+ballSpeedX;
  ballY=ballY+ballSpeedY;

  // If ball hit the right or left change direction of X
  if (ballX> width- ballR || ballX < ballR) {
    ballSpeedX=ballSpeedX*-1;
  }

  // If ball hit top or bottom change direction of Y
  if (ballY> height-ballR || ballY < ballR) {
    ballSpeedY=ballSpeedY*-1;
  }
  // Setup variable to true if shapes are over lapping, false if not
  boolean collision = hitWall(390, 300, 20, 300, 
    ballX, ballY, ballR);

  // Change ball direction when ball hit
  //and bump it back
  if (collision ==true) {
    ballSpeedX=ballSpeedX*-1;
    ballX +=ballSpeedX;
  }
}
boolean hitWall(int rx, int ry, int rw, int rh, 
  float cx, float cy, float cr) {

  float circleDistanceX= abs(cx - rx - rw/2);
  float circleDistanceY= abs(cy - ry - rh/2);

  if (circleDistanceX > (rw/2 + cr)) {
    return false;
  }
  if (circleDistanceY > (rh/2 + cr)) {
    return false;
  }
  if (circleDistanceX <= rw/2) {
    return true;
  }
  if (circleDistanceY <= rh/2) {
    return true;
  }

  float cornerDistance = pow(circleDistanceX - rw/2, 2) +
    pow(circleDistanceY - rh/2, 2);
  if (cornerDistance <=pow(cr, 2)) {
    return true;
  } else {
    return false;
  }
}

```

---

<div class="post-metadata">

**Author:** ![Cuber1](https://avatars.discourse-cdn.com/v4/letter/c/d6d6ee/32.png) [@Cuber1](https://discourse.processing.org/u/Cuber1)\
**Post date:** [March 2, 2022, 8:27pm UTC](https://discourse.processing.org/t/can-you-help-me-fix-problem-in-the-code/35512/3 "2022-03-02T20:27:24Z")

</div>

![BouncingBall](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/f33dbc7808c10f751316a6fbf3233419decf6f55.png)  
That is the reason I need your help. I think it had happened in the collision when it was true. I need to add more. However, I don’t know. I am a newbie. My skill is weak in the loop and boolean. I hope you understand

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 2, 2022, 8:53pm UTC](https://discourse.processing.org/t/can-you-help-me-fix-problem-in-the-code/35512/5 "2022-03-02T20:53:28Z")

</div>

see [Collision Detection - Happy Coding](https://happycoding.io/tutorials/processing/collision-detection#collision-detection-with-moving-objects)

---

<div class="post-metadata">

**Author:** ![Cuber1](https://avatars.discourse-cdn.com/v4/letter/c/d6d6ee/32.png) [@Cuber1](https://discourse.processing.org/u/Cuber1)\
**Post date:** [March 2, 2022, 10:18pm UTC](https://discourse.processing.org/t/can-you-help-me-fix-problem-in-the-code/35512/6 "2022-03-02T22:18:57Z")

</div>

thank you so much. I get it 😀 😀 😀

```auto
if ( ballX + ballSpeedX > 390 - ballR && 
       ballX +ballSpeedX < 410+ballR && 
       ballY >300-ballR && 
       ballY< 300+300) {
    ballSpeedX=ballSpeedX*-1-1;
  }

  // If ball hit the right or left change direction of X
  else if (ballX> width- ballR || ballX < ballR) {
    ballSpeedX=ballSpeedX*-1;
  }
  else 
    ballSpeedX = ballSpeedX +0.01;

  if (ballX > 390-ballR &&
    ballX < 410+ballR &&
    ballY + ballSpeedY > 300 - ballR &&
    ballY + ballSpeedY < 300 + 300) {
    ballSpeedY=ballSpeedY*-1;
  }

  // If ball hit top or bottom change direction of Y
  else if (ballY> height-ballR || ballY < ballR) {
    ballSpeedY=ballSpeedY*-1;
  }
  else 
    ballSpeedY = ballSpeedY+0.015;

```
