Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (534)
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 3491 times)
0 Members and 1 Guest are viewing this topic.
Offline theagentd
« Posted 2012-01-14 20: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.
Offline Roquen
« Reply #1 - Posted 2012-01-14 20: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.
Offline sproingie

JGO Kernel


Medals: 201



« Reply #2 - Posted 2012-01-14 23: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!
Legends of Yore - The Casual Retro Roguelike
Offline theagentd
« Reply #3 - Posted 2012-01-14 23: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<T> operator* 
   (
      detail::tquat<T> const & q,
      detail::tvec3<T> const & v
   )
   {
      typename detail::tquat<T>::value_type Two(2);

      detail::tvec3<T> uv, uuv;
      detail::tvec3<T> 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.
Offline Roquen
« Reply #4 - Posted 2012-01-15 10: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.)
Offline theagentd
« Reply #5 - Posted 2012-01-15 12: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.
Offline Nate

JGO Kernel


Medals: 145
Projects: 4
Exp: 14 years


Esoteric Software


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

These two might be helpful...
https://code.google.com/p/libgdx/source/browse/trunk/gdx/src/com/badlogic/gdx/math/Vector3.java
https://code.google.com/p/libgdx/source/browse/trunk/gdx/src/com/badlogic/gdx/math/Quaternion.java#170

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

Nate: You might want to add comments to methods which are not thread-safe.
Offline theagentd
« Reply #8 - Posted 2012-01-16 08:22:23 »

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

Myomyomyo.
Offline Bonbon-Chan

JGO Coder


Medals: 12



« Reply #9 - Posted 2012-01-16 08: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!
Legends of Yore - The Casual Retro Roguelike
Offline Roquen
« Reply #10 - Posted 2012-01-16 09: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.
Offline Roquen
« Reply #11 - Posted 2012-01-16 15: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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (35 views)
2014-07-24 01:59:36

Riven (33 views)
2014-07-23 21:16:32

Riven (21 views)
2014-07-23 21:07:15

Riven (24 views)
2014-07-23 20:56:16

ctomni231 (55 views)
2014-07-18 06:55:21

Zero Volt (47 views)
2014-07-17 23:47:54

danieldean (38 views)
2014-07-17 23:41:23

MustardPeter (43 views)
2014-07-16 23:30:00

Cero (59 views)
2014-07-16 00:42:17

Riven (56 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!