Jump to content


- - - - -

A few questions on using quaternions


4 replies to this topic

#1 karligula

    Valued Member

  • Members
  • PipPipPip
  • 180 posts

Posted 08 February 2006 - 06:19 PM

Hi folks,

I'm using quaternions to define the orientations for all the entities in my game engine, and I'm wondering how I can restrict a player's orientation to prevent them from pitching too far upwards or downwards and looking behind themselves upside down? Bearing in mind that this single quaternion also encodes their yaw and roll angles?

Also, how can I calculate a roll rotation to roll the player back to keep them vertical? I've noticed a few times when it's rolled slightly, which I think is due to accumulated maths errors.

Thanks everyone!

#2 Reedbeta

    DevMaster Staff

  • Administrators
  • 4974 posts
  • LocationBellevue, WA

Posted 08 February 2006 - 06:26 PM

If you're looking for an FPS-style camera, then I find it's easier to simply keep yaw and pitch in variables and compute the proper view matrix each frame. The keyboard and mouse controls can very easily be mapped to yaw and pitch variables, and of course pitch can be easily clamped to ±90° as you desire. Quats for everything else though :unsure:
reedbeta.com - developer blog, OpenGL demos, and other projects

#3 karligula

    Valued Member

  • Members
  • PipPipPip
  • 180 posts

Posted 20 February 2006 - 07:38 PM

Thanks reedbeta, that is definitely an option, but I was hoping that one of you maths wizards might be able to suggest a way to restrict quaternions to certain orientations... I think this would be useful for physics systems like rag dolls or that sort of thing... maybe ;-)

#4 Wernaeh

    Senior Member

  • Members
  • PipPipPipPip
  • 368 posts

Posted 07 March 2006 - 08:12 AM

There is a solution to this problem just using quaternions.

Basically, for this you need to build a matrix for the old and the new orientations, and extract direction vectors from these. Whenever the resulting coordinate system has the y axis with a different sign than the previous coordinate system, the movement was invalid (i.e. was rotating over the singularity). If this happens, project everything to the x-z plane, and find out the rotation angle of the projected systems on that plane. Apply a new rotation about the y axis with this angle to the old quaternion and use that as the new rotation instead.

This is, however, ridiculusly inefficent when compared to the yaw / pitch based, clamping approach.

Cheers,
- Wernaeh

#5 karligula

    Valued Member

  • Members
  • PipPipPip
  • 180 posts

Posted 10 March 2006 - 08:38 PM

I was just about to suggest that myself wernaeh... shame I never thought of it ;-)

I just thought there might be some sneaky way of peeking at the quaternion values and limiting them somehow, to stop them pointing in certain directions...

Looks like euler angles are the way to go ;-)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users