Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (754) Games in Android Showcase (229) games submitted by our members Games in WIP (842) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
 Home Help Search Login Register
Pages: [1]
 ignore  |  Print
 [SOLVED] Quaternion - Vector multiplication?  (Read 9957 times) 0 Members and 1 Guest are viewing this topic.
theagentd
 « 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

JGO Kernel

Medals: 517

 « 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)
 Games published by our own members! Check 'em out!
theagentd
 « 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

JGO Kernel

Medals: 517

 « 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
 « 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: 167
Projects: 4
Exp: 14 years

Esoteric Software

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

Roquen

JGO Kernel

Medals: 517

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

theagentd
 « 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.
 Games published by our own members! Check 'em out!
Roquen

JGO Kernel

Medals: 517

 « 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

JGO Kernel

Medals: 517

 « 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

 DesertCoockie (33 views) 2018-05-13 18:23:11 nelsongames (75 views) 2018-04-24 18:15:36 nelsongames (69 views) 2018-04-24 18:14:32 ivj94 (752 views) 2018-03-24 14:47:39 ivj94 (82 views) 2018-03-24 14:46:31 ivj94 (622 views) 2018-03-24 14:43:53 Solater (98 views) 2018-03-17 05:04:08 nelsongames (179 views) 2018-03-05 17:56:34 Gornova (405 views) 2018-03-02 22:15:33 buddyBro (1065 views) 2018-02-28 16:59:18
 princec 18x SkyAphid 12x philfrei 12x NuclearPixels 11x ByerN 10x FabulousFellini 7x Guerra2442 6x cylab 6x orange451 5x VaTTeRGeR 5x KaiHH 4x 65K 4x ags1 4x orangepascal 4x murarz208 4x Riven 4x
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 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