[osg-users] Orientating model in 3D space with quaternions

Sonya Blade sonyablade2010 at hotmail.com
Thu Jul 2 10:18:36 PDT 2015


Dear All,
I load a model with the following code in my program, p1 and p2 are osg::Vec3 type variables and simply representingthe each ends of object in spatial space ( I also use them to find the direction vector). What I want to achive is: to place that model up vector always aligned  facing upward ( this doesn't mean that it must coincidence/be parallel with global Z axis)  to achieve that  If I apply the quaternions then object is positioned with correct direction vector (calulated via p1, p2)  but rolled with arbitrary angle along that direction vector, which violates my conditional goal.
It also seems that it's not a supprise that it produces that result bcoz nowhere I specify the crucial right or up vector with quaternion which is required to position the object in 3D space. So How am I supposed to correct that effect either with quaternions or matrices ?  
                  loadedModel = osgDB::readNodeFile("quad.obj");                  osg::Vec3Array* verlist= static_cast<osg::Vec3Array* >(rubber_line->getVertexArray());                  osg::Vec3 p1 = (*verlist)[0];                  osg::Vec3 p2 = (*verlist)[1];
                  osg::ref_ptr<osg::PositionAttitudeTransform> Transf = new osg::PositionAttitudeTransform;                  Transf->setPosition( p1 );
                  osg::Quat quad;                  osg::Matrix matrix, mat2;
                  quad.makeRotate(osg::Vec3(0,1,0),p2-p1);
                  Transf->setAttitude(quad);                    Transf->addChild(loadedModel);                    rootnode->addChild(Transf);

 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/attachments/20150702/fc410cb2/attachment-0002.htm>


More information about the osg-users mailing list