Closest point on a̶ ̶s̶u̶r̶f̶a̶c̶e̶ 3d bell curve to a point

67 Views Asked by At

I am developing a game using SDFs and ray marching. High school starts next year so I don't really know any calculus (not even pre calc) or linear algebra.

Here's the problem:

A surface is defined by this function (removed constants, not sure if it changes anything): $$S(x,y) = e^{-(x^2+y^2)}$$ graph of S

A vector A is on the surface if it satisfies $S(A_x,A_y) = A_z$ .This is the distance function (with constants removed): $$\mathrm{dist}(a,b) = \sqrt{(a_x - b_x)^2 + (a_y - b_y)^2 + (a_z - b_z)^2}$$

Given any vector A, how can I find a vector B on the surface, such that $\mathrm{dist}(A, B)$ is minimum, or at least approximate it?

2

There are 2 best solutions below

0
On

The distance between a point $A(x_a,y_a,z_a)$ and the surface is $$d(x,y)=\sqrt{(x-x_a)^2+(y-y_a)^2+\left(e^{-x^2-y^2}-z_a\right)^2}$$ to minimize this function we should solve the system $$\begin{cases} (x-x_a)-2 x e^{-x^2-y^2} \left(e^{-x^2-y^2}-z_a\right)=0\\ (y-y_a)-2 y e^{-x^2-y^2} \left(e^{-x^2-y^2}-z_a\right)=0\\ \end{cases} $$ which is not solvable with elementary formulas.

One should implement a subprogram that solves the system above numerically.

For instance I tried to solve approximately when $A(2,5,3)$ and got

$x = 0.36, \;y= 0.86$

The real problem is finding the values where to start the approximation.

Linearizing the problem I found that a good start is

$$x_0= \frac{x_a}{2 z_a-1},y_0= \frac{y_a}{2 z_a-1}$$

0
On

as @Raffaele described, the obvious (and accurate) approach is to compute the partial derivatives and iteratively optimize, which is slow, really heavy on the hardware and requires multivariable calculus which I understand absolutely nothing, yet

while playing around a little, i noticed that the surface is symmetrical along the z axis, this can be exploited to perform faster optimization, or a straightforward algebraic approach

instead of $Surface(x,y) = e^{-(x^2+y^2)}$ we can use $Surface(x) = e^{-x^2}$

$m = Polar(A_x, A_y)$

$n = A_z$

$B = intersection( (m, Surface(m)), Slope(m), (m,n), Slope(m)^{-1})$

these functions can be changed to according to the surface:

$Polar(x,y)=|(x,y)|$

$Slope(x) = \partial S / \partial x $ (but $Slope(x)=x*k$ works for me which is faster)

$intersection(A, S_a, B, S_b) = (buf, S_a*(buf-A_x)+A_y)$

where $buf=\frac{(S_a*A_x - A_y - S_b * B_x + B_y)}{(S_a - S_b)}$