Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (753) Games in Android Showcase (228) 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
 Need help with quanterion (cube rotate)  (Read 6188 times) 0 Members and 1 Guest are viewing this topic.
akzyl

Senior Newbie

 « Posted 2012-03-04 19:20:31 »

Hello.
I need help with quanterion. I have spent on this all saturday and couple hour today and still I don't know how to do it properly (I feel so dumb).
I want to rotate cube (around on own x,y or z axis) in fact on the beginning only  two wall front and right.
//front wall
-0.5f,  0.5f,  0.0f,
0.5f,  0.5f,  0.0f,
0.5f, -0.5f,  0.0f,
-0.5f, -0.5f,  0.0f,
//right wall
0.5f,  0.5f,  0.0f,
0.5f, -0.5f,  0.0f,
0.5f, -0.5f, -1.0f,
0.5f,  0.5f, -1.0f

So if I want to make a rotation on Z axis (roll) I must to do something with 8 vectors and I did something like this
 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  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62 `private final static float DEGTORAD = (float)(Math.PI/180);private static float rollAngle = 4;private static Quaternion roll = new Quaternion();private Quaternion change;private float[] array = new float[]{          -0.5f,  0.5f,  0.0f,            0.5f,  0.5f,  0.0f,            0.5f, -0.5f,  0.0f,           -0.5f, -0.5f,  0.0f,                    0.5f,  0.5f,  0.0f,            0.5f, -0.5f,  0.0f,            0.5f, -0.5f, -1.0f,            0.5f,  0.5f, -1.0f};private FloatBuffer vertexData;private void update(){      roll();      for(int i = 0;i

and I must admit that the front wall rotate properly also right but unfrtunateli something bad happening with right wall namely
is getting smaller and smaller. The same happens with front wall if I change rotation e.g.
roll.setFromAxisAngle(new Vector4f(1, 0, 0, rollAngle * DEGTORAD));

What's wrong how to do this properly?

I think i should create this topic in 'LWJGL Development' it's more appropriate place. Can someone move it.
DiEvAl

Junior Newbie

 « Reply #1 - Posted 2012-03-04 21:31:54 »

It looks like you are creating wrong quaternion in roll().
akzyl

Senior Newbie

 « Reply #2 - Posted 2012-03-04 22:19:03 »

It looks like you are creating wrong quaternion in roll().
That's why I ask. I really don't know how it should be.
 Games published by our own members! Check 'em out!
Roquen

JGO Kernel

Medals: 517

 « Reply #3 - Posted 2012-03-05 04:52:36 »

A rotation about some axis 'V', where V is a normalized vector about an angle "a" in quaternion space is: sin(a/2)V + cos(a/2).

I'd assume that the Quaternion class that you're using has a method to directly set and/or create one directly from an axis angle pair.
akzyl

Senior Newbie

 « Reply #4 - Posted 2012-03-05 05:45:29 »

A rotation about some axis 'V', where V is a normalized vector about an angle "a" in quaternion space is: sin(a/2)V + cos(a/2).

I'd assume that the Quaternion class that you're using has a method to directly set and/or create one directly from an axis angle pair.
I'm using
org.lwjgl.util.vector.Quaternion
Roquen

JGO Kernel

Medals: 517

 « Reply #5 - Posted 2012-03-05 06:01:40 »

Humm...okay, something like this:
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16 `// x,y,z must be a unit vector & 'a' is the angle.public static Quaternion setAxisAngle(Quaternion q, float x, float y, float z, float a){  a *= 0.5f;  float sinA = (float)Math.sin(a);  float cosA = (float)Math.cos(a);  x *= sinA;  y *= sinA;  z *= sinA;  q.set(x,y,z,cosA);  return q;}`
DiEvAl

Junior Newbie

 « Reply #6 - Posted 2012-03-05 06:04:44 »

Try this.
 1  2  3  4  5 `         change = new Quaternion(array[i], array[i+1], array[i+2], 1.0f);         Quaternion.mul(roll, change, change);         array[i] = change.x / change.w;         array[i+1] = change.y / change.w;         array[i+2] = change.z / change.w;`

