简体   繁体   中英

Finding Distance from ray to 3D bezier-curve

I'm wondering if anyone knows how to find the smallest distance from a 3D line segment or ray to a 3D cubic-bezier curve or if there is anything built into the Unity game engine.

Calculate affine transformation that transform ray base point into (0,0,0) and direction becomes OX

If ray is defined by base point (rx0, ry0, rz0) and direction vector (dx, dy, dz) with length len (1 for normalized), then matrix of this transform is product of:

  • shift by (-rx0, -ry0, -rz0) -
  • then rotation around OZ axis by atan2(ry, rx)
  • then rotation around OY axis by acos(dz / len)

Apply this transform to Bezier curve contol points

Calculate minimal distance from curve to OX axis using zer0derivative approach (derivative is zero for max and min of function):

Write expression for squared distance depending of t :

SquaredDist(t) = by'(t)^2  + bz'(t)^2

Calculate derivative of SquaredDist by dt , make equation

SquaredDist' = 0

and solve it against t .

Equation is 5-th order polynomial equation, so no analytical, only numerical solution (or subdivision approach).

Check roots in 0..1 interval, also check Bezier curve ends and ray start point distance separately.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM