Set tranformation matrix to scene node?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
UncleBob
Posts: 106
Joined: Fri May 01, 2009 8:46 am

Set tranformation matrix to scene node?

Post by UncleBob »

Hello,

I'm a bit stuck with a little problem. I have a SceneNode, and under certain circumstances, another SceneNode has to dock with that node. That means it has to be brought into an appropriate position facing the other Node. I planned to do this in three steps:

-Get the global transformation matrix from the node to dock with
-manipulate that matrix to invert the rotation and translate it
-apply the rotation matrix to the node that has to dock

Seems simple enough, but I cannot find a way to accomplish step 3. There seems to be no ISceneNode::setAbsoluteTransformation method, or even one to set the relative transformation, or any kind of method that would take a matrix4 as an input. This has me somewhat confused... How am I supposed to manipulate the transformation of a SceneNode if not via a transformation matrix? Am I just gloriously missing a method in the documentation here?
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Set tranformation matrix to scene node?

Post by CuteAlien »

Irrlicht doesn't work with transformation matrices internally but with vectors for position, rotation and scale. Well, there is the IDummyTransformationSceneNode which works with a matrix, but I won't recommend that unless you have to (adding an additional node per node is a bad idea performance wise). I suspect the reason there is no setTransformation function is that it would be a little ambiguous what it would do. As there is more than one way to transform a matrix into those vectors. And so if you'd call setTransformation followed by getTransformation you can get different values (same transformation - just for example scale and rotation now mixed up in a different way). But if that's fine you can get all those values from the matrix itself. CMatrix4 has getter functions for all those vectors.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
UncleBob
Posts: 106
Joined: Fri May 01, 2009 8:46 am

Re: Set tranformation matrix to scene node?

Post by UncleBob »

Hmmm yes, that seems to work. Thanks a lot!
UncleBob
Posts: 106
Joined: Fri May 01, 2009 8:46 am

Re: Set tranformation matrix to scene node?

Post by UncleBob »

This isn't exactly related to the original question, but not worth its own thread, so I'm putting it in here while this one's still fresh:

What's the default alignement of an ISceneNode after creation (a scenenode with a rotation of (0,0,0))? I assumed it's facing Z+ and has the up-direction along Y+, but I'm getting inconsistent results when testing that assumption...
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Set tranformation matrix to scene node?

Post by CuteAlien »

The problem with that assumption is - what does alignment mean? Let's say you have a sphere or a box ... then there is no front. The direction of a model is basically defined by the way you model it - the side where you model has a nose is the front. Unless you add a rotation or translation to your model the values in the scene for each vertex are exactly the same values each vertex has in the model file. Meaning if the nose of your model is for example at 0,10,2 then that's the exact position it has in the scene after loading. If you put the camera now at 0,10,20 and look to the nose you will see the model from the front. If you put the camera at 0,10,-20 and look at the nose you will see the model from the back.

Similar the question about "up" is a little bit tricky. This also depends on your camera. Default Irrlicht camera has indeed Y as up.

What you can say is that Irrlicht has a left-handed coordinate system. So when X is right and is up then Z increases by depth.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
UncleBob
Posts: 106
Joined: Fri May 01, 2009 8:46 am

Re: Set tranformation matrix to scene node?

Post by UncleBob »

Hmmmyeah, I realised myself after thinking about it that the question was a bit strange and that a normal scene node doesn't really need an alignement... Except for mine, because they're used to define orientations. So let's ask a bit differently: What's the default orientation of a CameraSceneNode? because I'm currently using matrix4::buildCameraLookAtMatrixLH to work with directional and rotational vectors, as it's essentially the same thing even if it isn't a camera.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Set tranformation matrix to scene node?

Post by CuteAlien »

Default parameters for addCameraSceneNode are position=vector3df(0, 0, 0) and lookat=vector3df(0, 0, 100). And the up-vector is 0,1,0 (so y is up). But you can certainly change all of those easily.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
UncleBob
Posts: 106
Joined: Fri May 01, 2009 8:46 am

Re: Set tranformation matrix to scene node?

Post by UncleBob »

Thanks!

In that case, there must be some fundamental misconception in how I'm applying my matrices... here's what I'm trying to do:
I have different points on a scenenode that have a facing direction and an up vector. I use matrix.buildCameraLookAtMatrixLH to build a transformation matrix for them relative to their parent. The goal is to use them to align other nodes with it. But my results are very inconsistent with what I expect them to be.

the process goes like this:

Code: Select all

 
core::matrix4 matrix;
matrix.buildCameraLookAtMatrixLH(core::vector3df(0, 0, 0),ports[i].facing, ports[i].up);                           
 
(ports.facing and ports.up are normalised directional vectors.)
Now, I would expect that if I rotate a vector that has the same alignement as a default camera by this matrix, it would point in the same direction as these vectors are pointing. But that's not the case. Here's a little test I set up:

Code: Select all

 
        core::vector3df bugmeDir = core::vector3df(0,0,1);
        core::vector3df bugmeRot = core::vector3df(0, 1, 0);
        matrix.rotateVect(bugmeDir);
        matrix.rotateVect(bugmeRot);
 
I would expect these vectors to match the facing and up direction of the port the matrix was calculated from. But they don't (well , they do under special circumstances, but that's no use...). So, am I understanding the whole point of matrix::buildCameraLookAtMatrixLH completely wrong?
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Set tranformation matrix to scene node?

Post by hendu »

You're misunderstanding the camera matrix. It transforms world space to camera space, while your code snippet asks "where does this +Z normal point in camera space if my camera is looking at some other direction". Ie, the inverse of what you want, try using the inverse matrix.

The inverse cam matrix would tell you what +Z in cam space (forward) is in world space.
UncleBob
Posts: 106
Joined: Fri May 01, 2009 8:46 am

Re: Set tranformation matrix to scene node?

Post by UncleBob »

And suddenly, things start falling into place! :lol:

Thanks a lot!
Post Reply