diff options
| author | Brian Cully <bjc@spork.org> | 2025-12-18 20:28:53 -0500 |
|---|---|---|
| committer | Brian Cully <bjc@spork.org> | 2025-12-18 20:36:39 -0500 |
| commit | 0f990eb95a9ef3a1be8847a863f7216a8625692a (patch) | |
| tree | 9742a3730168e0ed9b1b6d259a79a430eda71f31 /site/main.mjs | |
| parent | 8e65c1c439b2fc0d0ae5d8482e9350e283cde882 (diff) | |
| download | automathon-0f990eb95a9ef3a1be8847a863f7216a8625692a.tar.gz automathon-0f990eb95a9ef3a1be8847a863f7216a8625692a.zip | |
add bouncing when hitting arena edge
Diffstat (limited to 'site/main.mjs')
| -rw-r--r-- | site/main.mjs | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/site/main.mjs b/site/main.mjs index 31f1375..703062d 100644 --- a/site/main.mjs +++ b/site/main.mjs @@ -112,8 +112,7 @@ function renderArena(robos, delta=0.0) { robo.speedx, robo.speedy ].map(x => timeScale * x); - const x = robo.x + velx; - const y = robo.y + vely; + const [_, x, y] = bounce(25, robo.heading, robo.x + velx, robo.y + vely, canvas.width, canvas.height); //console.debug('bjc render robo', robo, velx, vely, x, y) renderRobo(ctx, x, y); }); @@ -154,6 +153,26 @@ function loadForth(taintedPath) { }); } +function bounce(radius, heading, x, y, width, height) { + // todo: don't just clamp the x/y, instead calculate based on + // where the intersection with the wall is. + if (y < radius) { + // top wall + return [-1 * heading, x, radius]; + } else if (y >= height-radius) { + // bottom wall + return [-1 * heading, x, height-radius]; + } else if (x < radius) { + // left wall + return [heading - 180, radius, y]; + } else if (x >= width-radius) { + // right wall + return [180 - heading, width-radius, y]; + } else { + return [heading, x, y]; + } +} + async function loaded() { const roboWorker = new Worker('robo.mjs', { type: 'module' }); const robo = { @@ -206,6 +225,13 @@ async function loaded() { robo.speedy = speedy; robo.x += speedx; robo.y += speedy; + const canvas = document.querySelector(CANVAS_SELECTOR); + const [newHeading, newx, newy] = bounce(25, trans.vars.heading, robo.x, robo.y, canvas.width, canvas.height); + if (newHeading != trans.vars.heading) { + roboWorker.postMessage({ kind: 'setHeading', heading: newHeading }); + robo.x = newx; + robo.y = newy; + } renderArena([robo]); break; default: |
