# Strange ball behaviour

**URL:** <https://discourse.processing.org/t/strange-ball-behaviour/3931>\
**Category:** Coding Questions\
**Created:** [September 28, 2018, 11:50am UTC](https://discourse.processing.org/t/strange-ball-behaviour/3931 "2018-09-28T11:50:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexstrinda](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/alexstrinda/32/1739_2.png) [@Alexstrinda](https://discourse.processing.org/u/Alexstrinda)\
**Post date:** [September 28, 2018, 11:50am UTC](https://discourse.processing.org/t/strange-ball-behaviour/3931/1 "2018-09-28T11:50:53Z")

</div>

I wrote a very simple code that doesn’t work as expected. I could have fixed it with a patch or something similar, however, I am really interested in the logical flaw that leads to the problem.

The issue is that the ball’s height keeps getting smaller, which is not expected.

My sketch is here - [openProcessing.org](https://www.openprocessing.org/sketch/594161)

I have found that the ball’s maximum velocity decreases by one acceleration each time the ball hits the edge of the screen, but I don’t get why.

```auto
var y = 100;
var v = 0;
var a = 0.2;
var x = 100;

function setup() {
	createCanvas(windowWidth, windowHeight);
  frameRate (10);
	
}

function draw() {	
	
	ellipse(x, y, 20, 20);
	
        println (v);	
	
	if (y > windowHeight || y < 0){
	v = v * (-1);
	text (v, x,100);
	
	}
	
	/*
	if (x > windowWidth){
	v = v * (-1);
	}
	
	if (x < 0){
	v = v * (-1);
	
	}
	*/
  
	
	v = v + a;
	y = y + v;
    x = x + 1;
}

```

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [September 28, 2018, 12:06pm UTC](https://discourse.processing.org/t/strange-ball-behaviour/3931/2 "2018-09-28T12:06:34Z")

</div>

Hi Alexstrinda,

You get that behavior because your were counting the a value one more time during the descend then during the ascent if it make any sense…

Maybe the fix sill speak more to you. Just substract the a value to your velocity when it is touching the ground:

```auto
if (y > windowHeight || y < 0){
  v = v * (-1) - a;
}

```
