[OK] Problem with matrix

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
stefbuet
Competition winner
Posts: 495
Joined: Sun Dec 09, 2007 4:13 pm
Location: france

[OK] Problem with matrix

Post by stefbuet »

Hi,

I don't understand : when I set a rotation to a matrix, then, I'm not able to get the angle back, it's not the same!

Code: Select all

core::matrix4 m;
    m.setRotationDegrees(core::vector3df(0,140,0));
    std::cout<<"debug : "<<m.getRotationDegrees().Y<<std::endl;
Output :
debut : 40

With other rotation angles, here is what I get:
set rot ---- get rot
10 --------- 10
90 --------- 90
180 ------- 5*10^-6
250 ------- 290
360 ------- 1*10^-5

Err?
Last edited by stefbuet on Tue Mar 09, 2010 7:02 pm, edited 1 time in total.
DavidJE13
Posts: 165
Joined: Tue Jan 09, 2007 7:17 pm

Post by DavidJE13 »

have you looked at the other angles (x,z) it generates? Euler angles aren't great at defining a rotation; different values can give the same result. For example, the (0,180,0) could be done with (180,0,180) too. Also they will be looped within [0,360), hence the 360->~0.
For the 250 and 140 angles I can't say with certainty that this is what's happening, but it's very likely. You should store the values yourself if you need to use them later, or use quaternions for more control.
stefbuet
Competition winner
Posts: 495
Joined: Sun Dec 09, 2007 4:13 pm
Location: france

Post by stefbuet »

Well, I just want to calculate a node position.

I got it's parent position/rotation.
And I got the child position/rotation.

Without adding the child to the parent, I want to be able to calculate the child position/rotation as if it were a real child of the parent node.

So basicaly what I did is creating 2 matrices, first one for the parent in which I used setTranslation and setRotationDegrees functions, and second one for the child with translation/rotation informations added too.

Once matrices are filled by translation/rotation data, to get the resulting child position, I was just multiplying matrices like that :

newPosition=(parentMatrix*childMatrix).getTranslation();
newRotation=(parentMatrix*childMatrix).getRotationDegrees();

But as you see, my proper way is not really working.
How could I use matrices to do that? Is there a really need or quaternion?
Or the only way is to do it myself with cos/sin attacks ? :p

Thanks.
ent1ty
Competition winner
Posts: 1106
Joined: Sun Nov 08, 2009 11:09 am

Post by ent1ty »

for position, you can use getAbsolutePosition()
(if this is what you want)
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

You may look in to ISceneNode code. That kind of stuff should be done somewhere inside, but it is probably something like this:

Code: Select all

core::matrix4 m,n;

parent->updateAbsolutePosition();
m = parent->getAbsoluteTransformation();

n = child->getTransformation();

core::vector3df pos = child->getPosition();

m.translateVect(pos);
m.rotateVect(pos);

child->setPosition(pos);

n *= m;

child->setRotation(n.getRotationDegrees());
I don't know what scale would do to that code so be careful.
stefbuet
Competition winner
Posts: 495
Joined: Sun Dec 09, 2007 4:13 pm
Location: france

Post by stefbuet »

Well, I looked into sources...
It was like I was doing, and it's using also getRotationDegrees to get absolute rotation :o

Instead, I used childRot=nodeRot+parentRot and it's working.

Here is my code:

Code: Select all

void MyObject::updatePosition() { //simulate a parent effect without being a child of it!! (for Irrnewt...)
    
    core::matrix4 parentMatrix, childMatrix, absoluteMatrix;
    
    parentMatrix.setRotationDegrees(parentNode->getRotation());
    parentMatrix.setTranslation(parentNode->getPosition());
    
    childMatrix.setTranslation(nodePosition);
    
    absoluteMatrix=parentMatrix*childMatrix;
    
    
    node->setPosition(absoluteMatrix.getTranslation());
    node->setRotation(nodeRotation+parentNode->getRotation());
   
    
}
Thanks for having helped.
Post Reply