Camera Class

Started by
52 comments, last by Chozo 19 years, 6 months ago
Test came out okay. This:

EnVector3<float> vec(0.0f, 0.0f, 1.0f);std::cout << "Test1 before: " << vec.to_string() << std::endl;EnQuaternion<float> quat(std::cos(M_PI_2 / 2.0f), std::sin(M_PI_2 / 2.0f), 0.0f, 0.0f);std::cout << "Test1 after: " << quat.rotate(vec).to_string() << std::endl;vec = EnVector3<float>(1.0f, 0.0f, 0.0f);std::cout << "Test2 before: " << vec.to_string() << std::endl;quat = EnQuaternion<float>(std::cos(M_PI_2 / 2.0f), 0.0f, 0.0f, std::sin(M_PI_2 / 2.0f));std::cout << "Test2 after: " << quat.rotate(vec).to_string() << std::endl;vec = EnVector3<float>(0.0f, 1.0f, 0.0f);std::cout << "Test3 before: " << vec.to_string() << std::endl;quat = EnQuaternion<float>(std::cos(M_PI_2 / 2.0f), 0.0f, 0.0f, std::sin(M_PI_2 / 2.0f));std::cout << "Test3 after: " << quat.rotate(vec).to_string() << std::endl;


Gives this:
Test1 before: (0, 0, 1)Test1 after: (0, -1, 0)Test2 before: (1, 0, 0)Test2 after: (0, 1, 0)Test3 before: (0, 1, 0)Test3 after: (-1, 0, 0)


Those look like the proper rotations when I work it out in my head. Just furthering my confusion now.
Advertisement
No ideas?
Quote:Original post by Dmytry
BTW.
Quote:
EnQuaternion<float> q(std::cos(M_PI_2 / 2.0f), 1.0f, 0.0f, 0.0f);
EnVector3<float> v(1.0f, 0.0f, 0.0f);

m_log.vlogln(LogOutputNormal, "Before rotation: %s", v.to_string().c_str());
v = q.rotate(v);
m_log.vlogln(LogOutputNormal, "After rotation: %s", v.to_string().c_str());

- your quaternion is not unit-length.
Therefore, rotated vector is not unit length. So your quaternion class *might* even be correct :)
you can test your quaternion against other quaternion class such as my.


- Simply said, your first test is incorrect.
and result
Quote:
I get this output:
Before rotation: (1, 0, 0)
After rotation: (1.5, 0, 0)

probably may be ignored.
But my second set of tests came out okay. Which, to the best of my knowledge says my quat class is rotating vectors okay. Is that correct? I'm at a loss for what I need to fix because everything seems like it should be working.
If you want to to completely make sure(not only for special cases), you can compare your results for arbitrary quaternion and arbitrary vector to my results:
Vec3d v(1.2345,2.3456,4.67342);	Quaterniond q(0.592031,0.43251,-0.75362,0.12415);// constructor parameters: (scalar,x,y,z)	Normalize(q);	Quaterniond q2(-0.23456634,0.942123,-0.42121,0.374812);		Normalize(q2);	cout<<"q="<<q<<endl;	cout<<"v="<<v<<endl;	cout<<"q*v="<<QuaternionToMatrix(q)*v<<endl;	cout<<"q2="<<q2<<endl;	cout<<"q*q2="<<q*q2<<endl;results:q=quaternion:[0.559187,      0.408515,       -0.711811,      0.117262]v=vector:[1.2345,        2.3456, 4.67342]q*v=vector:[-4.9948,       -1.97318,       0.158859]q2=quaternion:[-0.208925,     0.839137,       -0.375166,      0.33384]q*q2=quaternion:[-0.765823,     0.190247,       -0.0990524,     0.606226]

If your code outputs same results, it must be right, if it outputs different results it might be right if my quaternion rotation is accidently non-standard(mirrored) (really hard to notice such problem) . Your rotation surely must be standard...
BTW, there's probably a language confusion. By "your first test is incorrect." i meant that "test is incorrect" , not "quaternion rotation is incorrect".

[Edited by - Dmytry on October 24, 2004 3:11:57 PM]
Is it important to note that if I move beind an object and then turn around, the controls end up reversed?
Quote:Original post by Chozo
Is it important to note that if I move beind an object and then turn around, the controls end up reversed?

if there's bug, you need to test your quaternion, then test something else, till you find the bug. Test arbitrary quaternions i gave - if it works, your quaternion*vector and quaternion*quaternion code is correct.
Could this be part of the problem (these are from my quat class)?

    // vector multiplication    template <class V>    EnQuaternion<T> operator*(const EnVector3<V>& rhs)    {        return *this * EnQuaternion<T>(0, rhs);    }    // vector multiplication    // and assignment    template <class V>    const EnQuaternion<T>& operator*=(const EnVector3<V>& rhs)    {        *this = *this * EnQuaternion<T>(0, rhs);        return *this;    }


My q * v returns a quaternion, not a vector (* as in multiply, not rotate). Everything I read said that when you multiply a quat and a vector, it's the same as multiplying the quat by a new quat with a scalar of 0 and the vector as the vector multiplying by. ie, quat(0, vector).

All the other tests you gave me came out as expected.
Quote:Original post by Chozo
Could this be part of the problem (these are from my quat class)?

*** Source Snippet Removed ***

My q * v returns a quaternion, not a vector (* as in multiply, not rotate). Everything I read said that when you multiply a quat and a vector, it's the same as multiplying the quat by a new quat with a scalar of 0 and the vector as the vector multiplying by. ie, quat(0, vector).

All the other tests you gave me came out as expected.


I'd not define that multiplication at all to be safe. I don't sure it's mathematically defined thing. You shouldn't be using it too often.


It's one of reasons why to rotate vector using quaternion i use
QuaternionToMatrix(q)*v;


I don't have quaternion*vector operator, i just accidently wrote q*v in that test for cout , it's may be somewhat confusing.

Also i see std::memmove in your code. Don't use it, just use = .

This topic is closed to new replies.

Advertisement