New To Mogre - Newb Question Re Node.Orientation

Maxxra

05-04-2012 16:57:26

I'm trying to do something relatively simple: figure out the orientation of a mesh as a vector, normalize the direction vector, add it to the current position of the mesh.

In other words, I want this mesh to "go forward".

I'm running into some problems - not the least of which is the realization that I need to read up on quaternions.

Quaternion q = shipNode.Orientation;

Is there an optimized way to convert q into a vector 3? Also, from my understanding Quaternions are defined as an axis and a rotation (w,x,y,z) - why would the orientation return a rotation? Is W=0 and the x,y,z the orientation?

Secondly, the vector3.Normalise() function returns a float... shouldnt it return a unit vector3? Am I looking at the wrong function?

I appreciate any light someone can shed on this - last couple of nights have been spent in frustration.

Thanks in advance,
Max

tafkag

05-04-2012 17:42:26

Secondly, the vector3.Normalise() function returns a float... shouldnt it return a unit vector3?

Normalise() normalises the vector and returns its length before normalization.


Vector3 test = new Vector3(2, 0, 0);
float length = test.Normalise();

// -> length = 2
// -> test = (1, 0, 0)

Maxxra

05-04-2012 18:45:31

Thank you for the quick reply tafkag!

It looks like vector3.NormalisedCopy() returns the Vector3 normalized as I needed.

zarfius

06-04-2012 05:21:19

I'm trying to do something relatively simple: figure out the orientation of a mesh as a vector, normalize the direction vector, add it to the current position of the mesh.
In other words, I want this mesh to "go forward".

It's actually pretty easy once you know how. The first problem to consider is what direction the mesh is facing by default. In other words, when the Orientation is Quaternion.IDENTITY what Vector3 does that represent. You need to know this up front and it depends on what direction the mesh was facing when you exported it from your favorite modelling program ;)

Okay, so lets say you have your mesh facing down the Z axis by default. You attach it to a scene node like so.

Entity entity = sceneManager.CreateEntity("Maxxra.mesh");
SceneNode sceneNode = sceneManager.RootSceneNode.CreateChildSceneNode();
sceneNode.AttachObject(entity);

Now, because you know that your mesh is facing down the Z axis by default all you need to do is rotate your scene node to face the direction you want and multiply the resulting orientation by the UNIT_Z vector to get your new direction.

// rotate the scene node to face the new direction
sceneNode.Rotate(Vector3.UNIT_Y, new Radian(new Degree(90)));

// now move the scene node in the new direction
float speed = 5.0f;
float deltaTime = 0.0166f; // time since last frame
Vector3 direction = sceneNode.Orientation * Vector3.UNIT_Z;
sceneNode.Position += direction.NormalisedCopy * deltaTime * speed;


Disclaimer: I wrote this code from the top of my head so it hasn't been tested ;)

Good luck.

Maxxra

06-04-2012 19:35:39

Thank you zarfius! I'll play with that and post some results back here.

Beauty

10-04-2012 21:28:08

Hi Maxxra,
welcome to our Mogre sub-community. :D

The questions are already answered. (Thanks to Zarifus and tafkag!)

For more quaternion look to this wiki page:
http://www.ogre3d.org/tikiwiki/Quaterni ... ion+Primer

Perhaps also the "Euler Angle Class" is useful for you:
http://www.ogre3d.org/tikiwiki/Euler+Angle+Class+Mogre