mirror of
https://github.com/AquariaOSE/Aquaria.git
synced 2024-11-29 03:33:48 +00:00
Unfuck & optimize Game::getWallNormal()
Another float->int->float roundtrip fixed.
This commit is contained in:
parent
d2c0e3e241
commit
d7bf48fc30
1 changed files with 16 additions and 43 deletions
|
@ -702,63 +702,36 @@ void Game::dilateGrid(unsigned int radius, ObsType test, ObsType set, ObsType al
|
|||
|
||||
Vector Game::getWallNormal(Vector pos, int sampleArea, float *dist, int obs)
|
||||
{
|
||||
TileVector t(pos);
|
||||
//Vector p = t.worldVector();
|
||||
const TileVector t(pos); // snap to grid
|
||||
Vector avg;
|
||||
float mindist = HUGE_VALF;
|
||||
const float szf = (TILE_SIZE*(sampleArea-1));
|
||||
int c = 0;
|
||||
//float maxLen = -1;
|
||||
std::vector<Vector> vs;
|
||||
if (dist != NULL)
|
||||
*dist = -1;
|
||||
for (int x = t.x-sampleArea; x <= t.x+sampleArea; x++)
|
||||
{
|
||||
for (int y = t.y-sampleArea; y <= t.y+sampleArea; y++)
|
||||
{
|
||||
if (x == t.x && y == t.y) continue;
|
||||
TileVector ct(x,y);
|
||||
Vector vt = ct.worldVector();
|
||||
const TileVector ct(x,y);
|
||||
if (isObstructed(ct, obs))
|
||||
{
|
||||
int xDiff = pos.x-vt.x;
|
||||
int yDiff = pos.y-vt.y;
|
||||
Vector v(xDiff, yDiff);
|
||||
vs.push_back (v);
|
||||
Vector v = pos - ct.worldVector();
|
||||
const float d = v.getLength2D();
|
||||
if (d < mindist)
|
||||
mindist = d;
|
||||
if (d < szf)
|
||||
{
|
||||
v.setLength2D(szf - d);
|
||||
avg += v;
|
||||
++c;
|
||||
}
|
||||
|
||||
if (dist!=NULL)
|
||||
{
|
||||
float d = (vt-pos).getLength2D();
|
||||
if (*dist == -1 || d < *dist)
|
||||
{
|
||||
*dist = d;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
const int sz = (TILE_SIZE*(sampleArea-1));
|
||||
for (size_t i = 0; i < vs.size(); i++)
|
||||
{
|
||||
float len = vs[i].getLength2D();
|
||||
if (len < sz)
|
||||
{
|
||||
vs[i].setLength2D(sz - len);
|
||||
c++;
|
||||
avg += vs[i];
|
||||
}
|
||||
}
|
||||
if (c)
|
||||
{
|
||||
avg /= c;
|
||||
if (avg.x != 0 || avg.y != 0)
|
||||
{
|
||||
if(dist)
|
||||
*dist = c ? mindist : -1.0f;
|
||||
avg.normalize2D();
|
||||
avg.z = 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
avg.x = avg.y = 0;
|
||||
}
|
||||
return avg;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue