1
0
forked from 0ad/0ad

Fix missing parentheses in formation turning angle calculation

The function that is calculating the difference between the two angles
is missing some parentheses.

Didn't notice it sooner as it works in 95% of all cases without them,
but sometimes you can see the formation doing a wrong turn.

reviewed by : @Freagarach

Differential revision: https://code.wildfiregames.com/D4626
This was SVN commit r26874.
This commit is contained in:
marder 2022-05-11 15:22:54 +00:00
parent ed4e07e594
commit dec727ce8e
2 changed files with 41 additions and 1 deletions

View File

@ -892,7 +892,7 @@ Formation.prototype.GetRealOffsetPositions = function(offsets)
Formation.prototype.AreAnglesSimilar = function(a1, a2)
{
const d = Math.abs(a1 - a2) % 2 * Math.PI;
const d = Math.abs(a1 - a2) % (2 * Math.PI);
return d < this.maxTurningAngle || d > 2 * Math.PI - this.maxTurningAngle;
};

View File

@ -0,0 +1,40 @@
Engine.LoadComponentScript("interfaces/Timer.js");
Engine.LoadComponentScript("interfaces/Formation.js");
Engine.LoadComponentScript("Formation.js");
const entity_id = 5;
AddMock(SYSTEM_ENTITY, IID_Timer, {
"SetInterval": () => {},
"SetTimeout": () => {},
});
const formationTemplate = {
"RequiredMemberCount": 2,
"DisabledTooltip": "",
"SpeedMultiplier": 1,
"FormationShape": "square",
"MaxTurningAngle": 0,
"SortingClasses": "Hero Champion Cavalry Melee Ranged",
"SortingOrder": "fillToTheCenter",
"ShiftRows": false,
"UnitSeparationWidthMultiplier": 1,
"UnitSeparationDepthMultiplier": 1,
"WidthDepthRatio": 1,
"Sloppiness": 0
};
const cmpFormation = ConstructComponent(entity_id, "Formation", formationTemplate);
const testingAngles = [];
for (let i = 0; i < 179; i++)
testingAngles.push(i * Math.PI / 180);
TS_ASSERT(testingAngles.every(x => !cmpFormation.AreAnglesSimilar(0, x)));
TS_ASSERT(testingAngles.every(x => !cmpFormation.AreAnglesSimilar(0, -x)));
cmpFormation.maxTurningAngle = Math.PI;
TS_ASSERT(testingAngles.every(x => cmpFormation.AreAnglesSimilar(0, x)));
TS_ASSERT(testingAngles.every(x => cmpFormation.AreAnglesSimilar(0, -x)));