Rotate gun with torque and angular acceleration/velocity

This commit is contained in:
2026-01-17 13:25:00 -08:00
parent 12d9d8ae95
commit d779f802a6

View File

@@ -285,7 +285,6 @@ const map = (function(els) {
})(wallElements);
let allStartingEdges;
let friction = 0;
let rotationSpeed = 0.25;
let started = false;
let restart = false;
@@ -297,10 +296,12 @@ function init() {
s.position = { x: 10, y: 10 };
s.velocity = { x: 0, y: 0 };
s.angularVelocity = 0;
// s. velocity = { x: -5*mult, y: 7*mult };
s.acceleration = { x: 0, y: 0 };
s.angularAcceleration = 0;
s.rotate = 0;
s.degrees = 0;
s.collision = null;
@@ -787,11 +788,11 @@ function lineIntxnPt({ x1, y1, x2, y2 }, { x1: x3, y1: y3, x2: x4, y2: y4 }) {
function updateShip(s, elapsed) {
const gravity = 0.25;
if (rotate > 0) {
s.degrees = (s.degrees + rotationSpeed * elapsed) % 360;
} else if (rotate < 0) {
s.degrees = (s.degrees - rotationSpeed * elapsed) % 360;
}
// if (rotate > 0) {
// s.degrees = (s.degrees + rotationSpeed * elapsed) % 360;
// } else if (rotate < 0) {
// s.degrees = (s.degrees - rotationSpeed * elapsed) % 360;
// }
gun.style.transform = `rotate(${s.degrees}deg)`;
@@ -799,6 +800,9 @@ function updateShip(s, elapsed) {
const { x: vx, y: vy } = s.velocity;
// const { x: ax, y: ay } = s.acceleration;
let { x: ax, y: ay } = s.acceleration;
const angularVel = s.angularVelocity;
const angularAcc = s.angularAcceleration;
const degrees = s.degrees;
ay += gravity;
s.velocity = {
@@ -806,6 +810,16 @@ function updateShip(s, elapsed) {
y: vy > 0 && vy + ay <= 0 ? 0 : vy + ay
};
s.angularVelocity = angularVel + angularAcc;
const friction = 0.05;
if (s.angularVelocity > 0) {
s.angularVelocity -= s.angularVelocity > friction ? friction : s.angularVelocity;
} else if (s.angularVelocity < 0) {
s.angularVelocity += -s.angularVelocity > friction ? friction : -s.angularVelocity;
}
velIndic.setAttribute('x2', s.velocity.x);
velIndic.setAttribute('y2', s.velocity.y);
acclIndic.setAttribute('x2', s.acceleration.x);
@@ -819,6 +833,12 @@ function updateShip(s, elapsed) {
};
const p = { x: pDelta.x + px, y: pDelta.y + py };
const turnRadians = elapsed * s.angularVelocity * metersPerMillisecond;
const radians = degrees * Math.PI / 180; // toFixed(15)?
const dDelta = turnRadians * 180 / Math.PI;
s.degrees = degrees + dDelta;
current = s.collision;
// const tempRadius = 7;
@@ -979,6 +999,7 @@ function animate(timestamp) {
}
let force = 1;
let torque = 0.1;
let spacePressed = false;
let restartPressed = false;
let upPressed = false;
@@ -1036,14 +1057,14 @@ document.addEventListener("keydown", function(e) {
case "Comma":
if (!rotateCCWPressed) {
rotateCCWPressed = true;
rotate += -1;
s.angularAcceleration -= torque;
}
break;
case "KeyE":
case "Period":
if (!rotateCWPressed) {
rotateCWPressed = true;
rotate += 1;
s.angularAcceleration += torque;
}
break;
case "KeyP": // Pause
@@ -1096,14 +1117,14 @@ document.addEventListener("keyup", function(e) {
case "Comma":
if (rotateCCWPressed) {
rotateCCWPressed = false;
rotate -= -1;
s.angularAcceleration += torque;
}
break;
case "KeyE":
case "Period":
if (rotateCWPressed) {
rotateCWPressed = false;
rotate -= 1;
s.angularAcceleration -= torque;
}
break;
}

Before

Width:  |  Height:  |  Size: 34 KiB

After

Width:  |  Height:  |  Size: 35 KiB