1
0
Fork 0

Fixed bad angle math when looking for ships in arc area

This commit is contained in:
Michaël Lemaire 2017-12-13 00:24:02 +01:00
parent cda87c3f06
commit e7c743716f
3 changed files with 13 additions and 10 deletions

View file

@ -5,11 +5,13 @@ module TK.SpaceTac.Specs {
check.nears(arenaAngle({ x: 0, y: 0 }, { x: 1, y: 1 }), Math.PI / 4);
})
test.case("gets an angular distance", check => {
check.equals(angularDistance(0.5, 1.5), 1.0);
check.nears(angularDistance(0.5, 1.5 + Math.PI * 6), 1.0);
check.same(angularDistance(0.5, -0.5), -1.0);
check.nears(angularDistance(0.5, -0.3 - Math.PI * 4), -0.8);
test.case("computes an angular difference", check => {
check.equals(angularDifference(0.5, 1.5), 1.0);
check.nears(angularDifference(0.5, 1.5 + Math.PI * 6), 1.0);
check.same(angularDifference(0.5, -0.5), -1.0);
check.nears(angularDifference(0.5, -0.3 - Math.PI * 4), -0.8);
check.nears(angularDifference(-3 * Math.PI / 4, 3 * Math.PI / 4), -Math.PI / 2);
check.nears(angularDifference(3 * Math.PI / 4, -3 * Math.PI / 4), Math.PI / 2);
})
test.case("converts between degrees and radians", check => {

View file

@ -59,10 +59,11 @@ module TK.SpaceTac {
}
/**
* Get the "angular distance" between two angles in radians
* Get the "angular difference" between two angles in radians, in ]-pi,pi] range.
*/
export function angularDistance(angle1: number, angle2: number): number {
return (angle2 - angle1) % (Math.PI * 2);
export function angularDifference(angle1: number, angle2: number): number {
let diff = angle2 - angle1;
return diff - Math.PI * 2 * Math.floor((diff + Math.PI) / (Math.PI * 2));
}
/**

View file

@ -141,7 +141,7 @@ module TK.SpaceTac {
if (dist < 0.000001 || dist > this.range) {
return false;
} else {
return Math.abs(angularDistance(arenaAngle(source, ship.location), angle)) < maxangle;
return Math.abs(angularDifference(arenaAngle(source, ship.location), angle)) < maxangle;
}
});
} else {
@ -195,7 +195,7 @@ module TK.SpaceTac {
if (arenaDistance(ship.location, target) > 1e-6) {
// Face the target
let angle = arenaAngle(ship.location, target);
if (Math.abs(angularDistance(angle, ship.arena_angle)) > 1e-6) {
if (Math.abs(angularDifference(angle, ship.arena_angle)) > 1e-6) {
let destination = new ArenaLocationAngle(ship.arena_x, ship.arena_y, angle);
let engine = first(ship.listEquipment(SlotType.Engine), () => true);
result.push(new ShipMoveDiff(ship, ship.location, destination, engine));