Skip to content

Commit

Permalink
- changed math utilities to floating point coordinate system.
Browse files Browse the repository at this point in the history
  • Loading branch information
coelckers committed Feb 15, 2022
1 parent 8cc8ebf commit 71943ab
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 25 deletions.
16 changes: 8 additions & 8 deletions source/core/gamefuncs.h
Expand Up @@ -357,10 +357,10 @@ inline double SquareDist(double lx1, double ly1, double lx2, double ly2)

inline DVector2 NearestPointLine(double px, double py, const walltype* wal)
{
double lx1 = wal->wall_int_pos().X;
double ly1 = wal->wall_int_pos().Y;
double lx2 = wal->point2Wall()->wall_int_pos().X;
double ly2 = wal->point2Wall()->wall_int_pos().Y;
double lx1 = wal->pos.X;
double ly1 = wal->pos.Y;
double lx2 = wal->point2Wall()->pos.X;
double ly2 = wal->point2Wall()->pos.Y;

double wall_length = SquareDist(lx1, ly1, lx2, ly2);

Expand All @@ -374,10 +374,10 @@ inline DVector2 NearestPointLine(double px, double py, const walltype* wal)

inline double SquareDistToWall(double px, double py, const walltype* wal, DVector2* point = nullptr)
{
double lx1 = wal->wall_int_pos().X;
double ly1 = wal->wall_int_pos().Y;
double lx2 = wal->point2Wall()->wall_int_pos().X;
double ly2 = wal->point2Wall()->wall_int_pos().Y;
double lx1 = wal->pos.X;
double ly1 = wal->pos.Y;
double lx2 = wal->point2Wall()->pos.X;
double ly2 = wal->point2Wall()->pos.Y;

double wall_length = SquareDist(lx1, ly1, lx2, ly2);

Expand Down
5 changes: 2 additions & 3 deletions source/core/rendering/scene/hw_bunchdrawer.cpp
Expand Up @@ -463,9 +463,8 @@ int BunchDrawer::WallInFront(int line1, int line2)
return -1;

DVector2 intersect;
SquareDistToWall(x1s * 16., y1s * -16., &wall[line2], &intersect);
intersect.X *= (1 / 16.);
intersect.Y *= (1 / -16.);
SquareDistToWall(x1s, -y1s, &wall[line2], &intersect);
intersect.Y = -intersect.Y;

if (d3 < max_overlap)
{
Expand Down
25 changes: 14 additions & 11 deletions source/core/rendering/scene/hw_walls.cpp
Expand Up @@ -50,12 +50,15 @@ DCoreActor* wall_to_sprite_actors[8]; // gets updated each frame. Todo: Encapsul

static walltype* IsOnWall(tspritetype* tspr, int height, DVector2& outpos)
{
double maxorthdist = 3; // maximum orthogonal distance to be considered an attached sprite.
double maxdistsq = 3 * 3;
double maxorthdist = 3 * maptoworld; // maximum orthogonal distance to be considered an attached sprite.
double maxdistsq = maxorthdist * maxorthdist;
walltype* best = nullptr;

auto sect = tspr->sectp;

float tx = tspr->pos.X * (float)inttoworld;
float ty = tspr->pos.Y * (float)inttoworld;

for(auto& wal : wallsofsector(sect))
{
// Intentionally include two sided walls. Even on them the sprite should be projected onto the wall for better results.
Expand All @@ -71,7 +74,7 @@ static walltype* IsOnWall(tspritetype* tspr, int height, DVector2& outpos)
// In Wanton Destruction's airplane level there's such a sprite assigned to the wrong sector.
if (d.X == 0)
{
double newdist = fabs(tspr->pos.X - wal.wall_int_pos().X);
double newdist = fabs(tx - wal.pos.X);
if (newdist < maxorthdist)
{
maxorthdist = newdist;
Expand All @@ -80,7 +83,7 @@ static walltype* IsOnWall(tspritetype* tspr, int height, DVector2& outpos)
}
else if (d.Y == 0)
{
double newdist = fabs(tspr->pos.Y - wal.wall_int_pos().Y);
double newdist = fabs(ty - wal.pos.Y);
if (newdist < maxorthdist)
{
maxorthdist = newdist;
Expand All @@ -89,7 +92,7 @@ static walltype* IsOnWall(tspritetype* tspr, int height, DVector2& outpos)
}
else
{
double wdist = SquareDistToWall(tspr->pos.X, tspr->pos.Y, &wal, &outpos);
double wdist = SquareDistToWall(tx, ty, &wal, &outpos);
if (wdist <= maxdistsq)
{
return &wal;
Expand Down Expand Up @@ -1140,12 +1143,12 @@ void HWWall::ProcessWallSprite(HWDrawInfo* di, tspritetype* spr, sectortype* sec
if (walldist)
{
// project the sprite right onto the wall.
auto v1 = NearestPointLine(pos[0].X, pos[0].Y, walldist);
auto v2 = NearestPointLine(pos[1].X, pos[1].Y, walldist);
glseg.x1 = v1.X * (1 / 16.f);
glseg.y1 = v1.Y * (1 / -16.f);
glseg.x2 = v2.X * (1 / 16.f);
glseg.y2 = v2.Y * (1 / -16.f);
auto v1 = NearestPointLine(glseg.x1, -glseg.y1, walldist);
auto v2 = NearestPointLine(glseg.x2, -glseg.y2, walldist);
glseg.x1 = v1.X;
glseg.y1 = -v1.Y;
glseg.x2 = v2.X;
glseg.y2 = -v2.Y;

}

Expand Down
4 changes: 2 additions & 2 deletions source/games/blood/src/gameutil.cpp
Expand Up @@ -772,7 +772,7 @@ BitArray GetClosestSpriteSectors(sectortype* pSector, int x, int y, int nDist, T
BitArray sectorMap(sector.Size()); // this gets returned to the caller.
sectorMap.Zero();
sectorMap.Set(sectnum(pSector));
double nDist4sq = 256. * nDist * nDist; // (nDist * 16)^2 - * 16 to account for Build's 28.4 fixed point format.
double nDist4sq = nDist * nDist; // (nDist * 16)^2 - * 16 to account for Build's 28.4 fixed point format.

BFSSectorSearch search(pSector);

Expand All @@ -792,7 +792,7 @@ BitArray GetClosestSpriteSectors(sectortype* pSector, int x, int y, int nDist, T
}
else // new method using proper math and no bad shortcut.
{
double dist1 = SquareDistToWall(x, y, &wal);
double dist1 = SquareDistToWall(x * inttoworld, y * inttoworld, &wal);
withinRange = dist1 <= nDist4sq;
}
if (withinRange) // if new sector is within range, add it to the processing queue
Expand Down
2 changes: 1 addition & 1 deletion source/games/exhumed/src/lighting.cpp
Expand Up @@ -254,7 +254,7 @@ void AddFlash(sectortype* pSector, int x, int y, int z, int val)

if (!var_18)
{
walldist = (((int)sqrt(SquareDistToWall(x, y, &wal))) >> 4) - 255;
walldist = (int)sqrt(SquareDistToWall(x * inttoworld, y * inttoworld, &wal)) - 255;
}

if (walldist < 0)
Expand Down

0 comments on commit 71943ab

Please sign in to comment.