From bb659fa875c24c5f708c70438d8bbd371a825e8b Mon Sep 17 00:00:00 2001 From: James McLean Date: Tue, 9 Jun 2015 10:55:23 -0400 Subject: Added rot/pos constructor for Matrix32 variant. --- core/variant_call.cpp | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'core') diff --git a/core/variant_call.cpp b/core/variant_call.cpp index 2f7e0205dc..7dd0eb8a0c 100644 --- a/core/variant_call.cpp +++ b/core/variant_call.cpp @@ -750,6 +750,12 @@ static void _call_##m_type##_##m_method(Variant& r_ret,Variant& p_self,const Var r_ret=Rect2(*p_args[0],*p_args[1],*p_args[2],*p_args[3]); } + static void Matrix32_init2(Variant& r_ret,const Variant** p_args) { + + Matrix32 m(*p_args[0], *p_args[1]); + r_ret=m; + } + static void Matrix32_init3(Variant& r_ret,const Variant** p_args) { Matrix32 m; @@ -1544,6 +1550,7 @@ _VariantCall::addfunc(Variant::m_vtype,Variant::m_ret,_SCS(#m_method),VCALL(m_cl _VariantCall::add_constructor(_VariantCall::Rect2_init1,Variant::RECT2,"pos",Variant::VECTOR2,"size",Variant::VECTOR2); _VariantCall::add_constructor(_VariantCall::Rect2_init2,Variant::RECT2,"x",Variant::REAL,"y",Variant::REAL,"width",Variant::REAL,"height",Variant::REAL); + _VariantCall::add_constructor(_VariantCall::Matrix32_init2,Variant::MATRIX32,"rot",Variant::REAL,"pos",Variant::VECTOR2); _VariantCall::add_constructor(_VariantCall::Matrix32_init3,Variant::MATRIX32,"x_axis",Variant::VECTOR2,"y_axis",Variant::VECTOR2,"origin",Variant::VECTOR2); _VariantCall::add_constructor(_VariantCall::Vector3_init1,Variant::VECTOR3,"x",Variant::REAL,"y",Variant::REAL,"z",Variant::REAL); -- cgit v1.2.3 From b19ed63eb6b3aa0fd9cdf54847deff96fd4830df Mon Sep 17 00:00:00 2001 From: James McLean Date: Thu, 11 Jun 2015 10:43:48 -0400 Subject: Implemented interpolation for affine transformations (Matrix32::interpolate_with) --- core/math/math_2d.cpp | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) (limited to 'core') diff --git a/core/math/math_2d.cpp b/core/math/math_2d.cpp index 0a3963f88f..185241e90f 100644 --- a/core/math/math_2d.cpp +++ b/core/math/math_2d.cpp @@ -622,9 +622,41 @@ float Matrix32::basis_determinant() const { } Matrix32 Matrix32::interpolate_with(const Matrix32& p_transform, float p_c) const { + + //extract parameters + Vector2 p1 = get_origin(); + Vector2 p2 = p_transform.get_origin(); + + float r1 = get_rotation(); + float r2 = p_transform.get_rotation(); + + Vector2 s1 = get_scale(); + Vector2 s2 = p_transform.get_scale(); + + //slerp rotation + Vector2 v1(Math::cos(r1), Math::sin(r1)); + Vector2 v2(Math::cos(r2), Math::sin(r2)); + + float dot = v1.dot(v2); + + dot = (dot < -1.0f) ? -1.0f : ((dot > 1.0f) ? 1.0f : dot); //clamp dot to [-1,1] + + Vector2 v; - - return Matrix32(); + if (dot > 0.9995f) { + v = Vector2::linear_interpolate(v1, v2, p_c); //linearly interpolate to avoid numerical precision issues + v.normalize(); + } else { + float angle = Math::acos(dot) * p_c; + Vector2 v3(v2 - v1 * dot); + v3.normalize(); + v = v1 * Math::cos(angle) + v3 * Math::sin(angle); + } + + //construct matrix + Matrix32 res(Math::atan2(v.y, v.x), Vector2::linear_interpolate(p1, p2, p_c)); + res.scale(Vector2::linear_interpolate(s1, s2, p_c)); + return res; } Matrix32::operator String() const { -- cgit v1.2.3 From c2181285eb225065a9b72f473c7a7a24949a7fe2 Mon Sep 17 00:00:00 2001 From: James McLean Date: Thu, 11 Jun 2015 13:44:04 -0400 Subject: Changed 'scale' to 'scale_basis' in 'interpolate_with'. --- core/math/math_2d.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core') diff --git a/core/math/math_2d.cpp b/core/math/math_2d.cpp index 185241e90f..cdb9363b9d 100644 --- a/core/math/math_2d.cpp +++ b/core/math/math_2d.cpp @@ -655,7 +655,7 @@ Matrix32 Matrix32::interpolate_with(const Matrix32& p_transform, float p_c) cons //construct matrix Matrix32 res(Math::atan2(v.y, v.x), Vector2::linear_interpolate(p1, p2, p_c)); - res.scale(Vector2::linear_interpolate(s1, s2, p_c)); + res.scale_basis(Vector2::linear_interpolate(s1, s2, p_c)); return res; } -- cgit v1.2.3 From 2e6d3b7fadc524f8bdc19929659da365d14ad759 Mon Sep 17 00:00:00 2001 From: James McLean Date: Thu, 11 Jun 2015 21:37:54 -0400 Subject: Changed floats to 'real_t'. --- core/math/math_2d.cpp | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) (limited to 'core') diff --git a/core/math/math_2d.cpp b/core/math/math_2d.cpp index cdb9363b9d..88717723ce 100644 --- a/core/math/math_2d.cpp +++ b/core/math/math_2d.cpp @@ -627,8 +627,8 @@ Matrix32 Matrix32::interpolate_with(const Matrix32& p_transform, float p_c) cons Vector2 p1 = get_origin(); Vector2 p2 = p_transform.get_origin(); - float r1 = get_rotation(); - float r2 = p_transform.get_rotation(); + real_t r1 = get_rotation(); + real_t r2 = p_transform.get_rotation(); Vector2 s1 = get_scale(); Vector2 s2 = p_transform.get_scale(); @@ -637,20 +637,18 @@ Matrix32 Matrix32::interpolate_with(const Matrix32& p_transform, float p_c) cons Vector2 v1(Math::cos(r1), Math::sin(r1)); Vector2 v2(Math::cos(r2), Math::sin(r2)); - float dot = v1.dot(v2); + real_t dot = v1.dot(v2); - dot = (dot < -1.0f) ? -1.0f : ((dot > 1.0f) ? 1.0f : dot); //clamp dot to [-1,1] + dot = (dot < -1.0) ? -1.0 : ((dot > 1.0) ? 1.0 : dot); //clamp dot to [-1,1] Vector2 v; - if (dot > 0.9995f) { - v = Vector2::linear_interpolate(v1, v2, p_c); //linearly interpolate to avoid numerical precision issues - v.normalize(); + if (dot > 0.9995) { + v = Vector2::linear_interpolate(v1, v2, p_c).normalized(); //linearly interpolate to avoid numerical precision issues } else { - float angle = Math::acos(dot) * p_c; - Vector2 v3(v2 - v1 * dot); - v3.normalize(); - v = v1 * Math::cos(angle) + v3 * Math::sin(angle); + real_t angle = p_c*Math::acos(dot); + Vector2 v3 = (v2 - v1*dot).normalized(); + v = v1*Math::cos(angle) + v3*Math::sin(angle); } //construct matrix -- cgit v1.2.3