Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 25 additions & 25 deletions calcMortarSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,22 +145,25 @@ const usa6cmTable = [
];

const tables = [
["SQU", squadTable],
["GER", gerTable],
["BR3", br3inchTable],
["BR4", br4inchTable],
["US6", usa6cmTable],
["SQU", squadTable, true],
["GER", gerTable, false],
["BR3", br3inchTable, false],
["BR4", br4inchTable, false],
["US6", usa6cmTable, false],
];

// gravity
const g = 9.8;

// deg to mil factor
// "1mil = 1/6400 of a circle in NATO countries."
const milF = 360 / 6400; // deg to mil factor
const milFNato = 360 / 6400;
// standard miliradian
const milF = 360 / (2 * Math.PI * 1000);

// conversions
function milToDeg(mil) {
function milToDeg(mil, nato) {
const mf = nato ? milFNato : milF;
return mil * milF;
}

Expand All @@ -172,16 +175,17 @@ function radToDeg(rad) {
return (rad * 180) / Math.PI;
}

function degToMil(deg) {
function degToMil(deg, nato) {
const mf = nato ? milFNato : milF;
return deg / milF;
}

function radToMil(rad) {
return degToMil(radToDeg(rad));
function radToMil(rad, nato) {
return degToMil(radToDeg(rad), nato);
}

function milToRad(mil) {
return degToRad(milToDeg(mil));
function milToRad(mil, nato) {
return degToRad(milToDeg(mil), nato);
}

// calculate time needed to hit target at distance x
Expand Down Expand Up @@ -231,7 +235,7 @@ function findAngle(x, y, v) {
const p1 = Math.sqrt(v ** 4 - g * (g * x ** 2 + 2 * y * v ** 2));
const a1 = Math.atan((v ** 2 + p1) / (g * x));

// a2 is always below 800 mil -> can't be used in the game
// a2 is always below 800 mil -> can't be used in the game (direct fire)
// const a2 = Math.atan((v ** 2 - p1) / (g * x));

return a1;
Expand All @@ -250,18 +254,16 @@ const maxPrecision = 6;
const startTime = Date.now();

tables.forEach((t) => {
const tName = t[0];
const tTable = t[1];
const [tName, tTable, tNato] = t;

console.log(`${tName}: Gathering velocities...`);
console.log(`${tName}: ===============================================`);

// get velocity per table row
const velocities = [];
tTable.forEach((entry) => {
const tDistance = entry[0];
const tAngle = entry[1];
const v = getVel(tDistance, milToRad(tAngle));
const [tDistance, tAngle] = entry;
const v = getVel(tDistance, milToRad(tAngle, tNato));
velocities.push(v);

console.log(`${tName}: ${pad(tDistance, 4)}m ${pad(tAngle, 4)}mil => ${v.toFixed(maxPrecision)}`);
Expand All @@ -275,7 +277,7 @@ tables.forEach((t) => {

console.log(`${tName}: ===============================================`);
console.log(`${tName}: average velocity: ${avgVel.toFixed(maxPrecision)}`);
console.log(`${tName}: maximum distance: ${getDist(avgVel, milToRad(800)).toFixed(2)}`);
console.log(`${tName}: maximum distance: ${getDist(avgVel, degToRad(45)).toFixed(2)}`);
console.log(`${tName}: ===============================================`);
console.log(`${tName}: Minimizing deviation...`);
console.log(`${tName}: ===============================================`);
Expand All @@ -299,8 +301,7 @@ tables.forEach((t) => {
const deviations = [];
// eslint-disable-next-line no-loop-func
tTable.forEach((entry) => {
const tDistance = entry[0];
const tAngle = entry[1];
const [tDistance, tAngle] = entry;
const estimatedAngle = radToMil(findAngle(tDistance, 0, avgVel));
const d = tAngle - estimatedAngle;
deviations.push(d);
Expand Down Expand Up @@ -334,7 +335,7 @@ tables.forEach((t) => {
console.log(`${tName}: ===============================================`);
console.log(`${tName}: FINAL VELOCITY : ${lAvgVel.toFixed(maxPrecision)}`);
console.log(`${tName}: FINAL AVG DEVIATION: ${lAvgDev.toFixed(maxPrecision + 1)}`);
console.log(`${tName}: FINAL MAX RANGE : ${getDist(lAvgVel, milToRad(800)).toFixed(2)}`);
console.log(`${tName}: FINAL MAX RANGE : ${getDist(lAvgVel, degToRad(45)).toFixed(2)}`);
console.log(`${tName}: ===============================================`);
console.log(`${tName}: Generating overview...`);
console.log(`${tName}: ===============================================`);
Expand All @@ -344,9 +345,8 @@ tables.forEach((t) => {
// iterate through table yet again, printing table values,
// calculated values based on optimized velocity, and deviation from table
tTable.forEach((entry) => {
const tDistance = entry[0];
const tAngle = entry[1];
const v = getVel(tDistance, milToRad(tAngle));
const [tDistance, tAngle] = entry;
const v = getVel(tDistance, milToRad(tAngle, tNato));

const estimatedAngle = radToMil(findAngle(tDistance, 0, avgVel));
const eAFormatted = pad(estimatedAngle.toFixed(1), 6);
Expand Down