about summary refs log tree commit diff
path: root/src/game/g_vmath.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/game/g_vmath.h')
-rw-r--r--src/game/g_vmath.h15
1 files changed, 15 insertions, 0 deletions
diff --git a/src/game/g_vmath.h b/src/game/g_vmath.h
index db7f4a50..ac3e1870 100644
--- a/src/game/g_vmath.h
+++ b/src/game/g_vmath.h
@@ -65,6 +65,21 @@ inline vector2_base<T> normalize(const vector2_base<T> &v)
 typedef vector2_base<float> vec2;
 typedef vector2_base<bool> bvec2;
 typedef vector2_base<int> ivec2;
+
+template<typename T>
+inline vector2_base<T> closest_point_on_line(vector2_base<T> line_point0, vector2_base<T> line_point1, vector2_base<T> target_point)
+{
+	vector2_base<T> c = target_point - line_point0;
+	vector2_base<T> v = (line_point1 - line_point0);
+	v = normalize(v);
+	T d = length(line_point0-line_point1);
+	T t = dot(v, c)/d;
+	return mix(line_point0, line_point1, clamp(t, (T)0, (T)1));
+	/*
+	if (t < 0) t = 0;
+	if (t > 1.0f) return 1.0f;
+	return t;*/
+}
 	
 // ------------------------------------
 template<typename T>