已知固定初速和落点的火炮抛物线计算

Posted on Wed 17 November 2021 in 游戏开发

应用于以下场景

一门火炮在高度h2,目标在高度0

火炮开火后,弹药初速度固定为v0,求此弹道公式。

答案

已知 l (发射距离),h2(起点高度差),v0标量速度

img

wolframalpha

这个公式的解析解为

img

转换为代码:

let gravity = 9.8;



function posCalc(endX: number, x: number, h = 0) {

  const v0 = 13;

  let l = endX;

  let g = gravity;

  let h2 = h;

  let angle = Math.acos(Math.sqrt(-Math.sqrt(0 - l ** 4 * v0 ** 4 * (g ** 2 * l ** 2 - 2 * g * h2 * v0 ** 2 - v0 ** 4)) / (h2 ** 2 * v0 ** 4 + l ** 2 * v0 ** 4) + (g * h2 * l ** 2 * v0 ** 2) / (h2 ** 2 * v0 ** 4 + l ** 2 * v0 ** 4) + (l ** 2 * v0 ** 4) / (h2 ** 2 * v0 ** 4 + l ** 2 * v0 ** 4)) / Math.sqrt(2));



  if (Number.isNaN(angle)) {

    return NaN;

  }

  return x * Math.tan(angle) - (gravity * (x ** 2)) / (2 * ((v0 * Math.cos(angle)) ** 2));

}



console.log([0,1,2,3,4,5].map((x) => posCalc(5, x, 2)))

playgroud:

https://www.typescriptlang.org/play/?#code/DYUwLgBA5gTghgNwJZgJ4QLwQJwDoAcA3AFDEBmArgHYDGYSA9lRAA4MDOAwnMDQBQgqAEwAaALghUKAWwBGIGABoIgoQC0JUuQuUAPTTPlKIAC0wQADAEoIAb2IBIGk3aQEF8wEYAzCScvIISRXOFoQEXMAWTgwE1w4WXYBYRErQggAegyIQCorQBd4x2cqVwggkLC1KJi4hKTVNTTM7MBWvwLCgNNUNjBBCnYQKtjcdgBHGDA+MrBQmhBKgCp5iAAmCABqUuDpsIjFlbTSByyIdnM+dwg95Zsl9iQqZcAO4EAYf8BZ5UAhG0f24shQqFAPFhokM4HcqHw+CYugwelJ+pdoPBkGgbNlzh4rlZUSs-I5jnBzGCAHp8AC0nisfFG4z4qygpnWEHcROuGXcNg2q0AA8CSCChIRMiwAQgwHn50BFYuEvJAoGkgkgam+JQQnkGcWpEwuV0ZqyWsEQKHQSxMB2IEAgRRKkCB1WGYwmZiu2VVlyWqw2wI1DshbogABZGYGlqpdu6EaqWVY0a7MdivfaadcSBarZAwKtbUNNXwAKzxu051Vmi2gX5Uf4DLM1ZxJFQpTIQdEIjM2A5HDJlvkV0Dq+JgviIw1oBGhxvNpbF7HLPxZLt-XvV+K1wcJnNMtU60kMzGNi4ly0uBigXDABhQQcAciFN8vygXIHvPZAFmUYDfyfNTUPxWPIFP558O+9aiOOWIkI4SBkE2AByhgKLgwQwXAMF8A+WJ2I4DgwOAFAwMwyEwX4AC+jg4WAeHMLoCIJtsaHPjY258AayLGk21FxuOepNhOEAJiu6E3O6GEbCYJCkaQxBWn+AEXgA2q+niKMsijeIo-qKLmAC6uDSHALAQroNgYAAfKwHDcLwebKK+EC6DZWJWKQQA

验算

import matplotlib.pyplot as plt

#x_data = [0,1,2,3,4,5,6,7]

#y_data = [2, 2.089787159527464, 2, 1.7306385214176083, 1.281702723780289, 0.6531926070880416, -0.15489182865913387, -1.142550583461237]

x_data = [0,1,2,3,4,5]

y_data = [0, 0.11852191096741502, 0.1777828664511225, 0.1777828664511225, 0.11852191096741499, 0]

plt.plot(x_data,y_data)

plt.show()

参考

斜抛运动

https://baike.baidu.com/item/斜抛运动/9905547?fr=aladdin

斜抛运动的运用:烟花燃放和铅球投掷模型:

https://zhuanlan.zhihu.com/p/164584567