• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Need help with Quaternion and Camera!

## Recommended Posts

hi all, I need a litle help to figure out how i can use quaternion for my camera. Currently i calculated the the rotation with my litle quaternion class. This works fine, but i''ve problems to figure out how i can move the camera in that direction in that i''ve rotate it with the quaternion?! I thought, i''ve to add the rotation to the lookat vector, but i don''t exactly know how i''ve to do this *sigh*. Here is the code snipped that i''ve used without quaternion :

VOID cCamera::Rotate (Float32 X, Float32 Y, Float32 Z)
{
cVector3 kView (m_kLockAt.X - m_kPosition.X, m_kLockAt.Y - m_kPosition.Y, m_kLockAt.Z - m_kPosition.Z);

// here is the quaternion code i''ve tryed but won''t work :(

/*
cVector3 kBlah;
-> But how must i use the rotation for the lookat vector?!
*/
if (X)
{
m_kLockAt.Z = (Float32) (m_kPosition.Z + cMath::FSinDeg(X)*kView.Y + cMath::FCosDeg(X)*kView.Z);
m_kLockAt.Y = (Float32) (m_kPosition.Y + cMath::FCosDeg(X)*kView.Y - cMath::FSinDeg(X)*kView.Z);
}
if (Y)
{
m_kLockAt.Z = (Float32) (m_kPosition.Z + cMath::FSinDeg(Y)*kView.X + cMath::FCosDeg(Y)*kView.Z);
m_kLockAt.X = (Float32) (m_kPosition.X + cMath::FCosDeg(Y)*kView.X - cMath::FSinDeg(Y)*kView.Z);
}
if (Z)
{
m_kLockAt.X = (Float32) (m_kPosition.X + cMath::FSinDeg(Z)*kView.Y + cMath::FCosDeg(Z)*kView.X);
m_kLockAt.Y = (Float32) (m_kPosition.Y + cMath::FCosDeg(Z)*kView.Y - cMath::FSinDeg(Z)*kView.X);
}

I thought the way to do this should be easy, but i don''t found him *g* pls help. Thanks in advance Mfg Impz0r

##### Share on other sites
Here is a formula that might help. It rotates a point ''p'' by a quaternion with scalar part ''s'' and vector part ''v''.

p'' = (s^2)p + v(p dot v) + 2s(v cross p) + v cross (v cross p)

"p dot v" means to take the dot product of p and v.
"v cross p" means to take the cross product of v and p.

This formula gives you the new vector p'' which is ''p'' after it is rotated by the quaternion.

##### Share on other sites
hi,

thanks gtdelarosa, your equation works for me
but i run into the next fault *sigh*

I use your equation to rotate my lookat vector, this works fine, but when i use this code the view looks after a few rotaions like crap *hmpf*

  VOID cCamera::Rotate (Float32 X, Float32 Y, Float32 Z){ cQuaternion kQ (X,Y,Z); // calculate the rotation m_kQuadAxis *= kQ; // i don''t know if that correct, if i don''t do this, the camera looks after an rotation immediately to the origin position..damned, but the thing is that after a few rotaions the scene became sickness *g*, it don''t look correctly..dunno how i could explain how does it look like ;D m_kQuadAxis.RotateVector (m_kLockAt);}

I hope someone could help me out of this crap *g*

Mfg Impz0r

##### Share on other sites
This is what I do in my camera class:

void QCamera::YawPitchRoll(float ya, float pa, float ra)
{
// create a quat from the 3 euler angles
Quaternion tmp_q;
tmp_q.FromEuler(ya,pa,ra);

// extract the scalar s and the vector x,y,z from the quat
float s = tmp_q.get_s();
float x,y,z;
(tmp_q.get_vector()).get_xyz(&x,&y,&z);

// multiply the 3 axes of the camera by x,y,z
Point3D xvec = m_U * x; // m_U is the side vector
Point3D yvec = m_V * y; // m_V is the lookup vector
Point3D zvec = m_N * -z;// m_N is the lookat vector

// add these three vectors together to form one vector
zvec += xvec + yvec;
zvec.get_xyz(&x,&y,&z);

// combine this new vector with the scalar s to create
// a new quat that is in camera space
Quaternion ypr_q(s,x,y,z);

// rotate each of the camera axes with this new quat
m_N = ypr_q.RotatePt(m_N);
m_U = ypr_q.RotatePt(m_U);
m_V = ypr_q.RotatePt(m_V);
}

The function RotatePt is just the formula I gave you before.

##### Share on other sites
hi,

thanks for the fast reply, but i couldn''t bring it to work, i don''t know where exactly the error lay, but after a few rotation all vectors aka lookat, up, right , have the value of -1.#IND0 i dunno what i did wrong *damnit*. But this is only when is move the camera out of the world origin (0.0.0).

Any idea?!

Mfg Impz0r

##### Share on other sites
quote:
Original post by Impz0r
hi,

thanks for the fast reply, but i couldn''t bring it to work, i don''t know where exactly the error lay, but after a few rotation all vectors aka lookat, up, right , have the value of -1.#IND0 i dunno what i did wrong *damnit*. But this is only when is move the camera out of the world origin (0.0.0).

Any idea?!

Mfg Impz0r

You may already know but -1.#IND0 means it''s undefined. Trig functions are undefined at certain angles...I''m not sure how to fix that though

A CRPG in development...

Need help? Well, go FAQ yourself.

##### Share on other sites
hi,

damn i hang with the camera, can''t figure out whats wrong
So i would ask if someone known a good tutorial about a camera with a use of quaternion?!

I would be very happy if you can post me a link or source..whatever

Mfg Impz0r

• ### Forum Statistics

• Total Topics
627701
• Total Posts
2978711

• 21
• 14
• 12
• 10
• 12