Simplification of NetworkClippedLine

This commit is contained in:
c0d3d3v 2022-05-03 13:48:02 +02:00
parent 0a6902acae
commit b5166f4048
No known key found for this signature in database
GPG key ID: 068AF680530DFF31

View file

@ -107,48 +107,16 @@ bool NetworkClippedLine(const CGameContext *pGameServer, int SnappingClient, vec
vec2 &ViewPos = pGameServer->m_apPlayers[SnappingClient]->m_ViewPos;
vec2 &ShowDistance = pGameServer->m_apPlayers[SnappingClient]->m_ShowDistance;
float dx1 = ViewPos.x - StartPos.x;
float dy1 = ViewPos.y - StartPos.y;
if(absolute(dx1) <= ShowDistance.x && absolute(dy1) <= ShowDistance.x)
return false;
float dx2 = ViewPos.x - EndPos.x;
float dy2 = ViewPos.y - EndPos.y;
if(absolute(dx2) <= ShowDistance.x && absolute(dy2) <= ShowDistance.x)
return false;
vec2 vecLine = vec2(EndPos.x - StartPos.x, EndPos.y - StartPos.y);
float dLengthLine = length(vecLine);
vecLine /= dLengthLine;
float dDistLine = dot(vecLine, StartPos);
vec2 vecPerpLine = vec2(-vecLine.y, vecLine.x);
float dDistPerpLine = dot(vecPerpLine, StartPos);
vec2 vecRect1 = vec2(ViewPos.x - ShowDistance.x, ViewPos.y - ShowDistance.y);
vec2 vecRect2 = vec2(ViewPos.x + ShowDistance.x, ViewPos.y - ShowDistance.y);
vec2 vecRect3 = vec2(ViewPos.x + ShowDistance.x, ViewPos.y + ShowDistance.y);
vec2 vecRect4 = vec2(ViewPos.x - ShowDistance.x, ViewPos.y + ShowDistance.y);
float dPerpLineDist1 = dot(vecPerpLine, vecRect1) - dDistPerpLine;
float dPerpLineDist2 = dot(vecPerpLine, vecRect2) - dDistPerpLine;
float dPerpLineDist3 = dot(vecPerpLine, vecRect3) - dDistPerpLine;
float dPerpLineDist4 = dot(vecPerpLine, vecRect4) - dDistPerpLine;
float dMinPerpLineDist = minimum(dPerpLineDist1, minimum(dPerpLineDist2, dPerpLineDist3, dPerpLineDist4));
float dMaxPerpLineDist = maximum(dPerpLineDist1, maximum(dPerpLineDist2, dPerpLineDist3, dPerpLineDist4));
// If all distances are positive, or all distances are negative,
// then the rectangle is on one side of the line or the other, so there's no intersection
if((dMinPerpLineDist <= 0.0 && dMaxPerpLineDist <= 0.0) || (dMinPerpLineDist >= 0.0 && dMaxPerpLineDist >= 0.0))
vec2 DistanceToLine, ClosestPoint;
if(closest_point_on_line(StartPos, EndPos, ViewPos, ClosestPoint))
{
return true;
DistanceToLine = ViewPos - ClosestPoint;
}
float dDistLine1 = dot(vecLine, vecRect1) - dDistLine;
float dDistLine2 = dot(vecLine, vecRect2) - dDistLine;
float dDistLine3 = dot(vecLine, vecRect3) - dDistLine;
float dDistLine4 = dot(vecLine, vecRect4) - dDistLine;
float dMinLineDist = minimum(dDistLine1, minimum(dDistLine2, dDistLine3, dDistLine4));
float dMaxLineDist = maximum(dDistLine1, maximum(dDistLine2, dDistLine3, dDistLine4));
// If the rectangle's points don't fall within the line segment's extent, then there's no intersection
return (dMaxLineDist <= 0.0 || dMinLineDist >= dLengthLine);
else
{
// No line section was passed but two equal points
DistanceToLine = ViewPos - StartPos;
}
float ClippDistance = maximum(ShowDistance.x, ShowDistance.y);
return (absolute(DistanceToLine.x) > ClippDistance || absolute(DistanceToLine.y) > ClippDistance);
}