# Signed Distance Field Direction

This topic is 2786 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi guys,

I have created a signed distance field (two dimensional) which contains the nearest distance to the boundary like in PixelJunk Shooter (described here: http://fumufumu.q-ga.../shooterGDC.pdf; pages 44 to 46). Now I want to calculate the direction to the nearest boundary for any given point. In the PixelJunk Shooter presentation they talk about gradients and four sample points around the given point, but I'm not very good with calculus.

Is there anyone who can easily explain how to do that? Some pseudo-code would be great :-)

##### Share on other sites
It's probably easier to do with just three points:

 double gradient_x = dist[x+1][y] - dist[x][y]; double gradient_y = dist[x][y+1] - dist[x][y];

You can then normalize the gradient vector and perhaps change its sign, and you'll get the vector that points where you want (approximately).

##### Share on other sites
This sounds really interesting. I will try this tomorrow morning. Thank you very much.

How will this look with more than three points? I think the result will be better in some cases with more sample points, isn't it?

##### Share on other sites
I just read the three pages of the PDF file you referenced. I think what they are doing is this:

 double gradient_x = dist[x+1][y] + dist[x+1][y+1] - dist[x][y] - dist[x][y+1]; double gradient_y = dist[x][y+1] + dist[x+1][y+1] - dist[x][y] - dist[x+1][y];

This is more appropriate if you are going to assign this gradient to the whole square between (x,y) and (x+1,y+1).

##### Share on other sites
I've investigated this some more and found a very simple solution:
[
CODE]
double gradient_x = dist[x-1] - dist[x+1];
double gradient_y = dist[y-1] - dist[y+1];

After normalizing the result it looks smooth and nice :-)

Thank you guys for your help.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 14
• 30
• 9
• 16
• 22