XNA Creators Club Online
Page 1 of 1 (4 items)
Sort Posts: Previous Next

Quaternions?

Last post 05-07-2008 8:32 PM by Ilidd. 3 replies.
  • 04-16-2008 8:42 AM

    Quaternions?

    I am writing a simple object viewer. I need to be able to pivot about the camera target to create the effect of moving around an object in 3 dimensions. I have achieved this with simple trigonometry and spherical coordinates to change the camera position (keeping the camera target the same). This works fine in isolation but when I apply new camera coordinates in conjunction with any view transformation matrices (such as translate), I am getting unexpected camera views - things in the wrong place and suchlike. Rather than working out the new camera coordinates 'longhand' as I am now, would a preferable solution be to use quaternions to calculate the new camera position? Will this eliminate these unexpected results because all the transforms are being handled by one matrix?

    Any advice would be welcome.

    Thanks in advance.

    If it looks like crap,
    and smells like crap,
    and tastes like crap,
    It's probably crap.
  • 04-16-2008 11:18 AM In reply to

    Re: Quaternions?

    The unexpected results are probably because you are not applying the math right. If you apply the math wrong with matrices, it'll be as wrong as with quaternions.

    In general, for the kind of operation you want, figure out what the order of operations is. Then multiply the matrices in question from left to right to apply the order.

    First, you want to rotate the camera around the up axis for yaw.
    Then you want to move the camera back (in world space) for distance.
    Then you want to rotate the camera around the X axis (in world space) for elevation.

    Now, "moving the camera" means "moving the vertices the other way." Hence, this is a reasonable approximation of what you want:

    Matrix View =
      Matrix.CreateRotationY(-camYaw) *
      Matrix.CreateTranslation(0, 0, -camPos) *
      Matrix.CreateRotationX(-camElevation);


    If you're currently trying to do this using quaternions, you should know that in XNA, quaternions multiply right-to-left, whereas matrices and vertices multiply left-to-right. Very confusing.

    Jon Watte, Direct3D MVP kW X-port 3ds Max .X exporter kW Animation source code
  • 04-16-2008 11:52 AM In reply to

    Re: Quaternions?

    Thank you for the reply.

    I'm not currently using quaternions, in fact I'm not entirely sure how to put them into practice to achieve what I want, or even if they are suitable. I am only figuring the maths out 'longhand' in the absence of any other method that I know and thought that maybe quaternions maybe the answer? Would you say that they are suited to this kind of calculation, ie The camera traveling around an imaginary sphere with a radius equivalent to the camera view distance and always looking at the camera target?

    If it looks like crap,
    and smells like crap,
    and tastes like crap,
    It's probably crap.
  • 05-07-2008 8:32 PM In reply to

    Re: Quaternions?

    Answer

    Short answer: Yes, quaternions will very easily do what you are trying to do. Riemer Grootjans wrote an article on using quaternions for exactly this use. You can find the article here:

    http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series2/Quaternions.php

    I think you'll find this solution to be a lot simpler than what you are doing currently.

    Best Regards,

    - I

Page 1 of 1 (4 items) Previous Next