Orientation of SceneNode
I am trying to move and rotate a SceneNode by the use of the keyboard, but it seems as if the orientation gets screwed when the node's yaw ends up between 180 and 359 degrees.
I have my SceneNode (with a ninja mesh). When I press W, it moves forward and S moves it backwards. I rotate it using A and D. As long as do not rotate over 180 degrees, I move in the correct directions, but as soon as I rotate more than 180 degrees, I do not move in what would have been the right direction, I keep moving in the previous direction.
To clarify the last part:
Ninja faces up +Z, rotation is 0 degrees. W moves up +Z axis.
Ninja faces down -Z, rotation is 180 degrees. W moves up +Z axis, and NOT down -Z axis as it should.
Here is the code:
public void Move(float fSpeed)
float rot = m_refSceneNode.Orientation.Yaw.ValueRadians + (float)System.Math.PI / 2.0f;
Vector3 vec = new Vector3();
vec.x = m_refSceneNode.Position.x + (float)System.Math.Cos(rot) * fSpeed;
vec.y = m_refSceneNode.Position.y;
vec.z = m_refSceneNode.Position.z - (float)System.Math.Sin(rot) * fSpeed;
m_refSceneNode.Position = vec;
public void Rotate(float fSpeed)
Btw.. the compensation of +90 degrees in the Move function is due to the fact that the ninja faces along +Z and not +X in it's initial position, so that screws up the math a bit if I don't compensate.
No one got any ideas? I've tried some different approaches, but none yielded any result. How do you move your stuff around in your applications??
Vector3 moveScale = new Vector3(0, 0, -10); //to move the ninja forward 10 units
But I'd suggest that you get the ninja to face to the right direction, by creating a child SceneNode for the main ninja node, turn the child node appropriately and attach the ninja to it. Then you handle the position of the main ninja node.
Vector3 moveScale = new Vector3(10, 0, 0); //move forward 10 units
Another node? I am not sure I understood what you meant there Bekas, excuse my slow brain
Something like this:
Entity ninja = sceneMgr.CreateEntity("ninja", "ninja.mesh");
// Main node for ninja
SceneNode ninjaNode = sceneMgr.RootSceneNode.CreateChildSceneNode();
// child node to turn ninja facing the right direction
SceneNode sn = ninjaNode.CreateChildSceneNode();
// child node will be turned in relation with its parent
I figured it out, it worked by taking ninjaNode.Orientation * new Vector3(0, 0, -1)
However, I have another problem that is more trickier to solve.. I need to be able to position my 3rd-person camera so it stays behind the ninja at all times. So far, I have not been able to find any solution. The above multiplication does not help in this matter.
Child SceneNode to the rescue:
// CameraNode attached to the ninja node
SceneNode cameraNode = ninjaNode.CreateChildSceneNode(10, 10, 10); //at some position relative to the ninja
Yes, but will that really put it behind the ninja at all times??
Edit: doh!! I am such a noob at times.. of course it does... THANK YOU!!!
Yep, when you move/turn the ninjaNode, cameraNode will follow.
When you build a hierarchy of scene nodes, when the parent node moves, its child nodes go with it.
I've reinstated this thread because there is one more problem with rotation that I need to resolve...
If I do this:
float yaw = myNode.Orientation.Yaw.ValueDegrees;
I get the yaw value in degrees. To my horror... I discovered that the actual value is pretty weird.. it goes from 0 to 90 and then BACK to 0 again.. even though I have turned 180 degrees!!! And turning the other way it goes from 0 to -90 and down to 0 again...
How do I get the real rotation? I mean.. between 0 and 360 degrees.