Add velocity

This commit is contained in:
2025-12-13 19:10:56 -08:00
parent 37f683e9a5
commit 88241ad705

View File

@@ -78,19 +78,24 @@
const reverseMoveButton = document.querySelector("#move-backward"); const reverseMoveButton = document.querySelector("#move-backward");
const forwardMoveButton = document.querySelector("#move-forward"); const forwardMoveButton = document.querySelector("#move-forward");
const fps = document.querySelector("#fps"); const fps = document.querySelector("#fps");
const velocity = [0, 20]; // meters per second
let zero; let previous, zero, frameCount = 0;
let frameCount = 0;
requestAnimationFrame(firstFrame); requestAnimationFrame(firstFrame);
function firstFrame(timestamp) { function firstFrame(timestamp) {
zero = timestamp; zero = timestamp;
previous = timestamp;
animate(timestamp); animate(timestamp);
} }
function animate(timestamp) { function animate(timestamp) {
if (timestamp - zero >= 1000) { const delta = timestamp - zero;
const elapsed = timestamp - previous;
previous = timestamp;
if (delta >= 1000) {
fps.innerText = frameCount; fps.innerText = frameCount;
zero = timestamp; zero = timestamp;
frameCount = 0; frameCount = 0;
@@ -98,7 +103,26 @@
frameCount++; frameCount++;
} }
requestAnimationFrame(t => animate(t)); const [velocityX, velocityY] = velocity;
const changeX = 0.001 * elapsed * velocityX;
const changeY = 0.001 * elapsed * velocityY;
const regex = /(-?\d*\.{0,1}\d+)px/g;
const def = ["0px", "0"];
let x, y;
if (hitbox.style.transform.length === 0) {
x = 0;
y = 0;
} else {
[[, x], [, y] = def] = [...hitbox.style.transform.matchAll(regex)];
}
const positionX = changeX + +x;
const positionY = changeY + +y;
hitbox.style.transform = `translate(${positionX}px, ${positionY}px)`;
// hitbox.style.transform = `translate(0px, ${position}px)`;
if (+y < 100) requestAnimationFrame(t => animate(t));
} }
function moveTank(el) { function moveTank(el) {
@@ -135,8 +159,8 @@
return tankTurn; return tankTurn;
} }
const tankTurn = turnTank(tank); // const tankTurn = turnTank(tank);
const tankMove = moveTank(hitbox); // const tankMove = moveTank(hitbox);
function degsToRads(degrees) { function degsToRads(degrees) {
return degrees * Math.PI / 180; return degrees * Math.PI / 180;

Before

Width:  |  Height:  |  Size: 9.1 KiB

After

Width:  |  Height:  |  Size: 10 KiB