Public Group

# stucked with quaternions

This topic is 2902 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi i have tried to write a camera based on quaternion, but it's acting weirdly :S

class camera{private:    vector view;    vector up;    vector pos;public:    inline camera()    {    }    inline camera(vector in_dir, vector in_pos, vector in_up)    {        view = in_dir;        up = in_up;        pos = in_pos;    }    inline void set(vector in_dir, vector in_pos, vector in_up)    {        view = in_dir;        up = in_up;        pos = in_pos;    }    void rotateX(double in_rad)    {        vector xAxis = view;        xAxis.cross(up);        quaternion tmp(xAxis*sin(in_rad/2), cos(in_rad/2));        quaternion viewQ(view,0);        quaternion newViewQ = tmp*viewQ*tmp.conjugate();        view = newViewQ.getVector();    }    void rotateY(double in_rad)    {        quaternion tmp(up*sin(in_rad/2), cos(in_rad/2));        quaternion viewQ(view,0);        quaternion newViewQ = tmp*viewQ*tmp.conjugate();        view = newViewQ.getVector();    }    void setCamera()    {        vector look = view+pos;        gluLookAt(pos.getX(), pos.getY(), pos.getZ(),                  look.getX(),look.getY(),look.getZ(),                  up.getX(),up.getY(),up.getZ());    }

Where am i wrong?

##### Share on other sites
I don't think there's enough information there to say for sure what's wrong. You didn't specify what it's doing wrong (you just said it's 'acting weirdly', which could mean any number of things), and there are quite a few crucial bits of code missing, such as what the initial values for the camera are, and what the OpenGL state is when setCamera() is called.

I'll also add that the use of quaternions here is unnecessary and doesn't really offer any benefits. Once you've gotten things working the way you want, I'd suggest dropping the quaternions and manipulating the camera's orientation through more straightforward means, such as a pair of Euler angles. (This is assuming that you're after typical 'FPS-style' motion, which appears to be the case based on what you posted.)

##### Share on other sites
Okay, problem solved anyway :D
But I think, You are right, so quaternions are not neccessary for 4 Me 4 now.

##### Share on other sites
Someone who has more experience with this can correct me if I am wrong, but I think that the main advantage to using a quaternion for a camera is to store the camera's orientation in a quaternion (not origin, target, and up vectors). This way you can use spherical linear interpolation to smooth the camera's movement.

psuedo (extremely simplified, ignoring details):

class Camera {    private:        Quaternion orientation;    public:        void lookAt(const Vector& origin, const Vector& target, const Vector& up) {            // build a quaternion based on these values        }        Matrix buildMatrix() {            // build a matrix from the quaternion for opengl (no need for gluLookAt        }};// in the renderer somewhereMatrix matrix = slerp(camera.getOrientation(), camera.getLastOrientation(), dt).buildMatrix();glMultMatrix(matrix.data());

However I may be completely off.

##### Share on other sites
Quote:
 Original post by c_olinHowever I may be completely off.
No, you're not. SLERP can be done with other representations (e.g. matrices) as well, but the quaternion version of SLERP is more efficient and (arguably) more elegant.

Of course, the way the OP was using quaternions (and the way they appear to be used in a few tutorials that are floating around online) was simply as a means of rotating the basis vectors of the camera. For this, they offer no measurable benefit (IMO), and in any case, updating the orientation by rotating individual basis vectors probably isn't the best way to go about it (again, IMO).

Finally, for a simple FPS-style camera with no roll, the camera can be smoothly interpolated using more simple methods, so I'd still say that representing the camera orientation using a quaternion isn't particularly necessary. For other types of camera though (e.g. 6DOF with autoleveling), a quaternion would probably be a good choice.

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 14
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631434
• Total Posts
3000051
×