diff --git a/source/Base/Vector3.cpp b/source/Base/Vector3.cpp index 34487808..9882a11e 100644 --- a/source/Base/Vector3.cpp +++ b/source/Base/Vector3.cpp @@ -570,6 +570,25 @@ bool Vector3::IsBetweenPoints(const Vector3 & begin, const Vector3 & end) const return EpsLtEq(GetSquaredDistanceTo(begin), length) && EpsLtEq(GetSquaredDistanceTo(end), length); } +// ------------------------------------------------------------------------------------------------ +void Vector3::Interpolate(const Vector3 & a, const Vector3 & b, Value d) +{ + x = STOVAL(static_cast< Float64 >(b.x) + ((a.x - b.x) * d)); + y = STOVAL(static_cast< Float64 >(b.y) + ((a.y - b.y) * d)); + z = STOVAL(static_cast< Float64 >(b.z) + ((a.z - b.z) * d)); +} + +// ------------------------------------------------------------------------------------------------ +Vector3 Vector3::Interpolated(const Vector3 & vec, Value d) const +{ + const Float64 inv = 1.0 - d; + return Vector3( + STOVAL((vec.x * inv) + (x * d)), + STOVAL((vec.y * inv) + (y * d)), + STOVAL((vec.z * inv) + (z * d)) + ); +} + // ------------------------------------------------------------------------------------------------ const Vector3 & Vector3::Get(CSStr str) { @@ -693,6 +712,8 @@ void Register_Vector3(HSQUIRRELVM vm) .Func(_SC("DistanceTo"), &Vector3::GetDistanceTo) .Func(_SC("SqDistanceTo"), &Vector3::GetSquaredDistanceTo) .Func(_SC("IsBetweenPoints"), &Vector3::IsBetweenPoints) + .Func(_SC("Interpolate"), &Vector3::Interpolate) + .Func(_SC("Interpolated"), &Vector3::Interpolated) // Member Overloads .Overload< void (Vector3::*)(void) >(_SC("Generate"), &Vector3::Generate) .Overload< void (Vector3::*)(Val, Val) >(_SC("Generate"), &Vector3::Generate) diff --git a/source/Base/Vector3.hpp b/source/Base/Vector3.hpp index f9927e47..6c5c9862 100644 --- a/source/Base/Vector3.hpp +++ b/source/Base/Vector3.hpp @@ -409,6 +409,16 @@ struct Vector3 */ bool IsBetweenPoints(const Vector3 & begin, const Vector3 & end) const; + /* -------------------------------------------------------------------------------------------- + * Sets this vector to the linearly interpolated vector between a and b. + */ + void Interpolate(const Vector3 & a, const Vector3 & b, Value d); + + /* -------------------------------------------------------------------------------------------- + * Sets this vector to the linearly interpolated vector between a and b. + */ + Vector3 Interpolated(const Vector3 & vec, Value d) const; + /* -------------------------------------------------------------------------------------------- * Extract the values for components of the Vector3 type from a string. */