Re: [osg-users] 【NEWBIE】Question about osg::MatrixTransform::getMatrix().getTrans() and getRotate()
Hi Jiechang, I am not sure I am able to pinpoint your problem. I see some weak spots but I am not sure if those are the true causes of your problem. And don't want to give wrong clues. Can you write short repro program which demos your problem ? I may then fix it and send you back. To learn you may try to separate rotations and translations by using two matrix transforms above loaded model. MatrixTransformTranslate->MatrixTransformRotate->Object. Apply only rotations to MatrixTransformRotate and translations to MatrixTransformTranslate. Cheers, Wojtek 2017-05-06 8:51 GMT+02:00 Jiechang Guo: > Hi Wojtek, > First, Thank you very much for your detailed reply. > 1. It's my mistake to say rotation around Y axis, I always think the z > axis is actually the y axis. > 2. The origin variable is > osg::Matrix origin = model1->getMatrix(); > I update this variable everytime when I translate or rotate the model. And > multiply it with my transform matrix so that I can get the correct result > after changing the position or orientation the model many times. Please > Correct Me if I'm not correct. > 3. OMG..I tried what you told me to. I just... I think I undestand what's > going on in side the constructor. No wonder I got that results and some > previous work about trackball rotate I did is wrong. Thank you. > 4. I've done some experiments about the order of the origin matrix. I get > the same result either I multiply it at first or at last... > The code is below: > osg::ref_ptr model1 = new osg::MatrixTransform; > model1->addChild(osgDB::readNodeFile("E:\\objdata\\FEMUR.obj", a)); > osg::Matrix origin = model1->getMatrix(); > model1->setMatrix(origin*osg::Matrix::translate(100, 0, 0)); > osg::Vec3 Center = model1->getBound().center(); > origin = model1->getMatrix(); > osg::Quat quat(osg::PI_4, osg::Z_AXIS); > model1->setMatrix(origin*osg::Matrix::translate(-Center)* > osg::Matrix::rotate(quat)*osg::Matrix::translate(Center)*osg::Matrix::translate(100, > 0, 0)); > > The reason that I want to get the Trans() and Rotate() is that I'm > doing a task: Compute the deviation of the origin model and target model. > These two model are the same and when the origin model is being manipulted > to the position of target model(which is a mesh model) I have to compute > whether they are overlaped and skip to another task. > Actually, I've already implemented this function, but I was confused by: > when I do only rotate task, the trans I get from getMatrix().getTrans() is > changing. I even don't know why it works when I only compute the trans > deviation. The code is below. > model1Translation = m1.model->getMatrix().getTrans(); > model1Quat = m1.model->getMatrix().getRotate(); > model2Translation = m2.model->getMatrix().getTrans(); > model2Quat = m2.model->getMatrix().getRotate(); > osg::Vec3 positionbias = model2Translation - model1Translation; > osg::Quat rotationbias = model2Quat - model1Quat; > if (abs(positionbias.x()) <= 2 && abs(positionbias.y()) <= 2 && > abs(positionbias.z()) <= 2) > { > //if (abs(rotationbias.x())<=0.1&&) > //{ > hm->pressNext(); > //} > } > > Cheers, > Jiechang > > -- > Read this topic online here: > http://forum.openscenegraph.org/viewtopic.php?p=70887#70887 > > > > > > ___ > osg-users mailing list > osg-users@lists.openscenegraph.org > http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org > ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] 【NEWBIE】Question about osg::MatrixTransform::getMatrix().getTrans() and getRotate()
Hi Jiechang, Few observations: 1. You write you want rotation around Y axis (0,1,0). But you rotate around Z axis (0,0,1). Btw there are osg::X_AXIS = (1,0,0) , osg::Y_AXIS = (0,1,0), osg::Z_AXIS = (0,0,1) constants defined in OSG which you may directly. 2. What is the origin variable in your example ? This is probably the other matrix which you premultiply and it influences your results. 3. Values stored in quaternion fields are rather non intuitive. I suggest you just make simpler experiment. Set quaternion variable directly with osg::Quat quat( Angle, Axis ). For example as osg::Quat quat( osg::PI_4, osg::Z_AXIS) as you do in your example. And then examine under debugger what constructor does and whats actually stored on 4 fields of Quat. Those numbers won't be the same as the ones you passed to constructor. And thats correct. You will find a plenty of info on Quaternions on the web. Look for them if you need to learn more. 4. This line probably has wrong order of transformations --> model1->setMatrix(origin*osg::Matrix::translate(-Center)* osg::Matrix::rotate(osg::DegreesToRadians(45.0), 0, 0, 1)*osg::Matrix::translate(Center)); I suppose you rather want to make it like this --> model1->setMatrix(osg:: Matrix::translate(-Center)*osg::Matrix::rotate(osg::DegreesToRadians(45.0), 0, 0, 1)*osg::Matrix::translate(Center)*origin); Reason for this is OSG uses row major matrices, so if you want to transform vertex by matrix you do it like this: result = vert * matrix. Thus your origin transform should be multiplied as last transform. Hope this helps, Wojtek Lewandowski 2017-05-05 13:54 GMT+02:00 Jiechang Guo: > Hi, > I'm a newbie and not good at math. > Please > I'm so confused with osg::MatrixTransform::getMatrix().getTrans() > and getRotate(). > I use the code below to rotate an object around y axis about 45 > degrees. > > model1->setMatrix(origin*osg::Matrix::rotate(osg::DegreesToRadians(45.0), > 0, 0, 1)); > > I want to get the rotation of the model, so I used the function to > get a quat: > > osg::Quat rotation = model1->getMatrix().getRotate(); > > I thought the rotation should be like (0.7853982,0,0,1)。but the > result is(0,0,0.382683,0,92388). > I've checked the source code of OSG, and I cann't get any > inspriation from it. > Another case, I thought I should move the object to its center and > do rotate then move it back (according to some book or paper). The code is > below. > > osg::Vec3 Center = model1->getBound().center(); > model1->setMatrix(origin*osg::Matrix::translate(-Center)* > osg::Matrix::rotate(osg::DegreesToRadians(45.0), 0, 0, > 1)*osg::Matrix::translate(Center)); > > The object is on the same position and rotation as the first case. >I try to get the rotation, I get more confused.. >I thought I didn't change the position of the model, the tranlation > I get > > osg::Vec3 translation = model1->getMatrix().getTrans(); > > should be (0,0,0) but (-168.184,-141.218, 0) and the rotation is just like > the first case. >Could you please help me to figure out why I got those results? > > Thank you! > > Cheers, > Jiechang > > -- > Read this topic online here: > http://forum.openscenegraph.org/viewtopic.php?p=70883#70883 > > > > > > ___ > osg-users mailing list > osg-users@lists.openscenegraph.org > http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org > ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org