Creating a Projectile class to calculate height horizontal distance and landing in JavaScript

We need to create a JavaScript class called Projectile that simulates projectile motion physics. This class calculates horizontal distance traveled by a projectile given initial conditions.

Problem Statement

Create a Projectile class that takes three parameters during initialization:

  • Starting height (h0): 0 ? h0
  • Starting velocity (v0): 0
  • Launch angle (a):

The class should include a horiz method that calculates horizontal distance traveled at time t.

Physics Formula

The horizontal distance in projectile motion is calculated using:

Horizontal Distance = v? × cos(?) × t where v? = initial velocity, ? = angle, t = time

Implementation

class Projectile {
    constructor(h0, v0, angle) {
        this.h0 = h0;           // Starting height
        this.v0 = v0;           // Initial velocity
        this.angle = angle;     // Launch angle in degrees
    }
    
    // Convert degrees to radians and calculate horizontal distance
    horiz(t) {
        const angleInRadians = this.angle * (Math.PI / 180);
        const horizontalDistance = this.v0 * Math.cos(angleInRadians) * t;
        return horizontalDistance;
    }
}

// Create projectile instance
const projectile = new Projectile(5, 2, 45);

// Calculate horizontal distance after 0.2 seconds
const distance = projectile.horiz(0.2);
console.log("Horizontal distance:", distance);
Horizontal distance: 0.28284271247461903

Enhanced Example with Multiple Calculations

class Projectile {
    constructor(h0, v0, angle) {
        this.h0 = h0;
        this.v0 = v0;
        this.angle = angle;
    }
    
    horiz(t) {
        const angleInRadians = this.angle * (Math.PI / 180);
        return this.v0 * Math.cos(angleInRadians) * t;
    }
    
    // Additional method: calculate vertical position
    height(t) {
        const angleInRadians = this.angle * (Math.PI / 180);
        const g = 9.81; // gravity
        const verticalVelocity = this.v0 * Math.sin(angleInRadians);
        return this.h0 + verticalVelocity * t - 0.5 * g * t * t;
    }
}

// Test with different projectiles
const proj1 = new Projectile(10, 50, 30);
const proj2 = new Projectile(0, 25, 45);

console.log("Projectile 1 - Horizontal distance at t=2:", proj1.horiz(2));
console.log("Projectile 1 - Height at t=2:", proj1.height(2));
console.log("Projectile 2 - Horizontal distance at t=1:", proj2.horiz(1));
Projectile 1 - Horizontal distance at t=2: 86.60254037844387
Projectile 1 - Height at t=2: 60.38461538461539
Projectile 2 - Horizontal distance at t=1: 17.677669529663687

Key Points

  • Convert degrees to radians using angle * (Math.PI / 180)
  • Horizontal velocity remains constant: v? × cos(?)
  • Multiply by time t to get total horizontal distance
  • The class can be extended with additional physics calculations

Conclusion

The Projectile class successfully models horizontal motion using basic trigonometry and physics principles. The horiz method accurately calculates distance traveled over time using the cosine component of the initial velocity.

Updated on: 2026-03-15T23:19:00+05:30

222 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements