Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (563) Games in Android Showcase (151) games submitted by our members Games in WIP (605) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 [SOLVED] Quaternion - Vector multiplication?  (Read 4311 times) 0 Members and 1 Guest are viewing this topic.
theagentd

« JGO Bitwise Duke »

Medals: 409
Projects: 2
Exp: 8 years

 « Posted 2012-01-14 19:40:37 »

I'm still trying to get skeleton animation working, but first I need to be able to load a model.

At first I was like "WHAT THE HELL IS THIS?!". Then I was like "Oh, this actually kind of makes sense!". Then I was like "WHAT THE HECK IS A QUATERNION?!". Then I was like "Haha! There's a Quaternion class in LWJGL!".
Then my example code was like
 1 `vec3 rotPos = joint.m_Orient * weight.m_Pos; //m_Orient is a quaternion`

and I was like "NO WAI YOU DID NOT JUST DO THAT" and it was like "Oh, yes, I did." and I was like "But there is no quaternion-vector multiplication function in the LWJGL classes!" and it was like "Well, screw you." Then Google was like "Did ya mean quaternion-quaternion multiplication X 1 000 000?" and I was like "NO." and then, well, here I am.

So, how do I multiply a quaternion and a vector to get a new magical vector?

Myomyomyo.
Roquen
 « Reply #1 - Posted 2012-01-14 19:55:08 »

I assume that your question is "how do I rotate a vector by a quaternion that represents a rotation?"  Because there's more that one meaning of multiple a vector by a quaternion.  If so, the the answer depends on how many vectors you want to rotate since there are multiple formulations.
sproingie

JGO Kernel

Medals: 202

 « Reply #2 - Posted 2012-01-14 22:33:46 »

LWJGL's Quaternion class is ... well it just isn't very good.  I'm not sure it even has a method to transform a Vector.

