Skip to content

Commit

Permalink
formula corr
Browse files Browse the repository at this point in the history
  • Loading branch information
sarry7045 committed Nov 10, 2024
1 parent f3c915f commit 87d3f5e
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions script.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const g = 9.81; // Acceleration due to gravity (m/s²)

let animationId = null;
let trajectory = [];
let currentTime = 0;
let projectile = null;

class Projectile {
constructor(mass, velocity, angle, height) {
this.mass = mass;
Expand All @@ -21,27 +21,27 @@ class Projectile {
this.maxHeight = height;
this.range = 0;
}

update(dt) {
this.time += dt;
this.x = this.vx * this.time;
this.y = this.h0 + this.vy * this.time - 0.35 * g * this.time * this.time;
this.y = this.h0 + this.vy * this.time - 0.5 * g * this.time * this.time;
this.maxHeight = Math.max(this.maxHeight, this.y);
this.range = this.x;
}

getFinalVelocity() {
const vx = this.vx;
const vy = this.vy - g * this.time;
return Math.sqrt(vx * vx + vy * vy);
}
}

function resizeCanvas() {
canvas.width = canvas.clientWidth;
canvas.height = canvas.clientHeight;
}

function drawGrid() {
ctx.strokeStyle = '#ddd';
ctx.lineWidth = 1;
Expand All @@ -61,7 +61,7 @@ function drawGrid() {
ctx.stroke();
}
}

function drawTrajectory() {
ctx.strokeStyle = 'rgba(100, 108, 255, 0.3)';
ctx.lineWidth = 2;
Expand All @@ -72,14 +72,14 @@ function drawTrajectory() {
});
ctx.stroke();
}

function drawProjectile(x, y) {
ctx.fillStyle = '#6666FF';
ctx.fillStyle = '#646cff';
ctx.beginPath();
ctx.arc(x, y, 8, 0, Math.PI * 2);
ctx.fill();
}

function updateResults() {
if (!projectile) return;

Expand All @@ -88,11 +88,11 @@ function updateResults() {
document.getElementById('range').textContent = projectile.range.toFixed(2) + ' m';
document.getElementById('final-velocity').textContent = projectile.getFinalVelocity().toFixed(2) + ' m/s';
}

function animate() {
if (!projectile) return;

const dt = 0.05; // 60 FPS
const dt = 0.016; // 60 FPS
projectile.update(dt);

// Scale factors for drawing
Expand All @@ -119,7 +119,7 @@ function animate() {

animationId = requestAnimationFrame(animate);
}

function launch() {
const mass = parseFloat(document.getElementById('mass').value);
const velocity = parseFloat(document.getElementById('velocity').value);
Expand All @@ -135,7 +135,7 @@ function launch() {

animate();
}

function reset() {
if (animationId) {
cancelAnimationFrame(animationId);
Expand All @@ -156,12 +156,12 @@ function reset() {
document.getElementById('range').textContent = '0.00 m';
document.getElementById('final-velocity').textContent = '0.00 m/s';
}

// Event listeners
window.addEventListener('resize', resizeCanvas);
document.getElementById('launch').addEventListener('click', launch);
document.getElementById('reset').addEventListener('click', reset);

// Initial setup
resizeCanvas();
drawGrid();
drawGrid();

0 comments on commit 87d3f5e

Please sign in to comment.