diff options
| author | Juan Linietsky | 2014-08-01 22:10:38 -0300 |
|---|---|---|
| committer | Juan Linietsky | 2014-08-01 22:10:38 -0300 |
| commit | 678948068bbde7f12a9c5f28a467b6cf4d127851 (patch) | |
| tree | 75572f3a5cc6089a6ca3046e9307d0a7c0b72c51 /core/math/face3.cpp | |
| parent | 9ff6d55822647c87eef392147ea15641d0922d47 (diff) | |
| download | godot-678948068bbde7f12a9c5f28a467b6cf4d127851.tar.gz godot-678948068bbde7f12a9c5f28a467b6cf4d127851.tar.zst godot-678948068bbde7f12a9c5f28a467b6cf4d127851.zip | |
Diffstat (limited to 'core/math/face3.cpp')
| -rw-r--r-- | core/math/face3.cpp | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/core/math/face3.cpp b/core/math/face3.cpp index 814f2d675..1adc95e4e 100644 --- a/core/math/face3.cpp +++ b/core/math/face3.cpp @@ -356,4 +356,100 @@ void Face3::get_support(const Vector3& p_normal,const Transform& p_transform,Vec } +Vector3 Face3::get_closest_point_to(const Vector3& p_point) const { + Vector3 edge0 = vertex[1] - vertex[0]; + Vector3 edge1 = vertex[2] - vertex[0]; + Vector3 v0 = vertex[0] - p_point; + + float a = edge0.dot( edge0 ); + float b = edge0.dot( edge1 ); + float c = edge1.dot( edge1 ); + float d = edge0.dot( v0 ); + float e = edge1.dot( v0 ); + + float det = a*c - b*b; + float s = b*e - c*d; + float t = b*d - a*e; + + if ( s + t < det ) + { + if ( s < 0.f ) + { + if ( t < 0.f ) + { + if ( d < 0.f ) + { + s = CLAMP( -d/a, 0.f, 1.f ); + t = 0.f; + } + else + { + s = 0.f; + t = CLAMP( -e/c, 0.f, 1.f ); + } + } + else + { + s = 0.f; + t = CLAMP( -e/c, 0.f, 1.f ); + } + } + else if ( t < 0.f ) + { + s = CLAMP( -d/a, 0.f, 1.f ); + t = 0.f; + } + else + { + float invDet = 1.f / det; + s *= invDet; + t *= invDet; + } + } + else + { + if ( s < 0.f ) + { + float tmp0 = b+d; + float tmp1 = c+e; + if ( tmp1 > tmp0 ) + { + float numer = tmp1 - tmp0; + float denom = a-2*b+c; + s = CLAMP( numer/denom, 0.f, 1.f ); + t = 1-s; + } + else + { + t = CLAMP( -e/c, 0.f, 1.f ); + s = 0.f; + } + } + else if ( t < 0.f ) + { + if ( a+d > b+e ) + { + float numer = c+e-b-d; + float denom = a-2*b+c; + s = CLAMP( numer/denom, 0.f, 1.f ); + t = 1-s; + } + else + { + s = CLAMP( -e/c, 0.f, 1.f ); + t = 0.f; + } + } + else + { + float numer = c+e-b-d; + float denom = a-2*b+c; + s = CLAMP( numer/denom, 0.f, 1.f ); + t = 1.f - s; + } + } + + return vertex[0] + s * edge0 + t * edge1; + +} |
