Sign in to follow this  
senseiTU

[SOLVED!] Camera with quaternion

Recommended Posts

>> SOLVED I forgot to normalize the view and side vector at the top of the rotate() function. What a stupid mistake... Hi! I tried to implement a camera that makes use of quaternions for rotations. Unfortunately this doesn't work out as I want it to. The pitch and roll rotations aren't smooth. yaw seems to work. even for as little amounts as 0.05 degrees, the cam seems to "jump" for pitch and roll (the general direction is correct though). Also what is strange for me is that I tried to do the following: reset the _ref, _pos and _up values in the rotate() function and instead passing the function increasing values of yaw for instance (1.0, 2.0,...). In my opinion, this should yield a rotation (1 degree at a time) but it doesn't. The thing does not move at all (actually it is jumping around 1 or 2 pixels but other than that, nothing). Whats wrong with my attempt here? Can anyone tell me if I made an mistake? Here are some code snippets. Let me know if you need anything else...
_cam->rotate(0.0, 0.0, 0.1);


The rotation itself:
void TCamera::rotate(float pitch, float roll, float yaw)
{
    TVector* view = new TVector(_pos, _ref);
    TVector* side = TVector::cross(view, _up);

    view->normalize();    // EDITED (it works now)
    side->normalize();    // EDITED (it works now)

    TQuat* yawPitchRoll = TQuat::createFromYawPitchRoll(_up, view, side, pitch, roll, yaw);    

    yawPitchRoll->print();

    TQuat* t1 = TQuat::product3(yawPitchRoll, new TQuat (0.0, view));
    TQuat* t2 = TQuat::product3(t1,yawPitchRoll->getInverse());
    
    _ref = TVector::add(_pos, t2->getVector());

    TQuat* t3 = TQuat::product3(yawPitchRoll, new TQuat (0.0, _up));
    TQuat* t4 = TQuat::product3(t3,yawPitchRoll->getInverse());
    
    _up = t4->getVector();

    side = TVector::cross(new TVector(_pos, _ref), _up);
    side->normalize();

    _up = TVector::cross(side, new TVector(_pos, _ref));
    _up->normalize();
}




Part of the quaternion class:
TQuat* TQuat::createFromAxisAngle(TVector* axis, float angle)
{
    TQuat* q = new TQuat(cos(((angle/2)*PI)/180), axis->getX()*sin(((angle/2)*PI)/180),
                  axis->getY()*sin(((angle/2)*PI)/180), axis->getZ()*sin(((angle/2)*PI)/180));
    return q;
}




TQuat* TQuat::createFromYawPitchRoll(TVector* up, TVector* view, TVector* side, float pitch, float roll, float yaw)
{
    TQuat* qyaw = TQuat::createFromAxisAngle(up, yaw);
    qyaw->normalize();
    TQuat* qtilt = TQuat::createFromAxisAngle(side, pitch);
    qtilt->normalize();
    TQuat* qroll = TQuat::createFromAxisAngle(view, roll);
    qroll->normalize();

    TQuat* t1 = TQuat::product3(TQuat::product3(qyaw, qtilt), qroll);
    return t1;
}





Thank you so much for your help, Tobias [Edited by - senseiTU on October 13, 2007 7:31:05 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this