소스 검색

Add vector lerp

Daniele Bartolini 10 년 전
부모
커밋
87409432b7
4개의 변경된 파일38개의 추가작업 그리고 0개의 파일을 삭제
  1. 9 0
      src/core/math/vector2.h
  2. 10 0
      src/core/math/vector3.h
  3. 11 0
      src/core/math/vector4.h
  4. 8 0
      src/lua/lua_math.cpp

+ 9 - 0
src/core/math/vector2.h

@@ -174,6 +174,15 @@ inline Vector2 min(const Vector2& a, const Vector2& b)
 	return v;
 }
 
+/// Returns the linearly interpolated vector between @a and @b at time @a t in [0, 1].
+inline Vector2 lerp(const Vector2& a, const Vector2& b, float t)
+{
+	Vector2 v;
+	v.x = lerp(a.x, b.x, t);
+	v.y = lerp(a.y, b.y, t);
+	return v;
+}
+
 /// Returns the pointer to the data of @a a.
 inline float* to_float_ptr(Vector2& a)
 {

+ 10 - 0
src/core/math/vector3.h

@@ -190,6 +190,16 @@ inline Vector3 min(const Vector3& a, const Vector3& b)
 	return v;
 }
 
+/// Returns the linearly interpolated vector between @a and @b at time @a t in [0, 1].
+inline Vector3 lerp(const Vector3& a, const Vector3& b, float t)
+{
+	Vector3 v;
+	v.x = lerp(a.x, b.x, t);
+	v.y = lerp(a.y, b.y, t);
+	v.z = lerp(a.z, b.z, t);
+	return v;
+}
+
 /// Returns the pointer to the data of @a a.
 inline float* to_float_ptr(Vector3& a)
 {

+ 11 - 0
src/core/math/vector4.h

@@ -210,6 +210,17 @@ inline Vector4 min(const Vector4& a, const Vector4& b)
 	return v;
 }
 
+/// Returns the linearly interpolated vector between @a and @b at time @a t in [0, 1].
+inline Vector4 lerp(const Vector4& a, const Vector4& b, float t)
+{
+	Vector4 v;
+	v.x = lerp(a.x, b.x, t);
+	v.y = lerp(a.y, b.y, t);
+	v.z = lerp(a.z, b.z, t);
+	v.w = lerp(a.w, b.w, t);
+	return v;
+}
+
 /// Returns the pointer to the data of @a a.
 inline float* to_float_ptr(Vector4& a)
 {

+ 8 - 0
src/lua/lua_math.cpp

@@ -248,6 +248,13 @@ static int vector3_min(lua_State* L)
 	return 1;
 }
 
+static int vector3_lerp(lua_State* L)
+{
+	LuaStack stack(L);
+	stack.push_vector3(lerp(stack.get_vector3(1), stack.get_vector3(2), stack.get_float(3)));
+	return 1;
+}
+
 static int vector3_forward(lua_State* L)
 {
 	LuaStack stack(L);
@@ -919,6 +926,7 @@ void load_math(LuaEnvironment& env)
 	env.load_module_function("Vector3", "angle",            vector3_angle);
 	env.load_module_function("Vector3", "max",              vector3_max);
 	env.load_module_function("Vector3", "min",              vector3_min);
+	env.load_module_function("Vector3", "lerp",             vector3_lerp);
 	env.load_module_function("Vector3", "forward",          vector3_forward);
 	env.load_module_function("Vector3", "backward",         vector3_backward);
 	env.load_module_function("Vector3", "left",             vector3_left);