(http://en.wikipedia.org/wiki/Homogeneous_coordinates)
akzyl

Senior Newbie

 « Reply #7 - Posted 2012-03-05 07:30:24 »

Humm...okay, something like this:
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16 `// x,y,z must be a unit vector & 'a' is the angle.public static Quaternion setAxisAngle(Quaternion q, float x, float y, float z, float a){  a *= 0.5f;  float sinA = (float)Math.sin(a);  float cosA = (float)Math.cos(a);  x *= sinA;  y *= sinA;  z *= sinA;  q.set(x,y,z,cosA);  return q;}`

This method doing the same what
roll.setFromAxisAngle(new Vector4f(0, 0, 1, rollAngle * DEGTORAD));
and result is the same.
Try this.
 1  2  3  4  5 `         change = new Quaternion(array[i], array[i+1], array[i+2], 1.0f);         Quaternion.mul(roll, change, change);         array[i] = change.x / change.w;         array[i+1] = change.y / change.w;         array[i+2] = change.z / change.w;`

(http://en.wikipedia.org/wiki/Homogeneous_coordinates)
and this in general does terrible things which I will not describe
Stranger
 « Reply #8 - Posted 2012-03-05 08:56:20 »

If you want to use quaternion Q to rotate point (vertex) P with respect to translated origin G use this formula:

P' = Q(P-G)Q' + G

where P' is the transformed point, and Q' is the quaternion inverse.

Anton
Roquen

JGO Kernel

Medals: 517

 « Reply #9 - Posted 2012-03-05 09:04:01 »

That's nice algebraically, but it significantly reduces.  But you hit on a good point, a rotation isn't being performed.
 Games published by our own members! Check 'em out!
Stranger
 « Reply #10 - Posted 2012-03-05 12:29:05 »

Here is some code. Hope it will work with minor changes.

 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  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44 `...   private void update(){      roll();      for(int i = 0;i

Anton
akzyl

Senior Newbie

 « Reply #11 - Posted 2012-03-05 17:37:41 »

Oh gosh!!! Stranger and Roquen your code works the same I mean PROPERLY  .
The mistake was in this part of code
 1  2  3 `array[i] = change.x;array[i+1] = change.y;array[i+2] = change.z;`

during rotation around axis Z I shouldn't do this
 1 `array[i+2] = change.z;`

around axis Y I shouldn't do this
 1 `array[i+1] = change.y;`

and
 1 `array[i] = change.x;`

during rotation around axis X.

I really appreciate your help.
Thanks all of you!
Roquen

JGO Kernel

Medals: 517

 « Reply #12 - Posted 2012-03-07 09:04:03 »

That great.  Now some comments.  First you're modifing your source data by incrementally rotating it.  You don't want to do this as float point operations introduce errors.  You want to make a modified copy, always using the 'source' data as input.  So you need two quaternions, one for the current rotation and a second (what you already have) to update the first (by a multiply).  The second thing is that directly rotating a vector by quaternion should only be used when the number of vectors is very small (like a couple), otherwise convert to a matrix and rotate with it.  And since the target data is to be used solely by the graphics card, you don't need to rotate the vectors yourself.  Feed the matrix to OpenGL and let it do the work.  If your library doesn't have a conversion, see this thread: http://www.java-gaming.org/topics/skeletal-animation/25483/view.html

@Stranger: You don't want to directly transcribe the algebraic equation into code.  The formula contains a fair amount of cancellation (by design), multiplies by zero & unneeded normalization which is a pretty undesirable thing in float point computation.  See here for these operations removed: http://www.java-gaming.org/topics/quaternion-vector-multiplication/25517/view.html
Stranger
 « Reply #13 - Posted 2012-03-07 09:55:08 »

@Roquen: I had in mind general case, just to get it working. Of course, the code can be optimized (if any).

Anton
Pages: [1]
 ignore  |  Print

 nelsongames (17 views) 2018-04-24 18:15:36 nelsongames (13 views) 2018-04-24 18:14:32 ivj94 (587 views) 2018-03-24 14:47:39 ivj94 (49 views) 2018-03-24 14:46:31 ivj94 (400 views) 2018-03-24 14:43:53 Solater (64 views) 2018-03-17 05:04:08 nelsongames (110 views) 2018-03-05 17:56:34 Gornova (175 views) 2018-03-02 22:15:33 buddyBro (723 views) 2018-02-28 16:59:18 buddyBro (93 views) 2018-02-28 16:45:17
 KaiHH 13x ByerN 11x Guerra2442 10x SHC 10x NuclearPixels 10x Zemlaynin 10x Damocles 6x VaTTeRGeR 5x orangepascal 4x philfrei 4x ags1 3x Spasi 3x ndnwarrior15 3x mesterh 3x JojoGlick 2x Springrbua 2x
 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