From 99ec37ff7a647fe872e542c68d114f0b8e712052 Mon Sep 17 00:00:00 2001 From: Faisal Saadatmand Date: Sun, 21 Feb 2021 22:12:15 -0500 Subject: [PATCH] Fixed test area upper limit: out of bound error The upper limit of the test area should be the string's maxlength - 1, otherwise two errors occur when iterating through the area using the <= operator: 1) addresses beyond the last element are indexed, triggering UB; this occurs when object.vPos is on the last row of the vWorldMap matrix. 2) the test area will warp around the screen; this happens when object.vPos is on the last column of the matrix it will wrongly index the element from the first column in the next row, potentially detecting a collision with a rectangle in the wrong position. I believe this error was not detected because the map is surrounded with walls. --- Videos/OneLoneCoder_PGE_CircleVsRect.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Videos/OneLoneCoder_PGE_CircleVsRect.cpp b/Videos/OneLoneCoder_PGE_CircleVsRect.cpp index ee5d923..dbf91cf 100644 --- a/Videos/OneLoneCoder_PGE_CircleVsRect.cpp +++ b/Videos/OneLoneCoder_PGE_CircleVsRect.cpp @@ -149,7 +149,7 @@ public: olc::vi2d vCurrentCell = object.vPos.floor(); olc::vi2d vTargetCell = vPotentialPosition; olc::vi2d vAreaTL = (vCurrentCell.min(vTargetCell) - olc::vi2d(1, 1)).max({ 0,0 }); - olc::vi2d vAreaBR = (vCurrentCell.max(vTargetCell) + olc::vi2d(1, 1)).min(vWorldSize); + olc::vi2d vAreaBR = (vCurrentCell.max(vTargetCell) + olc::vi2d(1, 1)).min(vWorldSize - olc::vi2d(1, 1)); olc::vf2d vRayToNearest; @@ -242,4 +242,4 @@ int main() if (demo.Construct(640, 480, 2, 2)) demo.Start(); return 0; -} \ No newline at end of file +}