# Can you help me change the Speed of the ball?

**URL:** <https://discourse.processing.org/t/can-you-help-me-change-the-speed-of-the-ball/35470>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [February 27, 2022, 9:32pm UTC](https://discourse.processing.org/t/can-you-help-me-change-the-speed-of-the-ball/35470 "2022-02-27T21:32:45Z")\
**Posts on this page:** 9\
**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:** [February 27, 2022, 9:32pm UTC](https://discourse.processing.org/t/can-you-help-me-change-the-speed-of-the-ball/35470/1 "2022-02-27T21:32:46Z")

</div>

Reset the velocity back to its initial value once the ball collides with an edge of the internal wall or the canvas  
boundary. Note that the sign of the velocity will depend on the edge the ball hits. For example, after the ball  
hits the right wall, the x-velocity will be reset to -3. Implement sensible behavior for the ball. The ball should  
not be moving so fast that the graders cannot evaluate the behavior of your code.  
\<// Setup the value for the ball  
float ballX;  
float ballY;  
float ballSpeedX = random(2, 8);  
float ballSpeedY = random(1, 10);  
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, 23, 300);

// Color of the ball changes gradually and continuously as it moves  
if (ballSpeedX \>=6) {  
fill(255, 0, 0);  
} else if (ballSpeedX\>=3) {  
fill(0, 255, 0);  
} else fill(0, 0, 255);

// 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= width -ballR;  
ballSpeedX=ballSpeedX\*-1;  
} else if (ballX\<ballR) {  
ballX=ballR;  
ballSpeedX=ballSpeedX\*-1;  
}

// If ball hit top or bottom change direction of Y  
if (ballY\> height-ballR) {  
ballY=height-ballR;  
ballSpeedY=ballSpeedY\*-1;  
} else if (ballY\<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=(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;  
}  
}\> [homework policy](https://discourse.processing.org/faq/#homework)

---

<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:** [February 27, 2022, 9:50pm UTC](https://discourse.processing.org/t/can-you-help-me-change-the-speed-of-the-ball/35470/2 "2022-02-27T21:50:16Z")

</div>

You just copied the assignment

What’s your actual question?

Where do you see a need for change in your code?

You wrote:

> Can you help me change the Speed of the ball?

In what way?

* * *

```auto
// Setup the value for the ball
float ballX;
float ballY;
// speed gets modified in setup()
float ballSpeedX = random(1.5, 3.5);
float ballSpeedY = random(1.5, 3.5);
float ballDia = 20.0;
float ballR = ballDia/2;

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

  ballX= random(10, 370);
  ballY= random(10, 570);

  // make it neg. in some cases
  if (random(1) > 0.5) 
    ballSpeedX*=-1;
  if (random(1) > 0.5) 
    ballSpeedY*=-1;
}

void draw() {
  background(0);

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

  // Color of the ball changes gradually and continuously as it moves
  if (ballSpeedX>=6) {
    fill(255, 0, 0);
  } else if (ballSpeedX>=3) {
    fill(0, 255, 0);
  } else fill(0, 0, 255);

  // 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 = width-ballR;
    ballSpeedX=ballSpeedX*-1;
  } else if (ballX<ballR) {
    ballX=ballR;
    ballSpeedX=ballSpeedX*-1;
  }

  // If ball hit top or bottom change direction of Y
  if (ballY> height-ballR) {
    ballY=height-ballR;
    ballSpeedY=ballSpeedY*-1;
  } else if (ballY<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) {
    ballSpeedX=ballSpeedX*-1;
    ballX=(ballX+ballSpeedX);
  }//if
} // draw 

// -------------------------------------------------------------------------

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;
  }
}//func 
// End of Sketch 

```

---

<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:** [February 27, 2022, 10:44pm UTC](https://discourse.processing.org/t/can-you-help-me-change-the-speed-of-the-ball/35470/3 "2022-02-27T22:44:08Z")

</div>

the color of the ball has to change when speed increases or decreases. Reset the velocity back to its initial value once the ball collides with an edge of the internal wall or the canvas boundary. Note that the sign of the velocity will depend on the edge the ball hits. For example, after the ball hits the right wall, the x-velocity will be reset to -3. It is like: when the ball hit the wall ball has the speed is 6 or more the ball will red color. After hit the size, the speed will reduce 2 and have green. The color will be changed when the speed change.

---

<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:** [February 27, 2022, 11:36pm UTC](https://discourse.processing.org/t/can-you-help-me-change-the-speed-of-the-ball/35470/4 "2022-02-27T23:36:53Z")

</div>

In this code, the speed did not change. It just kept the pace when it started. It has only two values. However, I need the ball to have three different speeds and change three colors. Can you help me rewrite the code in the speed path?

---

<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:** [February 28, 2022, 12:00am UTC](https://discourse.processing.org/t/can-you-help-me-change-the-speed-of-the-ball/35470/5 "2022-02-28T00:00:32Z")

</div>

Should the speed increase for every collision…?

---

<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:** [February 28, 2022, 12:18am UTC](https://discourse.processing.org/t/can-you-help-me-change-the-speed-of-the-ball/35470/6 "2022-02-28T00:18:08Z")

</div>

Speed of the ball will increase when hitting the right, left, top, or bottom. it will decrease when hit the wall

---

<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:** [February 28, 2022, 12:25am UTC](https://discourse.processing.org/t/can-you-help-me-change-the-speed-of-the-ball/35470/7 "2022-02-28T00:25:42Z")

</div>

Okay please implement this

---

<div class="post-metadata">

**Author:** ![hsm](https://avatars.discourse-cdn.com/v4/letter/h/ea666f/32.png) [@hsm](https://discourse.processing.org/u/hsm)\
**Post date:** [February 28, 2022, 2:11am UTC](https://discourse.processing.org/t/can-you-help-me-change-the-speed-of-the-ball/35470/8 "2022-02-28T02:11:31Z")

</div>

If I understand correctly, what you need to do is change the value of certain variables based on certain conditions. Can you think of any way to do that?

---

<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:** [February 28, 2022, 11:44am UTC](https://discourse.processing.org/t/can-you-help-me-change-the-speed-of-the-ball/35470/9 "2022-02-28T11:44:10Z")

</div>

**This is wrong (what I wrote)**

> [@Chrisir](#):
>
> ```auto
> // If ball hit the right or left change direction of X
> if (ballX > width-ballR ) {
> ballX = width-ballR;
> ballSpeedX=ballSpeedX*-1;
> }
> 
> ```

here a collision takes place. Since it’s on the right side of the screen, ballSpeedX is posittive (you need to know if the value is currently pos or neg!),

so before ballSpeedX=ballSpeedX\*-1; say ballSpeedX+=0.2; for example. Same for ballSpeedY (is it pos or neg?), here we want to add 0.2 or -0.2 as well (so that the direction stays the same)

Repeat for the other collisions accordingly, keep in mind whether it’s neg or pos (if clause).

**New attempt**

> [@Cuber1](#):
>
> Reset the velocity back to its initial value once the ball collides with an edge of the internal wall or the canvas boundary.

first store the initial values globally.

YOU WROTE:

> [@Cuber1](#):
>
> Speed of the ball will increase when hitting the right, left, top, or bottom. it will decrease when hit the wall

Assignment says:

> [@Cuber1](#):
>
> Reset the velocity back to its initial value once the ball collides with an edge of the internal wall or the canvas boundary.

I think you are wrong in saying, the ball will increase when hitting the right, left, top, or bottom.

I suggest the ball gets faster throughout and Reset the velocity back to its initial value once the ball collides with an edge of the internal wall or the canvas boundary.
