Rotate gun with torque and angular acceleration/velocity
This commit is contained in:
@@ -285,7 +285,6 @@ const map = (function(els) {
|
|||||||
})(wallElements);
|
})(wallElements);
|
||||||
|
|
||||||
let allStartingEdges;
|
let allStartingEdges;
|
||||||
let friction = 0;
|
|
||||||
let rotationSpeed = 0.25;
|
let rotationSpeed = 0.25;
|
||||||
let started = false;
|
let started = false;
|
||||||
let restart = false;
|
let restart = false;
|
||||||
@@ -297,10 +296,12 @@ function init() {
|
|||||||
|
|
||||||
s.position = { x: 10, y: 10 };
|
s.position = { x: 10, y: 10 };
|
||||||
s.velocity = { x: 0, y: 0 };
|
s.velocity = { x: 0, y: 0 };
|
||||||
|
s.angularVelocity = 0;
|
||||||
|
|
||||||
// s. velocity = { x: -5*mult, y: 7*mult };
|
// s. velocity = { x: -5*mult, y: 7*mult };
|
||||||
|
|
||||||
s.acceleration = { x: 0, y: 0 };
|
s.acceleration = { x: 0, y: 0 };
|
||||||
|
s.angularAcceleration = 0;
|
||||||
s.rotate = 0;
|
s.rotate = 0;
|
||||||
s.degrees = 0;
|
s.degrees = 0;
|
||||||
s.collision = null;
|
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) {
|
function updateShip(s, elapsed) {
|
||||||
const gravity = 0.25;
|
const gravity = 0.25;
|
||||||
|
|
||||||
if (rotate > 0) {
|
// if (rotate > 0) {
|
||||||
s.degrees = (s.degrees + rotationSpeed * elapsed) % 360;
|
// s.degrees = (s.degrees + rotationSpeed * elapsed) % 360;
|
||||||
} else if (rotate < 0) {
|
// } else if (rotate < 0) {
|
||||||
s.degrees = (s.degrees - rotationSpeed * elapsed) % 360;
|
// s.degrees = (s.degrees - rotationSpeed * elapsed) % 360;
|
||||||
}
|
// }
|
||||||
|
|
||||||
gun.style.transform = `rotate(${s.degrees}deg)`;
|
gun.style.transform = `rotate(${s.degrees}deg)`;
|
||||||
|
|
||||||
@@ -799,6 +800,9 @@ function updateShip(s, elapsed) {
|
|||||||
const { x: vx, y: vy } = s.velocity;
|
const { x: vx, y: vy } = s.velocity;
|
||||||
// const { x: ax, y: ay } = s.acceleration;
|
// const { x: ax, y: ay } = s.acceleration;
|
||||||
let { 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;
|
ay += gravity;
|
||||||
|
|
||||||
s.velocity = {
|
s.velocity = {
|
||||||
@@ -806,6 +810,16 @@ function updateShip(s, elapsed) {
|
|||||||
y: vy > 0 && vy + ay <= 0 ? 0 : vy + ay
|
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('x2', s.velocity.x);
|
||||||
velIndic.setAttribute('y2', s.velocity.y);
|
velIndic.setAttribute('y2', s.velocity.y);
|
||||||
acclIndic.setAttribute('x2', s.acceleration.x);
|
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 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;
|
current = s.collision;
|
||||||
|
|
||||||
// const tempRadius = 7;
|
// const tempRadius = 7;
|
||||||
@@ -979,6 +999,7 @@ function animate(timestamp) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let force = 1;
|
let force = 1;
|
||||||
|
let torque = 0.1;
|
||||||
let spacePressed = false;
|
let spacePressed = false;
|
||||||
let restartPressed = false;
|
let restartPressed = false;
|
||||||
let upPressed = false;
|
let upPressed = false;
|
||||||
@@ -1036,14 +1057,14 @@ document.addEventListener("keydown", function(e) {
|
|||||||
case "Comma":
|
case "Comma":
|
||||||
if (!rotateCCWPressed) {
|
if (!rotateCCWPressed) {
|
||||||
rotateCCWPressed = true;
|
rotateCCWPressed = true;
|
||||||
rotate += -1;
|
s.angularAcceleration -= torque;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case "KeyE":
|
case "KeyE":
|
||||||
case "Period":
|
case "Period":
|
||||||
if (!rotateCWPressed) {
|
if (!rotateCWPressed) {
|
||||||
rotateCWPressed = true;
|
rotateCWPressed = true;
|
||||||
rotate += 1;
|
s.angularAcceleration += torque;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case "KeyP": // Pause
|
case "KeyP": // Pause
|
||||||
@@ -1096,14 +1117,14 @@ document.addEventListener("keyup", function(e) {
|
|||||||
case "Comma":
|
case "Comma":
|
||||||
if (rotateCCWPressed) {
|
if (rotateCCWPressed) {
|
||||||
rotateCCWPressed = false;
|
rotateCCWPressed = false;
|
||||||
rotate -= -1;
|
s.angularAcceleration += torque;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case "KeyE":
|
case "KeyE":
|
||||||
case "Period":
|
case "Period":
|
||||||
if (rotateCWPressed) {
|
if (rotateCWPressed) {
|
||||||
rotateCWPressed = false;
|
rotateCWPressed = false;
|
||||||
rotate -= 1;
|
s.angularAcceleration -= torque;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
|
Before Width: | Height: | Size: 34 KiB After Width: | Height: | Size: 35 KiB |
Reference in New Issue
Block a user