diff --git a/html/images/space.svg b/html/images/space.svg index 464ef08..1f9a47c 100644 --- a/html/images/space.svg +++ b/html/images/space.svg @@ -57,6 +57,10 @@ fill: white; stroke: red; } + + #legs { + display: none; + } @@ -66,10 +70,14 @@ + + + + - + @@ -79,6 +87,7 @@
+ 0 s 0 fps
@@ -109,6 +118,7 @@ const maxSpeed = 100; const fps = document.querySelector("#fps"); + const time = document.querySelector("#time"); const info = document.querySelector("#debug"); const ship = document.querySelector(".ship"); const gun = ship.querySelector('#cannon'); @@ -135,19 +145,6 @@ return [+x, +y]; }); - // console.log("points", points); - - const lineContainer = document.querySelector('#lines'); - - // points.forEach(([x, y]) => { - // const el = document.createElementNS(namespaceURIsvg, 'line'); - // el.setAttribute('x1', x); - // el.setAttribute('y1', y); - // el.setAttribute('x2', x); - // el.setAttribute('y2', y); - // lineContainer.appendChild(el) - // }); - const triangleContainer = document.querySelector('#lines'); const trianglePts = points.map((pt, i) => [pt, points[(i + 1) % points.length]]); @@ -190,9 +187,11 @@ const db = distance(bx, by, shipx, shipy); const dc = distance(ax, ay, bx, by); - const alpha = Math.acos((db**2 + dc**2 - da**2) / (2 * db * dc)); - const beta = Math.acos((da**2 + dc**2 - db**2) / (2 * da * dc)); - isAcute = alpha < PI && beta < PI; + // https://en.wikipedia.org/wiki/Law_of_cosines + // Solve for angles α and β with inverse cosine (arccosine) + const α = Math.acos((db ** 2 + dc ** 2 - da ** 2) / (2 * db * dc)); + const β = Math.acos((da ** 2 + dc ** 2 - db ** 2) / (2 * da * dc)); + isAcute = α < PI && β < PI; } if (pos !== attr.pop()) { @@ -202,19 +201,11 @@ t.classList[isClockwise && isAcute ? "add" : "remove"](className); if (isClockwise && isAcute) visible.push(t); - - // return isClockwise ? [t, ...acc] : acc; - // i think i can also discard obtuse triangles? - - // if all the triangles are obtuse, i only need to check the nearest corner for a collision - // otherwise, i need to check only the acute triangles }); return visible; } - const lines = lineContainer.querySelectorAll('line'); - function wrapPos(positionX, positionY) { let x, y; @@ -314,8 +305,12 @@ requestAnimationFrame(firstFrame); + + let start; + let restart = false; function firstFrame(timestamp) { zero = timestamp; + zeroForTimer = timestamp; previous = timestamp; animate(timestamp); } @@ -326,6 +321,8 @@ function animate(timestamp) { const delta = timestamp - zero; + time.innerText = ((timestamp - zeroForTimer) * 0.001).toFixed(3); + // time.innerText = time.inner const elapsed = timestamp - previous; previous = timestamp; @@ -349,8 +346,8 @@ if (rotate > 0) gun.style.transform = `rotate(${(+degrees + rotationSpeed * elapsed) % 360}deg)`; else if (rotate < 0) gun.style.transform = `rotate(${(+degrees - rotationSpeed * elapsed) % 360}deg)`; - let [velocityX, velocityY] = velocity; - let [accelerationX, accelerationY] = acceleration; + let [velocityX, velocityY] = restart ? [0, 0] : velocity; + let [accelerationX, accelerationY] = restart ? [0, 0] : acceleration; if (velocityX > 0) accelerationX += -friction; else if (velocityX < 0) accelerationX += friction; @@ -372,7 +369,8 @@ const changeY = 0.001 * elapsed * velocityY; let [x, y] = getTranslate(hitbox); - let position = [positionX, positionY] = wrapPos(changeX + x, changeY + y); + let position = [positionX, positionY] = restart ? [0, 0] : wrapPos(changeX + x, changeY + y); + if (restart) restart = false; updateBullets(elapsed); // updateLines(position); @@ -437,6 +435,14 @@ wall.setAttribute('fill', cornerCollision || sideCollision ? 'red' : 'black'); hitbox.style.transform = `translate(${positionX}px, ${positionY}px)`; + if (cornerCollision || sideCollision) { + // restart game + zeroForTimer = timestamp; + restart = true; + // position = [0, 0]; + // acceleration = [0, 0]; + } + // if (+y < 200) // if (timestamp < 10000) requestAnimationFrame(t => animate(t));