# Creating "boundaries" for my ball using max and min

**URL:** <https://discourse.processing.org/t/creating-boundaries-for-my-ball-using-max-and-min/17367>\
**Category:** Coding Questions\
**Created:** [January 27, 2020, 3:22am UTC](https://discourse.processing.org/t/creating-boundaries-for-my-ball-using-max-and-min/17367 "2020-01-27T03:22:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![AxFan](https://avatars.discourse-cdn.com/v4/letter/a/a88e57/32.png) [@AxFan](https://discourse.processing.org/u/AxFan)\
**Post date:** [January 27, 2020, 3:22am UTC](https://discourse.processing.org/t/creating-boundaries-for-my-ball-using-max-and-min/17367/1 "2020-01-27T03:22:19Z")

</div>

So I was supposed to create this joystick like program in one of my questions but Im not sure how I can use functions max and min so the ball doesnt fly past the canvas when Im controlling it with my mouse.

float lineX = width/2;  
float lineY = height/2;  
float ballX = width/2;  
float ballY = height/2;  
final float DIAMETER\_BALL = 20;  
float moveX = mouseX-250;  
float moveY = mouseY-250;  
final float SPEED\_OF\_BALL = 1.2;

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

void draw() {  
background(255);  
drawBall();  
drawLine();  
moveBall();  
}

void drawBall() {  
ballX = width/2;  
ballY = height/2;  
ballX = (ballX + moveX);  
ballY = (ballY + moveY);  
ellipse((ballX+moveX), (ballY+moveY), DIAMETER\_BALL, DIAMETER\_BALL);  
}

void drawLine() {  
lineX = width/2;  
lineY = height/2;  
line(lineX, lineY-150, lineX, lineY+150);  
line(lineX-150, lineY, lineX+150, lineY);  
}

void moveBall() {  
moveX = (mouseX-250) / SPEED\_OF\_BALL;  
moveY = (mouseY-250) / SPEED\_OF\_BALL;  
}

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [January 27, 2020, 6:26am UTC](https://discourse.processing.org/t/creating-boundaries-for-my-ball-using-max-and-min/17367/2 "2020-01-27T06:26:11Z")

</div>

Check the [examples](https://processing.org/examples/) in the main page.This one would be helpful: [Bounce / Examples / Processing.org](https://processing.org/examples/bounce.html)

One point to watch:

> [@AxFan](#):
>
> float moveX = mouseX-250;  
> float moveY = mouseY-250;

The values of _mouseX_ and _mouseY_ are only available within `setup()` and `draw()`. You can define those variables globals but you need to assign their values in setup.

I recommend that instead of using 250 you sue `width/2` and `height/2`. this is easier to read and in case you change your sketch size, you don’t have to update all these “[magic numbers](https://en.wikipedia.org/wiki/Magic_number_(programming))”.

Finally, I like to say I do not fully understand what your function `drawBall()` does.

Related to your question, can you use [constrain()](https://processing.org/reference/constrain_.html) instead?

Kf

---

<div class="post-metadata">

**Author:** ![AxFan](https://avatars.discourse-cdn.com/v4/letter/a/a88e57/32.png) [@AxFan](https://discourse.processing.org/u/AxFan)\
**Post date:** [January 27, 2020, 6:43am UTC](https://discourse.processing.org/t/creating-boundaries-for-my-ball-using-max-and-min/17367/3 "2020-01-27T06:43:08Z")

</div>

Sorry, Im new to programming and I have to use max and min functions specifically in the problem. The drawBall() function is supposed to move the ball to a direction based on the mouse.

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [January 27, 2020, 6:49am UTC](https://discourse.processing.org/t/creating-boundaries-for-my-ball-using-max-and-min/17367/4 "2020-01-27T06:49:52Z")

</div>

In general, when the ball is moving along the x axis and it reaches the left boundary (the width of you sketch) then you use something like `posX=min(width,posX)` which would return the min of the two values and assign it back to your posX. However, you will need to detect when this happens as you will need to reverse the velocity along the x axis as well so it “bounces back”.

Kf
