[Chartjs]-Calculate x and y coords for bullet trajectory โ€“ Chart.js

2๐Ÿ‘

โœ…

I had to remove all of the Angular/Typescript stuff, but you should be able to run the below code and paste the output into a graphing program and verify it. What Iโ€™m doing is calculating the angle required to travel the distance input, then calculating the x and y vectors of the initial velocity. Then you can just multiply those by a timestep to get points that you can plot. If you want more granular points, just increase the step variable.

const calcTrajectory = (distance, height, velocity)=> {

  // get target distance - passed in

  // get launch angle to hit target at that distance
  let x = distance;
  let y = height;
  let g = 10.733; // gravity in yds
  let v = velocity / 3; // velocity in yds
  let angle = Math.asin((x*g)/(v*v));
  let step = 100;
  let xVelocity = v * Math.cos(angle);
  let yVelocity = v * Math.sin(angle)
  // graph x y coords based on target distance, launch angle, and bullet drop

  let data = {x:[], y:[]}
  for (let i = 0; i < 100; i++) {
    let time = (i / step);
    data.x.push(time*xVelocity)
    data.y.push(time* yVelocity)
    yVelocity -= (g/step)
  }
console.log(data);
}

calcTrajectory(250, 0, 2024.43)

Leave a comment