Jump to content
  • Advertisement
Sign in to follow this  
AmidaBucu

stucked with quaternions

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 somewhere
Matrix matrix = slerp(camera.getOrientation(), camera.getLastOrientation(), dt).buildMatrix();
glMultMatrix(matrix.data());



However I may be completely off.

Share this post


Link to post
Share on other sites
Quote:
Original post by c_olin
However 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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!