Quaternion and position calculations.

Get answers to all your basic programming questions. No Ogre questions, please!
Post Reply
grasmanek94
Gnoblar
Posts: 5
Joined: Tue Dec 20, 2011 9:03 am

Quaternion and position calculations.

Post by grasmanek94 »

Hey everybody,

Recently I was working on some code to measure the G-forces of an airplane in a game called GTA:San Andreas.
There is a function to get the object position and get it's quaternions (position: X,Y,Z variables, quaternion: qW,qX,qY,qZ variables)

I have some problems calculating the needed stuff with the quaternions, I would like to determine if the object is upside down and if it's pointing (more) up or(/than) down.

Could anyone explain me how to achieve this? Thanks in advance.
User avatar
areay
Bugbear
Posts: 819
Joined: Wed May 05, 2010 4:59 am
Location: Auckland, NZ
x 69

Re: Quaternion and position calculations.

Post by areay »

Well start here

http://www.ogre3d.org/tikiwiki/Quaterni ... ion+Primer

After you've fully understood everything in that document you'll be sweet.
grasmanek94
Gnoblar
Posts: 5
Joined: Tue Dec 20, 2011 9:03 am

Re: Quaternion and position calculations.

Post by grasmanek94 »

Wish my English was good enough to understand that, Man I think I really need to go to some university here at The Netherlands... but to which one lol? My whole school doesn't even know WHAT quaternions are OMG! that's embarassing.

However thanks for the links, what I did understand - I did learn. I learned a bit from it ;)

I will try to look for some video tut's , to see the quats in action, in the game I have it's impossible for me to visualize quaternions around airplanes because of the lack of functions for it.


if there only was a standalone C++/C source for quaternion calucations, I don't really want to extract it from an whole engine as I'm just a hobby programmer.
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 534

Re: Quaternion and position calculations.

Post by Kojack »

if there only was a standalone C++/C source for quaternion calucations, I don't really want to extract it from an whole engine as I'm just a hobby programmer.
Ogre's quaternion code is all in ogrequaternion.h and ogrequaternion.cpp. It's fairly stand alone.

For your problem, quaternions alone are hard to test for things like upside down objects. But luckily we can convert any local axis vector to a world axis vector using a quaternion.
The operation is world = orientation * local;
This rotates the local vector by the orientation.

How you could use this is as follows. I'm not sure what the coordinate system in GTA:SA is, but we'll assume it's the same as ogre ( [0,0,-1] faces forwards, [0,1,0] is the up vector).
Now if you do: world = orientation * Vector3(0,1,0);
it will rotate the local up vector of the air plane by it's orientation to get the current world vector. If world.y is greater than 0, the plane is upright. If world.y is less than zero, the plane is upside down (the closer to -1 world.y is, the closer to completely upside down it is). If you do acos(world.y), it will tell you the angle away from perfectly upright and level.

You can also do forward = orientation * Vector3(0,0,-1) to get the current heading of the plane as a vector.

Of course GTA:SA probably uses something else like [0,0,1] as the up vector and [0,1,0] as forwards, but this is an ogre forum so my examples above are for ogre's style. :)
grasmanek94
Gnoblar
Posts: 5
Joined: Tue Dec 20, 2011 9:03 am

Re: Quaternion and position calculations.

Post by grasmanek94 »

wow thank you for the great explaination , I assume qW is the 'world' you are talking about.

But, "pretty standalone" :/

Code: Select all

#include "OgreStableHeaders.h"
// NOTE THAT THIS FILE IS BASED ON MATERIAL FROM:

// Geometric Tools, LLC
// Copyright (c) 1998-2010
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
// http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt

#include "OgreQuaternion.h"

#include "OgreMath.h"
#include "OgreMatrix3.h"
#include "OgreVector3.h"
that already leads to 100+ includes. especialy OgreStableHeaders. is it safe to delete it?

Edit:
Em, can you list all things that can be removed safely? XD
I don't want to be too lazy but also not spent hours on it if it's 'easy' for you :)

Edit 2:
Do you think this is what I'm looking for?:
http://willperone.net/Code/quaternion.php <- however if possible I prefer to use the Ogre3D one, the game MotorM4X is really a great game so I would like to have the quat functions from the Ogre source ;P And this forum is more familiar with it's own functions so when I have any qiestions it will be easier to communicate ;D and ofcourse you already explained a bit how to use it.. But just the cut-out problem :x
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 534

Re: Quaternion and position calculations.

Post by Kojack »

You might want to take a look at Wild Magic's math code: http://www.geometrictools.com/LibMathem ... gebra.html
It's actually the original source for some of ogre's math code. It might be easier to isolate.

The quaternion's w is unrelated to the world vector. W is actually the cosine of half the angle of rotation. It's a bit freaky. If you don't know what the w,x,y,z in a quaternion are really doing, it's best to avoid messing with them. There's some very strict rules on what they can contain, even a slightly wrong value will make them break objects.
Sypheria
Gnoblar
Posts: 20
Joined: Fri Dec 09, 2011 1:49 pm

Re: Quaternion and position calculations.

Post by Sypheria »

If your W value is incorrect, it will force an assert in the DLL if you attempt to use it with something like a nodes roll / pitch / yaw.
grasmanek94
Gnoblar
Posts: 5
Joined: Tue Dec 20, 2011 9:03 am

Re: Quaternion and position calculations.

Post by grasmanek94 »

I can only GET values, there is no possibility yet for SETting them :P
I will try to isolate that code, thanks!
Merry Christmas and Happy New Year!
Sypheria
Gnoblar
Posts: 20
Joined: Fri Dec 09, 2011 1:49 pm

Re: Quaternion and position calculations.

Post by Sypheria »

You can set them; Ogre::Quaternion members are public. You just cannot set them via a set method.
grasmanek94
Gnoblar
Posts: 5
Joined: Tue Dec 20, 2011 9:03 am

Re: Quaternion and position calculations.

Post by grasmanek94 »

Sypheria wrote:You can set them; Ogre::Quaternion members are public. You just cannot set them via a set method.
oh that way, I thought you ment the objects...
http://wiki.sa-mp.com/wiki/GetVehicleRotationQuat
There is no SetVehicleRotationQuat (however I've requested it - for a idea I had - Fly my airplane and apply the quats to another to copy it's movements with Velocity along)
Post Reply