Java3D has a much more fully-featured quaternion class.  You can find it here though you might want to find an alternative implementation with source both for licensing and documentation reasons (that one doesn't even have a javadoc artifact)
theagentd

« JGO Bitwise Duke »

Medals: 409
Projects: 2
Exp: 8 years

 « Reply #3 - Posted 2012-01-14 22:35:24 »

I assume that your question is "how do I rotate a vector by a quaternion that represents a rotation?"  Because there's more that one meaning of multiple a vector by a quaternion.  If so, the the answer depends on how many vectors you want to rotate since there are multiple formulations.
You're exactly right. I want to rotate my position vector by the orientation quaternion. What do you mean by multiple formulations?

Also, this seems to be the relevant code in the C-something file:
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17 `   inline detail::tvec3 operator*    (      detail::tquat const & q,       detail::tvec3 const & v   )   {      typename detail::tquat::value_type Two(2);      detail::tvec3 uv, uuv;      detail::tvec3 QuatVector(q.x, q.y, q.z);      uv = glm::cross(QuatVector, v);      uuv = glm::cross(QuatVector, uv);      uv *= (Two * q.w);       uuv *= Two;       return v + uv + uuv;   }`

I have no idea what this means though...

LWJGL's Quaternion class is ... well it just isn't very good.  I'm not sure it even has a method to transform a Vector.

Java3D has a much more fully-featured quaternion class.  You can find it here though you might want to find an alternative implementation with source both for licensing and documentation reasons (that one doesn't even have a javadoc artifact)

I looked at the javadoc for that library, but I couldn't find a function for that either...

Myomyomyo.
Roquen
 « Reply #4 - Posted 2012-01-15 09:33:08 »

I'm not at home, so here's a quick answer off the top of my head.  (In other words, it probably doesn't work so verify it)

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28 `public final Vect3f rot(Vect3f v){  float  k0 = w*w - 0.5f;  float  k1;  float  rx,ry,rz;  // k1 = Q.V  k1    = v.x*x;  k1   += v.y*y;  k1   += v.z*z;  // (qq-1/2)V+(Q.V)Q  rx  = v.x*k0 + x*k1;  ry  = v.y*k0 + y*k1;  rz  = v.z*k0 + z*k1;  // (Q.V)Q+(qq-1/2)V+q(QxV)  rx += w*(y*v.z-z*v.y);  ry += w*(z*v.x-x*v.z);  rz += w*(x*v.y-y*v.x);  //  2((Q.V)Q+(qq-1/2)V+q(QxV))  rx += rx;  ry += ry;  rz += rz;  return new Vect3f(rx,ry,rz);}`

Quote
I want to rotate my position vector by the orientation quaternion. What do you mean by multiple formulations?
There are three main ways to have a quaternion represent a rotation.  Each of these can be "reversed" yielding six formulations with different properties (they are all related).  Given a specific mathematical formulation, each of may be used directly as if one was performing the operation in quaternions or it can be converted to some other mathematical type (almost always a matrix).  The above (assuming I didn't screw up) is a direct formulation where the quaternion is assume to be of unit length (otherwise it will yield a scaled rotation, where the scaling factor is the magnitude of the quaternion squared).  This question and your previous are really the same "in wolves clothing".  If you were to pug the vectors (1,0,0), (0,1,0), & (0,0,1) into the above it will yield three vectors (really every where we're saying vectors should be bivectors, but that's a different story).  Shove these three vectors into either a row or column of a matrix and you have the (misnamed) quaternion to matrix conversion.  Make it a 4x4 matrix and you can shove the translation vector in the appropriate spot and bingo...you're done.

Quote
LWJGL's Quaternion class is ... well it just isn't very good.
It doesn't really do anything and was obviously thrown together by someone with no understanding of quaternions (no offense intended if said person is reading this...you're far from alone.)
theagentd

« JGO Bitwise Duke »

Medals: 409
Projects: 2
Exp: 8 years

 « Reply #5 - Posted 2012-01-15 11:50:34 »

That was exactly what I needed! Thanks a lot! I also look forward to actually understanding that code when I get to college... >_>

"Take this appreciation! It's been in my family for years, but I feel that you should have it."

Myomyomyo.
Nate

« JGO Bitwise Duke »

Medals: 164
Projects: 4
Exp: 14 years

Esoteric Software

 « Reply #6 - Posted 2012-01-15 18:20:24 »

Roquen
 « Reply #7 - Posted 2012-01-16 05:18:22 »

theagentd

« JGO Bitwise Duke »

Medals: 409
Projects: 2
Exp: 8 years

 « Reply #8 - Posted 2012-01-16 07:22:23 »

Those tmp variables looked really bad, but were they actually used somewhere in the code?

Myomyomyo.
Bonbon-Chan

JGO Coder

Medals: 12

 « Reply #9 - Posted 2012-01-16 07:59:46 »

I only use quaternion to interpolate rotation. When I have I have compute all rotations, I transform quaternions to matrices. Then I only use matrix. I have never use quaternion with vector directly.
Roquen
 « Reply #10 - Posted 2012-01-16 08:51:09 »

The sweet point between direct computation and conversion to a matrix is hard to qualify.  For scalar computation converting to matrices generally be a win past some very small number of conversions.  For SIMD it really depends on the low level ops exposed.

@theagentd: I just skimmed...didn't look that closely.
Roquen
 « Reply #11 - Posted 2012-01-16 14:50:25 »

Quote
Quote
LWJGL's Quaternion class is ... well it just isn't very good.

It doesn't really do anything and was obviously thrown together by someone with no understanding of quaternions (no offense intended if said person is reading this...you're far from alone.)
Opps..I was actually thinking of some other library...I guess I should check my facts before opening my mouth.
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 wxwsk8er (49 views) 2015-03-20 15:39:46 Fairy Tailz (42 views) 2015-03-15 21:52:20 Olo (26 views) 2015-03-13 17:51:59 Olo (29 views) 2015-03-13 17:50:51 Olo (33 views) 2015-03-13 17:50:16 Olo (40 views) 2015-03-13 17:47:07 ClaasJG (29 views) 2015-03-10 11:36:42 ClaasJG (36 views) 2015-03-10 11:33:01 Pippogeek (45 views) 2015-03-05 14:36:23 Pippogeek (37 views) 2015-03-05 13:56:12
 KevinWorkman 23x LiquidNitrogen 23x basil_ 21x BurntPizza 21x theagentd 18x Riven 18x EgonOlsen 17x Roquen 14x princec 12x Varkas 9x Slyth2727 9x NegativeZero 9x SHC 9x Ecumene 8x ClaasJG 7x teletubo 7x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org