From b1fd38196e9378f96d14f9c7fdff5de9c60d7c2b Mon Sep 17 00:00:00 2001 From: Catalin Constantin Mititiuc Date: Wed, 17 Dec 2025 12:51:01 -0800 Subject: [PATCH] Make bullets originate from end of cannon --- html/images/tanks.svg | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/html/images/tanks.svg b/html/images/tanks.svg index b2b5d34..168cf39 100644 --- a/html/images/tanks.svg +++ b/html/images/tanks.svg @@ -5,6 +5,11 @@ /* fill-opacity: 0.9; */ } + #frames { + position: absolute; + right: 0px; + } + p { border: 1px solid black; box-size: border-box; @@ -37,6 +42,10 @@ opacity: 0.5; } + circle.bullet { + fill: orangered; + } + /* @keyframes rotate { */ /* from { */ /* transform: rotate(0); */ @@ -57,9 +66,7 @@ - - - + @@ -70,7 +77,7 @@ -
+
0 fps
@@ -137,13 +144,25 @@ const radians = degrees * Math.PI / 180; const vx = -Math.sin(radians); const vy = Math.cos(radians); + const bulletTimeout = 5000; // miliseconds + const cannonLength = 8; const el = document.createElementNS(namespaceURIsvg, 'circle'); + el.classList.add('bullet'); el.setAttribute('r', 1); el.setAttribute('cx', 0); el.setAttribute('cy', 0); const node = bulletsContainer.appendChild(el); - const bullet = { x: x, y: y, vx: vx, vy: vy, time: 5000, node: node } + + const bullet = { + x: x + vx * cannonLength, + y: y + vy * cannonLength, + vx: vx, + vy: vy, + time: bulletTimeout, + node: node + } + bullets.push(bullet); } @@ -208,6 +227,7 @@ const changeY = 0.001 * elapsed * velocityY; bullets.forEach((bullet, index) => { + const deleteCount = 1; bullets[index].time -= elapsed; if (bullets[index].time > 0) { @@ -220,7 +240,7 @@ bullet.node.style.transform = `translate(${bx}px, ${by}px)`; } else { bullet.node.remove(); - bullets.splice(index, 1); + bullets.splice(index, deleteCount); } }); @@ -293,7 +313,7 @@ fireButton.addEventListener("click", function (e) { const [x, y] = getTranslate(hitbox); - fireBullet(+x, +y); + fireBullet(x, y); });