Skip to content

Instantly share code, notes, and snippets.

@hackergrrl
Created April 13, 2021 21:40
Show Gist options
  • Save hackergrrl/def505154b671d35e2a477d316662988 to your computer and use it in GitHub Desktop.
Save hackergrrl/def505154b671d35e2a477d316662988 to your computer and use it in GitHub Desktop.
/**
* @param {Number} px - Player X
* @param {Number} py - Player Y
* @param {Number} pvx - Player Velocity-X
* @param {Number} pvy - Player Velocity-Y
* @param {Number} tx - Turret X
* @param {Number} ty - Turret Y
* @param {Number} ts - Turret Projectile Speed
**/
function getProjVel (px, py, pvx, pvy, tx, ty, ts) {
const x = (px + pvx * (tx - px) - tx) / ((tx - px) * ts)
const y = (py + pvy * (ty - py) - ty) / ((ty - py) * ts)
return [x, y]
}
// Example use
const targetVelocity = getProjVel(0, 0, 0, 0, 5, 5, 1)
const targetAngle = Math.atan2(targetVelocity[1], targetVelocity[0]) * (180 / Math.PI)
console.log(targetVelocity, targetAngle)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment