How does the following normal function work (pseudo code) ?
I couldn't see the math behind it directly so help me.
It finds the normal to the procedurally generated terrain
Vector2 normal(x,y)
{
Vector2 avg
for(int w = -5; w <= 5; w++)
{
for(int h = -5; h <= 5; h++)
{
if pixel at terrain is solid at (x+w,y+h)
{
avg -= (w,h)
}
}
}
return avg/hypot(avg.x,avg.y) //normalize
}