Quaternion and position calculations.

Get answers to all your basic programming questions. No Ogre questions, please!

Quaternion and position calculations.

Postby grasmanek94 » Tue Dec 20, 2011 9:11 am

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.
grasmanek94
Gnoblar
 
Posts: 5
Kudos: 0
Joined: 20 Dec 2011

Re: Quaternion and position calculations.

Postby areay » Fri Dec 23, 2011 9:17 am

Well start here

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

After you've fully understood everything in that document you'll be sweet.
User avatar
areay
Bugbear
 
Posts: 812
Kudos: 65
Joined: 05 May 2010
Location: Auckland, NZ

Re: Quaternion and position calculations.

Postby grasmanek94 » Fri Dec 23, 2011 5:10 pm

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.
grasmanek94
Gnoblar
 
Posts: 5
Kudos: 0
Joined: 20 Dec 2011

Re: Quaternion and position calculations.

Postby Kojack » Fri Dec 23, 2011 8:27 pm

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. :)
User avatar
Kojack
OGRE Moderator
OGRE Moderator
 
Posts: 7126
Kudos: 516
Joined: 25 Jan 2004
Location: Brisbane, Australia

Re: Quaternion and position calculations.

Postby grasmanek94 » Fri Dec 23, 2011 9:04 pm

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/B ... SE_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
grasmanek94
Gnoblar
 
Posts: 5
Kudos: 0
Joined: 20 Dec 2011

Re: Quaternion and position calculations.

Postby Kojack » Sat Dec 24, 2011 1:24 am

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.
User avatar
Kojack
OGRE Moderator
OGRE Moderator
 
Posts: 7126
Kudos: 516
Joined: 25 Jan 2004
Location: Brisbane, Australia

Re: Quaternion and position calculations.

Postby Sypheria » Sat Dec 24, 2011 7:53 am

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.
Sypheria
Gnoblar
 
Posts: 20
Kudos: 0
Joined: 09 Dec 2011

Re: Quaternion and position calculations.

Postby grasmanek94 » Sat Dec 24, 2011 2:42 pm

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!
grasmanek94
Gnoblar
 
Posts: 5
Kudos: 0
Joined: 20 Dec 2011

Re: Quaternion and position calculations.

Postby Sypheria » Sat Dec 24, 2011 2:45 pm

You can set them; Ogre::Quaternion members are public. You just cannot set them via a set method.
Sypheria
Gnoblar
 
Posts: 20
Kudos: 0
Joined: 09 Dec 2011

Re: Quaternion and position calculations.

Postby grasmanek94 » Sat Dec 24, 2011 4:33 pm

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)
grasmanek94
Gnoblar
 
Posts: 5
Kudos: 0
Joined: 20 Dec 2011


Return to Back to Basics

Who is online

Users browsing this forum: No registered users and 3 guests