Fixed bad angle math when looking for ships in arc area
This commit is contained in:
parent
cda87c3f06
commit
e7c743716f
|
@ -5,11 +5,13 @@ module TK.SpaceTac.Specs {
|
||||||
check.nears(arenaAngle({ x: 0, y: 0 }, { x: 1, y: 1 }), Math.PI / 4);
|
check.nears(arenaAngle({ x: 0, y: 0 }, { x: 1, y: 1 }), Math.PI / 4);
|
||||||
})
|
})
|
||||||
|
|
||||||
test.case("gets an angular distance", check => {
|
test.case("computes an angular difference", check => {
|
||||||
check.equals(angularDistance(0.5, 1.5), 1.0);
|
check.equals(angularDifference(0.5, 1.5), 1.0);
|
||||||
check.nears(angularDistance(0.5, 1.5 + Math.PI * 6), 1.0);
|
check.nears(angularDifference(0.5, 1.5 + Math.PI * 6), 1.0);
|
||||||
check.same(angularDistance(0.5, -0.5), -1.0);
|
check.same(angularDifference(0.5, -0.5), -1.0);
|
||||||
check.nears(angularDistance(0.5, -0.3 - Math.PI * 4), -0.8);
|
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 => {
|
test.case("converts between degrees and radians", check => {
|
||||||
|
|
|
@ -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 {
|
export function angularDifference(angle1: number, angle2: number): number {
|
||||||
return (angle2 - angle1) % (Math.PI * 2);
|
let diff = angle2 - angle1;
|
||||||
|
return diff - Math.PI * 2 * Math.floor((diff + Math.PI) / (Math.PI * 2));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -141,7 +141,7 @@ module TK.SpaceTac {
|
||||||
if (dist < 0.000001 || dist > this.range) {
|
if (dist < 0.000001 || dist > this.range) {
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
return Math.abs(angularDistance(arenaAngle(source, ship.location), angle)) < maxangle;
|
return Math.abs(angularDifference(arenaAngle(source, ship.location), angle)) < maxangle;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
@ -195,7 +195,7 @@ module TK.SpaceTac {
|
||||||
if (arenaDistance(ship.location, target) > 1e-6) {
|
if (arenaDistance(ship.location, target) > 1e-6) {
|
||||||
// Face the target
|
// Face the target
|
||||||
let angle = arenaAngle(ship.location, 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 destination = new ArenaLocationAngle(ship.arena_x, ship.arena_y, angle);
|
||||||
let engine = first(ship.listEquipment(SlotType.Engine), () => true);
|
let engine = first(ship.listEquipment(SlotType.Engine), () => true);
|
||||||
result.push(new ShipMoveDiff(ship, ship.location, destination, engine));
|
result.push(new ShipMoveDiff(ship, ship.location, destination, engine));
|
||||||
|
|
Loading…
Reference in a new issue