[Help] Quaternion to vector correctly

Problems building or running the engine, queries about how to use features etc.
Post Reply
User avatar
tomneo2004
Greenskin
Posts: 132
Joined: Thu Jun 18, 2009 4:03 pm

[Help] Quaternion to vector correctly

Post by tomneo2004 »

How can i convert quaternion to vector correctly, i have a robot on terrain which control by player, when robot fire a bullet, robot will pass it's own orientation to bullet class, then bullet will set it's own orientation as robot, now the bullet will fly straight in that direction, but i only can get back quaternion not vector, therefore, i can do like this calculation Direction*TimeStep*Movement . I found a code can convert quaternion but, it is not work for me. How can i do?

It use quaternion times a vector but my bullet might face any possible direction not just negative z

Code: Select all

Ogre::Quaternion pQuat=quat;

pQuat=pQuat.Inverse();

Ogre::Vector3 pDirection=pQuat*Ogre::Vector3::NEGATIVE_UNIT_Z;

return pDirection;
Ogre beginner
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 534

Re: [Help] Quaternion to vector correctly

Post by Kojack »

You can't really convert a quaternion to a vector, but you can rotate a vector by a quaternion which is what you want here.

The third line of the code you provided is the one you need. When you multiply a quaternion by a vector, it rotates the vector to point in a new direction. The vector (Negative unit Z in this case) is the direction your robot faces when no rotations have been done (how it's initially loaded). For example, the ogre ninja model faces negative z, so multiplying the ninja's current quaternion by NEGATIVE_UNIT_Z will give you a new vector pointing in the direction the ninja is facing (no matter how you've rotated him). The ogre robot mesh faces along positive x naturally, so for him you'd multiply his quaternion by UNIT_X.

(leave out the pQuat=pQuat.Inverse() line, not sure what that's doing there, it will mess up the direction in this case)
User avatar
tomneo2004
Greenskin
Posts: 132
Joined: Thu Jun 18, 2009 4:03 pm

Re: [Help] Quaternion to vector correctly

Post by tomneo2004 »

Kojack wrote:You can't really convert a quaternion to a vector, but you can rotate a vector by a quaternion which is what you want here.

The third line of the code you provided is the one you need. When you multiply a quaternion by a vector, it rotates the vector to point in a new direction. The vector (Negative unit Z in this case) is the direction your robot faces when no rotations have been done (how it's initially loaded). For example, the ogre ninja model faces negative z, so multiplying the ninja's current quaternion by NEGATIVE_UNIT_Z will give you a new vector pointing in the direction the ninja is facing (no matter how you've rotated him). The ogre robot mesh faces along positive x naturally, so for him you'd multiply his quaternion by UNIT_X.

(leave out the pQuat=pQuat.Inverse() line, not sure what that's doing there, it will mess up the direction in this case)
Thanks for help it works, but do you know what direction is scene node facing when it been created?
Ogre beginner
User avatar
Kaze Youkai
Greenskin
Posts: 108
Joined: Tue Jun 03, 2008 10:47 pm
Location: Austin, Texas
Contact:

Re: [Help] Quaternion to vector correctly

Post by Kaze Youkai »

I think that's just something you have to apply by convention, or by using some sort of custom format which would contain this data. I don't think the OGRE mesh contains information about which direction is the "normal" orientation.
If you think about it, you'll find that there's no reliable way to discern it automatically, and hence has to be encoded by a human.

Unless, of course I'm mistaken, and there is some mesh property about its natural direction or something.
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 534

Re: [Help] Quaternion to vector correctly

Post by Kojack »

It all depends on the direction the mesh is facing when exported from your 3d software (max, maya, etc). Some people like to model with the character facing them, so in ogre the initial direction would be UNIT_Z. Others might rotate the character around to face into NEGATIVE_UNIT_Z.
The only way to tell is to load the model up and look at it.
User avatar
tomneo2004
Greenskin
Posts: 132
Joined: Thu Jun 18, 2009 4:03 pm

Re: [Help] Quaternion to vector correctly

Post by tomneo2004 »

Kojack wrote:It all depends on the direction the mesh is facing when exported from your 3d software (max, maya, etc). Some people like to model with the character facing them, so in ogre the initial direction would be UNIT_Z. Others might rotate the character around to face into NEGATIVE_UNIT_Z.
The only way to tell is to load the model up and look at it.
I am not the artist neither my friend, Both of us are not familiar with 3d software, so the game we are designing use the free resource which get from internet. Yes, i usually load model up and look original direction it face. I think that is only way for me now, Also i have a problem about changing model's direction. I use 3ds max to make model to face negative z then use OgreMax to export the model, but the model not face the direction as i do in the 3d software when i load it up. It face correct direction only when i use MeshMagic(Command line tool) to change it's direction. Why? What's the magic in MeshMagic tool?
Ogre beginner
Post Reply