## Re: [Opensg-users] setting quaternions depending on two vectors

 Re: [Opensg-users] setting quaternions depending on two vectors From: Gerrit Voss - 2008-02-28 13:37:38 ```Hi, On Thu, 2008-02-28 at 14:07 +0100, sebbzdak@... wrote: > Hello, > > i have a question using quaternions. > I'm looking for a method to set a quaternion depending on two vectors - > like one vector for direction and one vector representing an up-vector. > The method i need should set a quaternion so that the quaternion could be > used to rotate an object into the state that it looks in direction given > by the direction-vectors and pointing up into direction given by the > up-vector. > Is there a possibility to do that? > > Or, likely the same: Is there a way to create a quaternion that represents > the coordinate system defined by three axes? as this is essentially a matrix, just put the your axis into the matrix and convert to the quaternion. kind regards, gerrit ```

 [Opensg-users] setting quaternions depending on two vectors From: - 2008-02-28 13:16:58 ```Hello, i have a question using quaternions. I'm looking for a method to set a quaternion depending on two vectors - like one vector for direction and one vector representing an up-vector. The method i need should set a quaternion so that the quaternion could be used to rotate an object into the state that it looks in direction given by the direction-vectors and pointing up into direction given by the up-vector. Is there a possibility to do that? Or, likely the same: Is there a way to create a quaternion that represents the coordinate system defined by three axes? Kind regards Sebastian ```
 Re: [Opensg-users] setting quaternions depending on two vectors From: Gerrit Voss - 2008-02-28 13:37:38 ```Hi, On Thu, 2008-02-28 at 14:07 +0100, sebbzdak@... wrote: > Hello, > > i have a question using quaternions. > I'm looking for a method to set a quaternion depending on two vectors - > like one vector for direction and one vector representing an up-vector. > The method i need should set a quaternion so that the quaternion could be > used to rotate an object into the state that it looks in direction given > by the direction-vectors and pointing up into direction given by the > up-vector. > Is there a possibility to do that? > > Or, likely the same: Is there a way to create a quaternion that represents > the coordinate system defined by three axes? as this is essentially a matrix, just put the your axis into the matrix and convert to the quaternion. kind regards, gerrit ```
 Re: [Opensg-users] setting quaternions depending on two vectors From: - 2008-02-28 15:40:20 ```Thank you for the advice. I don't know how i have to fill the matrix to set it adjusted to more than one vector. I would be happy to get a hint :) Sebastian > > Hi, > > On Thu, 2008-02-28 at 14:07 +0100, sebbzdak@... wrote: >> Hello, >> >> i have a question using quaternions. >> I'm looking for a method to set a quaternion depending on two vectors - >> like one vector for direction and one vector representing an up-vector. >> The method i need should set a quaternion so that the quaternion could >> be >> used to rotate an object into the state that it looks in direction given >> by the direction-vectors and pointing up into direction given by the >> up-vector. >> Is there a possibility to do that? >> >> Or, likely the same: Is there a way to create a quaternion that >> represents >> the coordinate system defined by three axes? > > as this is essentially a matrix, just put the your axis into the matrix > and convert to the quaternion. > > kind regards, > gerrit > > > > > ------------------------------------------------------------------------- > This SF.net email is sponsored by: Microsoft > Defy all challenges. Microsoft(R) Visual Studio 2008. > http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ > _______________________________________________ > Opensg-users mailing list > Opensg-users@... > https://lists.sourceforge.net/lists/listinfo/opensg-users > ```
 Re: [Opensg-users] setting quaternions depending on two vectors From: Gerrit Voss - 2008-03-03 02:16:31 ```Hi, On Thu, 2008-02-28 at 16:40 +0100, sebbzdak@... wrote: > Thank you for the advice. > I don't know how i have to fill the matrix to set it adjusted to more than > one vector. > I would be happy to get a hint :) > Matrix m; m[0] = x-axis; (y cross z) m[1] = y-axis; (usually up) (z cross x) m[2] = z-axis; (usually to) If you start with reasonably arbitrary up and to vectors, use a cross product to calculate x-axis and than adjust to up vector by replacing it with the cross between x-axis and z-axis (to). This way all your coordinate axis are right angled. kind regards, gerrit ```
 Re: [Opensg-users] setting quaternions depending on two vectors From: Sebastian B. - 2008-03-03 22:19:28 ```Thanks a lot! That works and was exactly what I was looking for. Kind regards Sebastian B. Gerrit Voss schrieb: > Hi, > > On Thu, 2008-02-28 at 16:40 +0100, sebbzdak@... wrote: > >> Thank you for the advice. >> I don't know how i have to fill the matrix to set it adjusted to more than >> one vector. >> I would be happy to get a hint :) >> >> > > Matrix m; > > m[0] = x-axis; (y cross z) > m[1] = y-axis; (usually up) (z cross x) > m[2] = z-axis; (usually to) > > If you start with reasonably arbitrary up and to vectors, use a > cross product to calculate x-axis and than adjust to up vector > by replacing it with the cross between x-axis and z-axis (to). > This way all your coordinate axis are right angled. > > kind regards, > gerrit > > > > > ------------------------------------------------------------------------- > This SF.net email is sponsored by: Microsoft > Defy all challenges. Microsoft(R) Visual Studio 2008. > http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ > _______________________________________________ > Opensg-users mailing list > Opensg-users@... > https://lists.sourceforge.net/lists/listinfo/opensg-users > ```
 Re: [Opensg-users] setting quaternions depending on two vectors From: Daniel Sperka - 2008-02-29 00:06:00 ```IIRC, this is similar to the transforms done in the billboards. I recently needed something similar to what you describe. First, I'll try to refer to the "direction" and "up" vectors, either the "old" or "new". You're seeking a transform which takes you FROM the "old direction and up" TO the "new direction and up". The quaternion you require is the product of two quaternions. One quaternion, call it qD, represents a rotation FROM the "old direction" vector to the "new direction" vector. The second quaternion takes care of the fact that qD does NOT necessarily rotate the "up" vector correctly. Vec3f direc_old, direc_new; // assume these get their values in Vec3f up_old, up_new; // your code somewhere ... ... Quaternion qD(direc_old, direc_new); // Now apply this transform to the "old up" vector Vec3f up_temp; qD.multVec(up_old, up_temp); // Make a second quaternion which rotates up_temp into the "new up" vector. Quaternion qU(up_temp, up_new); // The result is the product of these quaternions Quaternion qResult; qResult = qU; qResult.mult(qD); Can't say that this code will compile ;). I've paraphrased my own algorithm.... which does something similar but not quite the same. I must say that exercise really taught me how to use quaternions! It took me a while to convince myself this was right. Think of the qResult as the product of two transforms: first you transform the direction/pointing vector, then you apply the "up" transformation. The "up" transformation doesn't affect the first transform because it is really a rotation around the direction vector! Hope this helps! Dan -- Daniel J. Sperka, Ph. D. UC Davis Center for Neuroscience ```
 Re: [Opensg-users] setting quaternions depending on two vectors From: Sebastian B. - 2008-03-03 22:48:33 ```Thank you for your answere. I implemented your code and got almost the result i hoped for. There is one problem: I have a plane-modell and i calculate the axes for direction-vector, up-vector and side-vector in a way that i get an orthogonal coordinate system. To rotate the plane from origin into this coordinate-system i use your method implemented as shown beneath. This works well except if the planes-direction is the same as the world-x-axis and the up vector is (0, -1, 0) - than that the plane is rotated around his direction vector 180 degrees. Than instead of pointing as expected in the same direction, it ist turned around pointing to the oposite direction. The method fails in some other situations too. I'm not sure on which vector it depends. I could solve my problem using the method posted from Gerrit Voss on 02/28/08 and 03/04/08. But i am interested why this method is not working. OSG::Quaternion Mathematic::lookAt(OSG::Vec3f direction_new, OSG::Vec3f up_new) { OSG::Vec3f direction_old(1.0f, 0.0f, 0.0f); OSG::Vec3f up_old(0.0f, 1.0f, 0.0f); OSG::Quaternion qD(direction_old, direction_new); OSG::Vec3f up_temp; qD.multVec(up_old, up_temp); OSG::Quaternion qU(up_temp, up_new); OSG::Quaternion qResult; qResult = qU; qResult.mult(qD); return qResult; } void FlightState::rotateAroundFrontVector(float angle) { OSG::Quaternion rotation; rotation.setValueAsAxisDeg(frontVec, angle); rotation.multVec(upVec, upVec); rotation.multVec(downVec, downVec); rotation.multVec(leftVec, leftVec); rotation.multVec(rightVec, rightVec); // quaternionRotation is used to rotate the plane quaternionRotation = Mathematic::lookAt(frontVec, upVec); } void FlightState::rotateAroundUpVector(float angle) { ... } void FlightState::rotateAroundRightVector(float angle) { ... } Kind regards Sebastian B. Daniel Sperka schrieb: > IIRC, this is similar to the transforms done in the billboards. I > recently needed something similar to what you describe. > > First, I'll try to refer to the "direction" and "up" vectors, either the > "old" or "new". You're seeking a transform which takes you FROM the "old > direction and up" TO the "new direction and up". > > The quaternion you require is the product of two quaternions. > > One quaternion, call it qD, represents a rotation FROM the "old > direction" vector to the "new direction" vector. The second quaternion > takes care of the fact that qD does NOT necessarily rotate the "up" > vector correctly. > > > Vec3f direc_old, direc_new; // assume these get their values in > Vec3f up_old, up_new; // your code somewhere > > ... > ... > > Quaternion qD(direc_old, direc_new); > > // Now apply this transform to the "old up" vector > > Vec3f up_temp; > qD.multVec(up_old, up_temp); > > // Make a second quaternion which rotates up_temp into the "new up" > vector. > Quaternion qU(up_temp, up_new); > > // The result is the product of these quaternions > Quaternion qResult; > qResult = qU; > qResult.mult(qD); > > > Can't say that this code will compile ;). I've paraphrased my own > algorithm.... which does something similar but not quite the same. I > must say that exercise really taught me how to use quaternions! > > It took me a while to convince myself this was right. Think of the > qResult as the product of two transforms: first you transform the > direction/pointing vector, then you apply the "up" transformation. The > "up" transformation doesn't affect the first transform because it is > really a rotation around the direction vector! > > > Hope this helps! > > Dan > > > ```
 Re: [Opensg-users] setting quaternions depending on two vectors From: Daniel Sperka - 2008-03-04 17:53:07 ```Interesting. (hurries to check his own code for this problem....whew!) My usage of this technique is (thankfully) insulated from this little quirk. I believe the problem is that using Quaternion qD(a, b) where a and b point in opposite directions leads to an ambiguity. Suppose a and b are as you describe, (0, 1, 0) and (0, -1, 0). You can rotate a into b by rotating 180 degrees around the x axis. You can also do it by rotating around the z axis. The choice is arbitrary, and in this case the code gives you an incorrect answer. Take a look at the code for QuaternionBase::setValue(VectorType& from, VectorType& to).... in 1.8 its at line 360 of OSGQuaternion.inl. There are two checks - for parallel vectors and for anti-parallel vectors. In the latter case there is a section which attempts to choose an axis to rotate around. I suppose the better solution here is Gerrit's.... or possibly just using rotation.setValueAsAxisDeg(frontVec, angle); for your rotations. dan Sebastian B. wrote: > Thank you for your answere. > I implemented your code and got almost the result i hoped for. > There is one problem: > I have a plane-modell and i calculate the axes for direction-vector, > up-vector and side-vector in a way that i get an orthogonal coordinate > system. To rotate the plane from origin into this coordinate-system i > use your method implemented as shown beneath. > This works well except if the planes-direction is the same as the > world-x-axis and the up vector is (0, -1, 0) - than that the plane is > rotated around his direction vector 180 degrees. Than instead of > pointing as expected in the same direction, it ist turned around > pointing to the oposite direction. > The method fails in some other situations too. I'm not sure on which > vector it depends. > > I could solve my problem using the method posted from Gerrit Voss on > 02/28/08 and 03/04/08. > But i am interested why this method is not working. > > OSG::Quaternion Mathematic::lookAt(OSG::Vec3f direction_new, OSG::Vec3f > up_new) > { > OSG::Vec3f direction_old(1.0f, 0.0f, 0.0f); > OSG::Vec3f up_old(0.0f, 1.0f, 0.0f); > > OSG::Quaternion qD(direction_old, direction_new); > > OSG::Vec3f up_temp; > qD.multVec(up_old, up_temp); > > OSG::Quaternion qU(up_temp, up_new); > > OSG::Quaternion qResult; > qResult = qU; > qResult.mult(qD); > > return qResult; > } > > void FlightState::rotateAroundFrontVector(float angle) > { > OSG::Quaternion rotation; > rotation.setValueAsAxisDeg(frontVec, angle); > rotation.multVec(upVec, upVec); > rotation.multVec(downVec, downVec); > rotation.multVec(leftVec, leftVec); > rotation.multVec(rightVec, rightVec); > > // quaternionRotation is used to rotate the plane > quaternionRotation = Mathematic::lookAt(frontVec, upVec); > } > > void FlightState::rotateAroundUpVector(float angle) > { > ... > } > > void FlightState::rotateAroundRightVector(float angle) > { > ... > } > > > Kind regards > > Sebastian B. > > > Daniel Sperka schrieb: > >> IIRC, this is similar to the transforms done in the billboards. I >> recently needed something similar to what you describe. >> >> First, I'll try to refer to the "direction" and "up" vectors, either the >> "old" or "new". You're seeking a transform which takes you FROM the "old >> direction and up" TO the "new direction and up". >> >> The quaternion you require is the product of two quaternions. >> >> One quaternion, call it qD, represents a rotation FROM the "old >> direction" vector to the "new direction" vector. The second quaternion >> takes care of the fact that qD does NOT necessarily rotate the "up" >> vector correctly. >> >> >> Vec3f direc_old, direc_new; // assume these get their values in >> Vec3f up_old, up_new; // your code somewhere >> >> ... >> ... >> >> Quaternion qD(direc_old, direc_new); >> >> // Now apply this transform to the "old up" vector >> >> Vec3f up_temp; >> qD.multVec(up_old, up_temp); >> >> // Make a second quaternion which rotates up_temp into the "new up" >> vector. >> Quaternion qU(up_temp, up_new); >> >> // The result is the product of these quaternions >> Quaternion qResult; >> qResult = qU; >> qResult.mult(qD); >> >> >> Can't say that this code will compile ;). I've paraphrased my own >> algorithm.... which does something similar but not quite the same. I >> must say that exercise really taught me how to use quaternions! >> >> It took me a while to convince myself this was right. Think of the >> qResult as the product of two transforms: first you transform the >> direction/pointing vector, then you apply the "up" transformation. The >> "up" transformation doesn't affect the first transform because it is >> really a rotation around the direction vector! >> >> >> Hope this helps! >> >> Dan >> >> >> >> > > ------------------------------------------------------------------------- > This SF.net email is sponsored by: Microsoft > Defy all challenges. Microsoft(R) Visual Studio 2008. > http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ > _______________________________________________ > Opensg-users mailing list > Opensg-users@... > https://lists.sourceforge.net/lists/listinfo/opensg-users > > -- Daniel J. Sperka, Ph. D. UC Davis Center for Neuroscience ```