Manoz - 1 year ago 183

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

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

`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.

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**