Limit ship max speed

This commit is contained in:
2025-12-17 16:59:48 -08:00
parent 9730ff85fb
commit 0b841d8873

View File

@@ -76,6 +76,7 @@
let friction = 0; let friction = 0;
let rotate = 0; let rotate = 0;
let rotationSpeed = 0.25; let rotationSpeed = 0.25;
const maxSpeed = 100;
const rules = document.styleSheets[0].cssRules; const rules = document.styleSheets[0].cssRules;
@@ -112,9 +113,10 @@
return [x, y]; return [x, y];
} }
function fireBullet(x, y) { function fireBullet(x, y, velocity) {
const degrees = getRotate(gun); const degrees = getRotate(gun);
const radians = degrees * Math.PI / 180; // toFixed(15)? const radians = degrees * Math.PI / 180; // toFixed(15)?
const speed = 200; // meters per second
const vx = -Math.sin(radians); const vx = -Math.sin(radians);
const vy = Math.cos(radians); const vy = Math.cos(radians);
const bulletTimeout = 5000; // miliseconds const bulletTimeout = 5000; // miliseconds
@@ -129,8 +131,8 @@
const bullet = { const bullet = {
x: x + vx * cannonLength, x: x + vx * cannonLength,
y: y + vy * cannonLength, y: y + vy * cannonLength,
vx: vx, vx: vx * speed + velocity[0],
vy: vy, vy: vy * speed + velocity[1],
time: bulletTimeout, time: bulletTimeout,
node: bulletsContainer.appendChild(el) node: bulletsContainer.appendChild(el)
} }
@@ -172,9 +174,11 @@
if (delta >= 1000) { if (delta >= 1000) {
fps.innerText = frameCount; fps.innerText = frameCount;
info.innerText = 'bullets\nx\ty\tvx\tvy\n' + bullets.map((b, index) => { info.innerText = `velocity ${velocity}\n`
return `${b.x.toFixed(2)}\t${b.y.toFixed(2)}\t${b.vx.toFixed(2)}\t${b.vy.toFixed(2)}`; + 'bullets\nx\ty\tvx\tvy\n'
}).join("\n"); + bullets.map((b, index) => {
return `${b.x.toFixed(2)}\t${b.y.toFixed(2)}\t${b.vx.toFixed(2)}\t${b.vy.toFixed(2)}`;
}).join("\n");
zero = timestamp; zero = timestamp;
frameCount = 0; frameCount = 0;
@@ -200,6 +204,13 @@
velocityY = velocityY > 0 && velocityY + accelerationY < 0 ? 0 : velocityY + accelerationY; velocityY = velocityY > 0 && velocityY + accelerationY < 0 ? 0 : velocityY + accelerationY;
velocity = [velocityX, velocityY]; velocity = [velocityX, velocityY];
if (velocity[0] > maxSpeed) velocity[0] = maxSpeed;
else if (velocity[0] < -maxSpeed) velocity[0] = -maxSpeed
if (velocity[1] > maxSpeed) velocity[1] = maxSpeed;
else if (velocity[1] < -maxSpeed) velocity[1] = -maxSpeed
const changeX = 0.001 * elapsed * velocityX; const changeX = 0.001 * elapsed * velocityX;
const changeY = 0.001 * elapsed * velocityY; const changeY = 0.001 * elapsed * velocityY;
@@ -208,8 +219,8 @@
bullets[index].time -= elapsed; bullets[index].time -= elapsed;
if (bullets[index].time > 0) { if (bullets[index].time > 0) {
bullets[index].y += 0.1 * elapsed * bullets[index].vy; bullets[index].y += 0.001 * elapsed * bullets[index].vy;
bullets[index].x += 0.1 * elapsed * bullets[index].vx; bullets[index].x += 0.001 * elapsed * bullets[index].vx;
let [bx, by] = wrapPos(bullets[index].x, bullets[index].y) let [bx, by] = wrapPos(bullets[index].x, bullets[index].y)
bullets[index].x = bx; bullets[index].x = bx;
@@ -254,7 +265,8 @@
if (!spacePressed) { if (!spacePressed) {
spacePressed = true; spacePressed = true;
const [x, y] = getTranslate(hitbox); const [x, y] = getTranslate(hitbox);
fireBullet(x, y); // fireBullet(x, y);
fireBullet(x, y, velocity);
} }
break; break;
case "KeyW": case "KeyW":

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 12 KiB