summaryrefslogtreecommitdiff
path: root/core/math
diff options
context:
space:
mode:
authorJuan Linietsky <reduzio@gmail.com>2014-08-01 22:10:38 -0300
committerJuan Linietsky <reduzio@gmail.com>2014-08-01 22:10:38 -0300
commit678948068bbde7f12a9c5f28a467b6cf4d127851 (patch)
tree75572f3a5cc6089a6ca3046e9307d0a7c0b72c51 /core/math
parent9ff6d55822647c87eef392147ea15641d0922d47 (diff)
Small Issues & Maintenance
-=-=-=-=-=-=-=-=-=-=-=-=-= -Begin work on Navigation Meshes (simple pathfinding for now, will improve soon) -More doc on theme overriding -Upgraded OpenSSL to version without bugs -Misc bugfixes
Diffstat (limited to 'core/math')
-rw-r--r--core/math/face3.cpp96
-rw-r--r--core/math/face3.h1
-rw-r--r--core/math/math_2d.cpp5
-rw-r--r--core/math/math_2d.h3
4 files changed, 104 insertions, 1 deletions
diff --git a/core/math/face3.cpp b/core/math/face3.cpp
index 814f2d675d..1adc95e4e9 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;
+}
diff --git a/core/math/face3.h b/core/math/face3.h
index 630c408de3..5a509299a2 100644
--- a/core/math/face3.h
+++ b/core/math/face3.h
@@ -68,6 +68,7 @@ public:
real_t get_area() const;
Vector3 get_median_point() const;
+ Vector3 get_closest_point_to(const Vector3& p_point) const;
bool intersects_ray(const Vector3& p_from,const Vector3& p_dir,Vector3 * p_intersection=0) const;
bool intersects_segment(const Vector3& p_from,const Vector3& p_dir,Vector3 * p_intersection=0) const;
diff --git a/core/math/math_2d.cpp b/core/math/math_2d.cpp
index 6c160abaca..3aaa539fbb 100644
--- a/core/math/math_2d.cpp
+++ b/core/math/math_2d.cpp
@@ -77,6 +77,11 @@ float Vector2::angle_to(const Vector2& p_vector2) const {
return Math::atan2( tangent().dot(p_vector2), dot(p_vector2) );
}
+float Vector2::angle_to_point(const Vector2& p_vector2) const {
+
+ return Math::atan2( x-p_vector2.x, y - p_vector2.y );
+}
+
float Vector2::dot(const Vector2& p_other) const {
return x*p_other.x + y*p_other.y;
diff --git a/core/math/math_2d.h b/core/math/math_2d.h
index 3cc5bdc843..fa40d305f5 100644
--- a/core/math/math_2d.h
+++ b/core/math/math_2d.h
@@ -90,7 +90,8 @@ struct Vector2 {
float distance_to(const Vector2& p_vector2) const;
float distance_squared_to(const Vector2& p_vector2) const;
float angle_to(const Vector2& p_vector2) const;
-
+ float angle_to_point(const Vector2& p_vector2) const;
+
float dot(const Vector2& p_other) const;
float cross(const Vector2& p_other) const;
Vector2 cross(real_t p_other) const;