function bounce() {
      const row = Math.floor(y/sH*rows), col = Math.floor(x/sW*cols),
        i = row*cols+col, walls = rooms[i];
      const left = col*cW,
        right = (col+1)*cW,
        top = row*rH,
        bottom = (row+1)*rH;
      let bounced = false;
      if (vx<0) {
        if ((walls&LEFT) && x<=left+r) {
          x += (1+e)*(left+r-x);
          const fy = sign(vy)*d*Math.abs(vx);
          vy -= fy/m;
          vx = -vx*e;
          bounced = true;
        }
      } else {
        if ((walls&RIGHT) && x>=right-r) {
          x -= (1+e)*(x+r-right);
          const fy = sign(vy)*d*Math.abs(vx);
          vy -= fy/m;
          vx = -vx*e;
          bounced = true;
        }
      }
      if (vy<0) {
        if ((walls&TOP) && y<=top+r) {
          y += (1+e)*(top+r-y);
          const fx = sign(vx)*d*Math.abs(vy);
          vx -= fx/m;
          vy = -vy*e;
          bounced = true;
        }
      } else {
        if ((walls&BOTTOM) && y>=bottom-r) {
          y -= (1+e)*(y+r-bottom);
          const fx = sign(vx)*d*Math.abs(vy);
          vx -= fx/m;
          vy = -vy*e;
          bounced = true;
        }
      }
      if (bounced) return;
      let cx, cy;
      if ((rooms[i-1]&TOP) || rooms[i-cols]&LEFT) {
        if ((x-left)*(x-left)+(y-top)*(y-top)<=r*r) {
          cx = left;
          cy = top;
        }
      }
      else if ((rooms[i-1]&BOTTOM) || rooms[i+cols]&LEFT) {
        if ((x-left)*(x-left)+(bottom-y)*(bottom-y)<=r*r) {
          cx = left;
          cy = bottom;
        }
      }
      else if ((rooms[i+1]&TOP) || rooms[i-cols]&RIGHT) {
        if ((right-x)*(right-x)+(y-top)*(y-top)<=r*r) {
          cx = right;
          cy = top;
        }
      }
      else if ((rooms[i+1]&BOTTOM) || rooms[i+cols]&RIGHT) {
        if ((right-x)*(right-x)+(bottom-y)*(bottom-y)<=r*r) {
          cx = right;
          cy = bottom;
        }
      }
      if (!cx) return;
      let nx = x-cx, ny = y-cy;
      const l = Math.sqrt(nx*nx+ny*ny);
      nx /= l;
      ny /= l;
      const p = vx*nx+vy*ny;
      vx -= 2*p*nx*e;
      vy -= 2*p*ny*e;
    }
/**
     * Check whether we hit any walls, and if so: Bounce.
     *
     * Bounce = reverse velocity in bounce direction, multiply with elasticity
     * Also apply drag in perpendicular direction ("friction with the wall")
     */
Check whether we hit any walls, and if so: Bounce.
Bounce = reverse velocity in bounce direction, multiply with elasticity
Also apply drag in perpendicular direction ("friction with the wall")
Check whether we hit any walls and if so : Bounce . Bounce = reverse velocity in bounce direction multiply with elasticity Also apply drag in perpendicular direction ( " friction with the wall " )
function bounce() {
      const row = Math.floor(y/sH*rows), col = Math.floor(x/sW*cols),
        i = row*cols+col, walls = rooms[i];
      const left = col*cW,
        right = (col+1)*cW,
        top = row*rH,
        bottom = (row+1)*rH;
      let bounced = false;
      if (vx<0) {
        if ((walls&LEFT) && x<=left+r) {
          x += (1+e)*(left+r-x);
          const fy = sign(vy)*d*Math.abs(vx);
          vy -= fy/m;
          vx = -vx*e;
          bounced = true;
        }
      } else {
        if ((walls&RIGHT) && x>=right-r) {
          x -= (1+e)*(x+r-right);
          const fy = sign(vy)*d*Math.abs(vx);
          vy -= fy/m;
          vx = -vx*e;
          bounced = true;
        }
      }
      if (vy<0) {
        if ((walls&TOP) && y<=top+r) {
          y += (1+e)*(top+r-y);
          const fx = sign(vx)*d*Math.abs(vy);
          vx -= fx/m;
          vy = -vy*e;
          bounced = true;
        }
      } else {
        if ((walls&BOTTOM) && y>=bottom-r) {
          y -= (1+e)*(y+r-bottom);
          const fx = sign(vx)*d*Math.abs(vy);
          vx -= fx/m;
          vy = -vy*e;
          bounced = true;
        }
      }
      if (bounced) return;
      let cx, cy;
      if ((rooms[i-1]&TOP) || rooms[i-cols]&LEFT) {
        if ((x-left)*(x-left)+(y-top)*(y-top)<=r*r) {
          cx = left;
          cy = top;
        }
      }
      else if ((rooms[i-1]&BOTTOM) || rooms[i+cols]&LEFT) {
        if ((x-left)*(x-left)+(bottom-y)*(bottom-y)<=r*r) {
          cx = left;
          cy = bottom;
        }
      }
      else if ((rooms[i+1]&TOP) || rooms[i-cols]&RIGHT) {
        if ((right-x)*(right-x)+(y-top)*(y-top)<=r*r) {
          cx = right;
          cy = top;
        }
      }
      else if ((rooms[i+1]&BOTTOM) || rooms[i+cols]&RIGHT) {
        if ((right-x)*(right-x)+(bottom-y)*(bottom-y)<=r*r) {
          cx = right;
          cy = bottom;
        }
      }
      if (!cx) return;
      let nx = x-cx, ny = y-cy;
      const l = Math.sqrt(nx*nx+ny*ny);
      nx /= l;
      ny /= l;
      const p = vx*nx+vy*ny;
      vx -= 2*p*nx*e;
      vy -= 2*p*ny*e;
    }
