use "canvas" use "std" use "math" maxFPS = 30 frame_time = 1000 / maxFPS dd = 0.5 max = 25 G = 100 // задаём константу методом подбора dt = 0.33 // шаг вычисления b = newarray(0) g = newarray(0) def glob(x, y) = { "x": x "y": y "r": 25 "m": 1 "dx": 0 "dy": 0 "color": #ff00a5a5 } def collision(a, b) { if((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y) < (a.r/2 + b.r/2) * (a.r/2 + b.r/2)) return true return false } canvas = showcanvas() setOnTouchEvent(::touch) a = glob(WIDTH/2, HEIGHT/2) a.color = rand(#ffffffac) a.m = 110 a.r += a.m / 2 b += a canvas.setTextSize(25) while (true) { g = b canvas.drawColor(#ff000000) for(i = 0, i < length(g), i ++) { canvas.setColor(g[i].color) canvas.drawCircle(g[i].x, g[i].y, g[i].r) //if (g[i].x - g[i].r < 0 || g[i].x + g[i].r > WIDTH) g[i].dx = -g[i].dx //if (g[i].y - g[i].r < 0 || g[i].y + g[i].r > HEIGHT) g[i].dy = -g[i].dy } canvas.setColor(#ffffffff) canvas.drawText("m"+g[0].m, g[0].x, g[0].y) repaint() update() sleep(frame_time) } def update(){ a = 0 ax = 0 ay = 0 dx = 0 dy = 0 r = 0 for (i = 0, i < length(b), i++) for (j = 0, j < length(b), j++){ if (i == j) continue if (collision(b[i], b[j])) { if (b[i].m > b[j].m) { b[i].m += b[j].m b[i].r += b[j].r / 2 b = arraySplice(b, j, 1) } else { b[j].m += b[i].m b[j].r += b[i].r / 2 b = arraySplice(b, i, 1) } continue } dx = b[j].x - b[i].x dy = b[j].y - b[i].y r = dx * dx + dy * dy // тут R^2 //if (r < 0.1) r = 0.1 // избегаем деления на очень маленькое число a = G * b[j].m / r r = sqrt(r) // тут R ax = a * dx / r // a * cos ay = a * dy / r // a * sin b[i].dx += ax * dt b[i].dy += ay * dt } for (i = 0, i < length(b), i++){ b[i].x += b[i].dx * dt b[i].y += b[i].dy * dt } } def touch(e) { if (e.action == Action.DOWN) { a = glob(e.x, e.y) a.color = rand(#ffffffac) a.dx = rand(-5, 5) a.dy = rand(-5, 5) a.m = rand(1, 7) a.r += a.m b += a } }