Skip to content

Instantly share code, notes, and snippets.

@larchanka
Created July 9, 2024 21:18
Show Gist options
  • Save larchanka/09d8774b45143d4ab05eb69e9a6b960c to your computer and use it in GitHub Desktop.
Save larchanka/09d8774b45143d4ab05eb69e9a6b960c to your computer and use it in GitHub Desktop.
/**
* Calculates the ideal ramp length and height for a car jump based on the preference for a higher or longer jump.
*
* @param {'higher' | 'longer'} preference - The preference for the jump, either "higher" or "longer".
* @returns {{length: string, height: string}} - An object containing the scaled length and height of the ramp in meters.
* @throws {Error} - Throws an error if the preference is not "higher" or "longer".
*/
function calculateRcRamp(preference) {
const g = 9.81;
const scale = 1;
let theta, height;
if (preference === 'higher') {
theta = 60 * (Math.PI / 180);
height = 0.15;
} else if (preference === 'longer') {
theta = 30 * (Math.PI / 180);
height = 0.05;
} else {
throw new Error('Invalid preference. Choose "higher" or "longer".');
}
const v0 = Math.sqrt(2 * g * height);
const d = 2 * height * Math.cos(theta) * (Math.sin(theta) + Math.sqrt(Math.pow(Math.sin(theta), 2) + 1));
const scaledLength = d * scale;
const scaledHeight = height * scale;
return {
length: scaledLength.toFixed(2),
height: scaledHeight.toFixed(2)
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment