Manoz - 9 months ago 82

jQuery Question

I am confused with math.abs().

I did Research through internet but couldn't find any relation with this bouncing ball's animation. I want to know how this works and how ball is bouncing smoothly after using math.abs() function?

`function bounce() {`

if (x + dx > 293 || x + dx < 0) {

dx = -dx;

}

if (y >= 290) {

y = 290;

}

if (y + dy > 290 || y + dy < 0) {

dx *= 0.99;

dy = -dy;

}

//if (Math.abs(dx) < 0.01) {

// dx = 0;

}

dy++;

}

I did

Answer

`dx`

is the displacement on x.

`Math.abs(dx)`

is the absolute speed on x, that is the value without the sign, always positive or null.

```
if (Math.abs(dx) < 0.01) {
```

could have been written as

```
if (dx>-0.01 && dx < 0.01) {
```

Basically, this line with the following one stops the ball along `x`

if it's already slow.