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

Sebastian Messerschmidt sebastian.messerschmidt at gmx.de
Fri Jul 3 00:39:41 PDT 2015


Am 03.07.2015 um 08:38 schrieb Sonya Blade:
> Hi Sebastian,
>
> > Usually you use an cosine-matrix for this. It allows to determine 
> the quaternion which translates one coordinate frame
> >(i.e. 3 independent vectors) into another, but I think osg doesn't 
> have this.
>
> Does anybody can confirm also that there is not a feature like that in 
> osg? These 3 vectors may seem that they are independent
> but actually they are dependant, because knowing any 2 of them you can 
> always get the 3rd one with cross product of first two.
> So any modifications on one of them affects others as well.
Uhm, yes they are independent (a cross product actually produces an 
independent vector from two independent vectors ...), that what is 
called an orthonormal base.
And as there is more than one '3rd' vector forming an orthonormal base 
with the other two vectors, your roll is not well defined.
>
> >I think you can use the makeRotate by applying the resulting 
> quaternion to the vec3(0,0,1) and (1,0,0)
> > and using the makeRotate with the results somehow to get the inverse.
> I already tried that without succes, the glitch here is at the bare 
> minimum I have to define 2 vectors (up/direction, up/right, 
> direction/right
> etcc combinaton) but I don't how to combine those two into a quaternion ?
Simply multiply the resulting quaternions.
Or if you have the 3 orthonormal axes of your rotated model, use the 
direct cosine matrix.
See:
https://en.wikipedia.org/wiki/Direction_cosine
and
https://en.wikipedia.org/wiki/Euclidean_vector#Multiple_Cartesian_bases

I'm using GMTL from http://ggt.sourceforge.net/ for the math and convert 
it to osg, if this would be an option for you.
GMTL is header only, so it might be worth a try

Cheers
Sebastian

>
> I forgot to mention that I get plenty of following errorson console 
> screen, when I assign direction vector, this happens when I try
> to pan, move, zoom of whole scene. Could that be the source of 
> discrepancy ?
Seems like some dimension is calcuated to 0 or a division by zero 
happens. Hard to guess without debugging it.
>
> CullVisitor::apply(Geode&) detected NaN,
>     depth=nan, center=(0 -2.5 0),
>     matrix={
>         nan nan nan nan
>         nan nan nan nan
>         nan nan nan nan
>         35 -35 0 1
> }
>
> ------------------------------------------------------------------------
> Date: Thu, 2 Jul 2015 19:25:34 +0200
> From: sebastian.messerschmidt at gmx.de
> To: osg-users at lists.openscenegraph.org
> Subject: Re: [osg-users] Orientating model in 3D space with quaternions
>
> Hi Sonya,
>
> Usually you use an cosine-matrix for this. It allows to determine the 
> quaternion which translates one coordinate frame (i.e. 3 independent 
> vectors) into another, but I think osg doesn't have this.
> I think you can use the makeRotate by applying the resulting 
> quaternion to the vec3(0,0,1) and (1,0,0) and using the makeRotate 
> with the results somehow to get the inverse.
>
> Cheers
> Sebastian
>
>     Dear All,
>
>     I load a model with the following code in my program, p1 and p2
>     are osg::Vec3 type variables and simply representing
>     the 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);
>
>
>
>
>     _______________________________________________
>     osg-users mailing list
>     osg-users at lists.openscenegraph.org  <mailto:osg-users at lists.openscenegraph.org>
>     http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
>
>
> _______________________________________________ osg-users mailing list 
> osg-users at lists.openscenegraph.org 
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
>
> _______________________________________________
> osg-users mailing list
> osg-users at lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/attachments/20150703/d8c4b9df/attachment-0003.htm>


More information about the osg-users mailing list