[Solved] Help for rotating a SceneNode via Quaternion

Problems building or running the engine, queries about how to use features etc.
Post Reply
iskren
Gnoblar
Posts: 4
Joined: Sat Apr 16, 2016 2:24 pm

[Solved] Help for rotating a SceneNode via Quaternion

Post by iskren »

Howdy everyone!

I have a SceneNode that goes forward when 'W' is pressed (by adding a Vector3(0,0,-1) to the current position).
I'm trying to rotate the SceneNode based on mouse.X.rel in order to change where the SceneNode's Z Axis is pointed at, but so far I managed to 'turn my head' and the scene node keeps moving at the original direction.

Here's the relevant pieces of code:

In MainApp::go()

Code: Select all

    p = scene->createSceneNode("p");
    p->setPosition(Ogre::Vector3::ZERO);
    p->setOrientation(Ogre::Quaternion::IDENTITY);
    p->attachObject(camera);
In MainApp::frameRenderingQueued()

Code: Select all

        Ogre::Vector3 direction = p->getOrientation() * Ogre::Vector3::NEGATIVE_UNIT_Z;
        direction.y = 0;
        direction.normalise();
        // ms below is a MouseState grabbed a few lines above
        Ogre::Quaternion rotate = Ogre::Quaternion(-Ogre::Degree(ms.X.rel*0.1), Ogre::Vector3::UNIT_Y);
        Ogre::Vector3 newDirection = rotate * direction;
        Ogre::Quaternion newOrientation = Ogre::Vector3::NEGATIVE_UNIT_Z.getRotationTo(newDirection);
        p->setOrientation(newOrientation);
Moving the mouse across X rotates the camera but going 'forward' moves towards the original direction, as if the Z axis was never rotated. Please help me understand where my error is!
I'm using Ogre 1.9.0.

Thank you!
Last edited by iskren on Mon Apr 18, 2016 7:17 am, edited 1 time in total.
Guardian121
Gnoblar
Posts: 9
Joined: Sun Nov 29, 2009 1:24 am
x 1

Re: Help for rotating a SceneNode via Quaternion

Post by Guardian121 »

If you add Vector3(0,0,-1) to the current position, it will only move Vector3(0,0,-1) no matter the nodes rotation.
You could try

p->translate(Vector3(0,0,-1),Ogre::Node::TransformSpace::TS_LOCAL);

or

p->setPosition(p->getPosition()+newOrientation*Vector3(0,0,-1));
xrgo
OGRE Expert User
OGRE Expert User
Posts: 1148
Joined: Sat Jul 06, 2013 10:59 pm
Location: Chile
x 168

Re: Help for rotating a SceneNode via Quaternion

Post by xrgo »

it looks like you're trying to implement WASD camera controls
Image
Ogre 2.1 samples has one simple implementation:
https://bitbucket.org/sinbad/ogre/src/d ... ew-default
iskren
Gnoblar
Posts: 4
Joined: Sat Apr 16, 2016 2:24 pm

Re: Help for rotating a SceneNode via Quaternion

Post by iskren »

Hello xrgo and Guardian121,
thank you both for the help!

I was trying to figure out Quaternions more than anything really, by using them in a simple scenario to get a better understanding more than anything.
So the rotation code actually worked, which is awesome and now using translate with TS_LOCAL actually moves me forward!
Does this mean that when adding a vector to a node's position, we change the coordinates based on the world axes, rather than the local ones?
Now that I've typed it, it only seems natural :)

I looked at the link for the camera control. It seems to me it's using pitch and yaw to rotate, rather than Quaternions, but I read an external article(trying to find the link) that Quaternions are more optimized and a better approach ?

Thank you once more, now I have a very rough first person camera and I can walk around a scene freely!
xrgo
OGRE Expert User
OGRE Expert User
Posts: 1148
Joined: Sat Jul 06, 2013 10:59 pm
Location: Chile
x 168

Re: [Solved] Help for rotating a SceneNode via Quaternion

Post by xrgo »

Quaternions are hard to understand
I recommend you to watch this next 2-3 videos: https://www.youtube.com/watch?v=eaSrOS2 ... C41F1AFBAB
iskren
Gnoblar
Posts: 4
Joined: Sat Apr 16, 2016 2:24 pm

Re: [Solved] Help for rotating a SceneNode via Quaternion

Post by iskren »

Very helpful indeed! I also found this quite easy to understand as well: https://www.youtube.com/watch?v=SCbpxiCN0U0
Post Reply