Skip to content

Instantly share code, notes, and snippets.

@ssube
Last active January 27, 2016 03:56
Show Gist options
  • Save ssube/4e2a69d77b363ff67ce5 to your computer and use it in GitHub Desktop.
Save ssube/4e2a69d77b363ff67ce5 to your computer and use it in GitHub Desktop.
function withinRadius(v, r) {
return v.l < r;
}
function getDistance(playerA, playerB) {
const x = playerA.position.x - playerB.position.x;
const y = playerA.position.y - playerB.position.y;
const l = Math.sqrt((x * x) + (y * y));
return {x, y, l};
}
function makeUnit(v) {
return {x: v.x / v.l, y: v.y / v.l, l: 1};
}
function angleFromAtan(a) {
if (a > 0) {
return (x * 360 / (2 * Math.PI));
} else {
return ((2*PI + x) * 360 / (2*PI));
}
}
function movementBearing(playerA, playerB) {
// Get the distance as a unit vector
const v = getDistance(playerA, playerB);
if (withinRadius(v, playerB.hearing.radius)) {
const uv = makeUnit(v);
return angleFromAtan(Math.atan2(uv.y, uv.x));
} else {
return -1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment