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)
|
Vector Game::getWallNormal(Vector pos, int sampleArea, float *dist, int obs)
|
||||||
{
|
{
|
||||||
TileVector t(pos);
|
const TileVector t(pos); // snap to grid
|
||||||
//Vector p = t.worldVector();
|
|
||||||
Vector avg;
|
Vector avg;
|
||||||
|
float mindist = HUGE_VALF;
|
||||||
|
const float szf = (TILE_SIZE*(sampleArea-1));
|
||||||
int c = 0;
|
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 x = t.x-sampleArea; x <= t.x+sampleArea; x++)
|
||||||
{
|
{
|
||||||
for (int y = t.y-sampleArea; y <= t.y+sampleArea; y++)
|
for (int y = t.y-sampleArea; y <= t.y+sampleArea; y++)
|
||||||
{
|
{
|
||||||
if (x == t.x && y == t.y) continue;
|
if (x == t.x && y == t.y) continue;
|
||||||
TileVector ct(x,y);
|
const TileVector ct(x,y);
|
||||||
Vector vt = ct.worldVector();
|
|
||||||
if (isObstructed(ct, obs))
|
if (isObstructed(ct, obs))
|
||||||
{
|
{
|
||||||
int xDiff = pos.x-vt.x;
|
Vector v = pos - ct.worldVector();
|
||||||
int yDiff = pos.y-vt.y;
|
const float d = v.getLength2D();
|
||||||
Vector v(xDiff, yDiff);
|
if (d < mindist)
|
||||||
vs.push_back (v);
|
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
if(dist)
|
||||||
}
|
*dist = c ? mindist : -1.0f;
|
||||||
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)
|
|
||||||
{
|
|
||||||
avg.normalize2D();
|
avg.normalize2D();
|
||||||
avg.z = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
avg.x = avg.y = 0;
|
|
||||||
}
|
|
||||||
return avg;
|
return avg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue