diff --git a/src/math/vector.cpp b/src/math/vector.cpp index 59c9de26..ef6371b1 100644 --- a/src/math/vector.cpp +++ b/src/math/vector.cpp @@ -504,7 +504,9 @@ template<> quat quat::rotate(float degrees, float x, float y, float z) template<> quat quat::rotate(vec3 const &src, vec3 const &dst) { vec3 v = cross(src, dst); +v.printf(); float d = dot(src, dst) + lol::sqrt(sqlength(src) * sqlength(dst)); +fprintf(stderr, "%f + %f = %f\n", dot(src, dst), lol::sqrt(sqlength(src) * sqlength(dst)), d); return normalize(quat(d, v.x, v.y, v.z)); } diff --git a/test/unit/quat.cpp b/test/unit/quat.cpp index e6993561..de5ac87a 100644 --- a/test/unit/quat.cpp +++ b/test/unit/quat.cpp @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright: (c) 2010-2011 Sam Hocevar +// Copyright: (c) 2010-2013 Sam Hocevar // This program is free software; you can redistribute it and/or // modify it under the terms of the Do What The Fuck You Want To // Public License, Version 2, as published by Sam Hocevar. See @@ -165,6 +165,29 @@ LOLUNIT_FIXTURE(QuaternionTest) LOLUNIT_ASSERT_DOUBLES_EQUAL(e.y, d.y, 1e-5); LOLUNIT_ASSERT_DOUBLES_EQUAL(e.z, d.z, 1e-5); } + + LOLUNIT_TEST(FromTwoVectors) + { + vec3 a(1.f, 2.f, 3.f); + vec3 b(4.f, 5.f, 6.f); + float ratio = length(a) / length(b); + + quat q = quat::rotate(a, b); + + /* Check that q transforms a into b */ + vec3 c = q.transform(a); + + LOLUNIT_ASSERT_DOUBLES_EQUAL(c.x, b.x * ratio, 1e-5); + LOLUNIT_ASSERT_DOUBLES_EQUAL(c.y, b.y * ratio, 1e-5); + LOLUNIT_ASSERT_DOUBLES_EQUAL(c.z, b.z * ratio, 1e-5); + + /* Check that ~q transforms b into a */ + vec3 d = (~q).transform(b); + + LOLUNIT_ASSERT_DOUBLES_EQUAL(d.x, a.x / ratio, 1e-5); + LOLUNIT_ASSERT_DOUBLES_EQUAL(d.y, a.y / ratio, 1e-5); + LOLUNIT_ASSERT_DOUBLES_EQUAL(d.z, a.z / ratio, 1e-5); + } }; } /* namespace lol */