function bounce ( ) { const row = Math . floor ( y / sH * rows ) , col = Math . floor ( x / sW * cols ) , i = row * cols + col , walls = rooms [ i ] ; const left = col * cW , right = ( col + 1 ) * cW , top = row * rH , bottom = ( row + 1 ) * rH ; let bounced = false ; if ( vx < 0 ) { if ( ( walls & LEFT ) && x <= left + r ) { x += ( 1 + e ) * ( left + r - x ) ; const fy = sign ( vy ) * d * Math . abs ( vx ) ; vy -= fy / m ; vx = - vx * e ; bounced = true ; } } else { if ( ( walls & RIGHT ) && x >= right - r ) { x -= ( 1 + e ) * ( x + r - right ) ; const fy = sign ( vy ) * d * Math . abs ( vx ) ; vy -= fy / m ; vx = - vx * e ; bounced = true ; } } if ( vy < 0 ) { if ( ( walls & TOP ) && y <= top + r ) { y += ( 1 + e ) * ( top + r - y ) ; const fx = sign ( vx ) * d * Math . abs ( vy ) ; vx -= fx / m ; vy = - vy * e ; bounced = true ; } } else { if ( ( walls & BOTTOM ) && y >= bottom - r ) { y -= ( 1 + e ) * ( y + r - bottom ) ; const fx = sign ( vx ) * d * Math . abs ( vy ) ; vx -= fx / m ; vy = - vy * e ; bounced = true ; } } if ( bounced ) return ; let cx , cy ; if ( ( rooms [ i - 1 ] & TOP ) || rooms [ i - cols ] & LEFT ) { if ( ( x - left ) * ( x - left ) + ( y - top ) * ( y - top ) <= r * r ) { cx = left ; cy = top ; } } else if ( ( rooms [ i - 1 ] & BOTTOM ) || rooms [ i + cols ] & LEFT ) { if ( ( x - left ) * ( x - left ) + ( bottom - y ) * ( bottom - y ) <= r * r ) { cx = left ; cy = bottom ; } } else if ( ( rooms [ i + 1 ] & TOP ) || rooms [ i - cols ] & RIGHT ) { if ( ( right - x ) * ( right - x ) + ( y - top ) * ( y - top ) <= r * r ) { cx = right ; cy = top ; } } else if ( ( rooms [ i + 1 ] & BOTTOM ) || rooms [ i + cols ] & RIGHT ) { if ( ( right - x ) * ( right - x ) + ( bottom - y ) * ( bottom - y ) <= r * r ) { cx = right ; cy = bottom ; } } if ( ! cx ) return ; let nx = x - cx , ny = y - cy ; const l = Math . sqrt ( nx * nx + ny * ny ) ; nx /= l ; ny /= l ; const p = vx * nx + vy * ny ; vx -= 2 * p * nx * e ; vy -= 2 * p * ny * e ; }
Check whether we hit any walls, and if so: Bounce.
Check whether we hit any walls and if so : Bounce .