From 73b4e8a988911ffe3ae87860bf5d088d64ce7f89 Mon Sep 17 00:00:00 2001 From: Alug Date: Sat, 6 Jan 2024 04:57:18 +0100 Subject: [PATCH] change this --- src/r_main.c | 60 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 19 deletions(-) diff --git a/src/r_main.c b/src/r_main.c index 5b8f527b3..d5131747f 100644 --- a/src/r_main.c +++ b/src/r_main.c @@ -1137,39 +1137,61 @@ void R_Init(void) boolean R_IsPointInSector(sector_t *sector, fixed_t x, fixed_t y) { size_t i; - size_t passes = 0; + line_t *closest = NULL; + fixed_t closestdist = INT32_MAX; for (i = 0; i < sector->linecount; i++) { - vertex_t *v1, *v2; + vertex_t v, *v1, *v2; + fixed_t dist; + + // find the line closest to the point we're looking for. + P_ClosestPointOnLine(x, y, sector->lines[i], &v); v1 = sector->lines[i]->v1; v2 = sector->lines[i]->v2; - // make sure v1 is below v2 - if (v1->y > v2->y) + // do some correction in order to prevent points outside the line from being measured. + if (abs(v1->x - v2->x) > abs(v1->y - v2->y)) { - vertex_t *tmp = v1; - v1 = v2; - v2 = tmp; + if (v.x < v1->x && v.x < v2->x) + v = v1->x < v2->x ? *v1 : *v2; + else if (v.x > v1->x && v.x > v2->x) + v = v1->x > v2->x ? *v1 : *v2; } - else if (v1->y == v2->y) - // horizontal line, we can't match this - continue; - - if (v1->y <= y && y < v2->y) + else { - // if the y axis in inside the line, find the point where we intersect on the x axis... - fixed_t vx = v1->x + (INT64)(v2->x - v1->x) * (y - v1->y) / (v2->y - v1->y); + if (v.y < v1->y && v.y < v2->y) + v = v1->y < v2->y ? *v1 : *v2; + else if (v.y > v1->y && v.y > v2->y) + v = v1->y > v2->y ? *v1 : *v2; + } - // ...and if that point is to the left of the point, count it as inside. - if (vx < x) - passes++; + dist = R_PointToDist2(v.x, v.y, x, y); + if (dist < closestdist) + { + closest = sector->lines[i]; + closestdist = dist; + } + else if (dist == closestdist) + { + // We're probably matching against a corner here + // Of our two lines, find the one with an angle closest towards the mobj + // ...But in the end, we really only care whether the lesser angle is "less than 180", + // so we'll use P_PointOnLineSide on one line and switch to the other if it fails + if ((v.x == closest->v1->x && v.y == closest->v1->y && !P_PointOnLineSide(x, y, closest)) + || (v.x == closest->v2->x && v.y == closest->v2->y && P_PointOnLineSide(x, y, closest))) + { + closest = sector->lines[i]; + closestdist = dist; + } + // If we're NOT at a corner, then either we're on the same side of both lines, + // or a later line in the sector will be closer, so don't bother switching now } } - // and odd number of passes means we're inside the polygon. - return passes % 2; + // if the side of the closest line is in this sector, we're inside of it. + return P_PointOnLineSide(x, y, closest) == 0 ? closest->frontsector == sector : closest->backsector == sector; } //