Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
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  
  OpenGL Axis Rotations  (Read 2374 times)
0 Members and 1 Guest are viewing this topic.
Offline kieve

Senior Newbie




I am Awesome.


« Posted 2011-05-07 04:12:33 »

SOLVED
See my last post for details.

I am programming a 3x3 puzzle cube (aka Rubik's Cube) and I am having some difficulty with axis rotations.
When i press "X" i have the rotX increase by 90 degrees, the same goes for Y, and Z. You can only rotate on one axis at a time.
The problem is, I wish for my cube to rotate relative to my perspective. An example of what happens now is if i rotate 90 degrees on the X axis, then try rotating on the Y axis, the cube turns on (what i see to be) the Z axis.

1  
2  
3  
      GL11.glRotatef(rotX, 1.0f, 0.0f, 0.0f);
      GL11.glRotatef(rotY, 0.0f, 1.0f, 0.0f);
      GL11.glRotatef(rotZ, 0.0f, 0.0f, 1.0f);


If you wish to demo the program: File Beam
Controls:
1 - turn red
2 - turn orange
3 - turn blue
4 - turn green
5 - turn yellow
6 - turn white
X - rotate on "X" axis
Y - rotate on "Y" axis
Z - rotate on "Z" axis
Left Shift - Hold to turn / rotate opposite direction.
Right Shift - Reset cube.
Escape - Exit.
Enter - Scramble cube.
Backspace - Undo last move (includes scramble moves).

import universe.characteristics.Awesome
class kieve extends Awesome{
    public kieve(){
        beAwesome();
    }
    public void beAwesome(){
        while(true)awesomeness++;
    }
}
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2011-05-07 05:02:13 »

I get a NullPointerException in org.lwjgl.opengl.GL11.glShadeModel() at line 160 in rubikEngine.Engine.initGL()
I also get a RuntimeException saying "Image based resources must be loaded as part of init() or the game loop. They cannot be loaded before initialisation." (haha they misspelled initialization) This was thrown inside the Slick library at line 426 in rubikEngine.Engine.loadTextures()

Hope that helped Cheesy

Offline Captain Awesome

Junior Member


Medals: 2


Hi


« Reply #2 - Posted 2011-05-07 14:40:59 »

I get a NullPointerException in org.lwjgl.opengl.GL11.glShadeModel() at line 160 in rubikEngine.Engine.initGL()
I also get a RuntimeException saying "Image based resources must be loaded as part of init() or the game loop. They cannot be loaded before initialisation." (haha they misspelled initialization) This was thrown inside the Slick library at line 426 in rubikEngine.Engine.loadTextures()

Hope that helped Cheesy

The game worked fine here on W7.

To op: I see what you mean by that weird rotation thingy, it does seem like when you rotate around the x-axis, the y and z axis switch places with each other.
I haven't worked with opengl that much before, but wouldn't it be better to just have one glRotatef call? Like glRotatef(1.0f, rotX, rotY, rotZ); or something like that.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline kieve

Senior Newbie




I am Awesome.


« Reply #3 - Posted 2011-05-07 14:58:13 »

I get a NullPointerException in org.lwjgl.opengl.GL11.glShadeModel() at line 160 in rubikEngine.Engine.initGL()
I also get a RuntimeException saying "Image based resources must be loaded as part of init() or the game loop. They cannot be loaded before initialisation." (haha they misspelled initialization) This was thrown inside the Slick library at line 426 in rubikEngine.Engine.loadTextures()

Hope that helped Cheesy

The NPE might have been caused by your video card not supporting smooth shading, i removed it and re-uploaded. My The Slick texture loader is bound to fail sometimes, if it does it just loads with basic GL11.glColor3f() calls instead of texture binding, so that should be fine.

To op: I see what you mean by that weird rotation thingy, it does seem like when you rotate around the x-axis, the y and z axis switch places with each other.
I haven't worked with opengl that much before, but wouldn't it be better to just have one glRotatef call? Like glRotatef(1.0f, rotX, rotY, rotZ); or something like that.

Maybe, but from what I understand is that glRotatef(angle, x,y,z) where angle is the angle of rotation, and x,y,z is either 1.0f or 0.0f, depending if you want to rotate that angle on that axis. The problem is that the angle for each axis is not the same, say maybe I want to rotate 90 degrees on the X-axis, but 180 degrees on the Y-axis.

import universe.characteristics.Awesome
class kieve extends Awesome{
    public kieve(){
        beAwesome();
    }
    public void beAwesome(){
        while(true)awesomeness++;
    }
}
Offline Captain Awesome

Junior Member


Medals: 2


Hi


« Reply #4 - Posted 2011-05-07 15:01:33 »

To op: I see what you mean by that weird rotation thingy, it does seem like when you rotate around the x-axis, the y and z axis switch places with each other.
I haven't worked with opengl that much before, but wouldn't it be better to just have one glRotatef call? Like glRotatef(1.0f, rotX, rotY, rotZ); or something like that.

Maybe, but from what I understand is that glRotatef(angle, x,y,z) where angle is the angle of rotation, and x,y,z is either 1.0f or 0.0f, depending if you want to rotate that angle on that axis. The problem is that the angle for each axis is not the same, say maybe I want to rotate 90 degrees on the X-axis, but 180 degrees on the Y-axis.


Maybe, I just heard somewhere that you should minimize your calls to opengl. I should probably let someone more experienced answer this though Wink
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #5 - Posted 2011-05-07 22:54:13 »

Haha now I get an NPE at the next line, glClearColor(), and the same Slick exception.
Wow, my computer is the crappiest thing ever :/

Offline kieve

Senior Newbie




I am Awesome.


« Reply #6 - Posted 2011-05-08 01:47:43 »

I have been reading, and I'm coming across "quaternions" frequently and "gimbal lock". Any help with this?

import universe.characteristics.Awesome
class kieve extends Awesome{
    public kieve(){
        beAwesome();
    }
    public void beAwesome(){
        while(true)awesomeness++;
    }
}
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #7 - Posted 2011-05-08 05:11:26 »

Are Google and Wikipedia broken again?

Offline kieve

Senior Newbie




I am Awesome.


« Reply #8 - Posted 2011-05-08 13:46:02 »

Are Google and Wikipedia broken again?

Absolutely not, like i said, I have been reading, a TON. The thing is I don't know how to implement it in OpenGL.

import universe.characteristics.Awesome
class kieve extends Awesome{
    public kieve(){
        beAwesome();
    }
    public void beAwesome(){
        while(true)awesomeness++;
    }
}
Offline kieve

Senior Newbie




I am Awesome.


« Reply #9 - Posted 2011-05-08 16:57:33 »

I have come up with some code that to me seems that it should work, but does not and does work.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
if(rotating){
   currentSpeed = rotationSpeed;
   quatRotationChange.setFromAxisAngle(new Vector4f(turnX, turnY, turnZ, (isPrime? -1:1) * currentSpeed));      
   Quaternion.mul(quatCurrentRotation, quatRotationChange, quatCurrentRotation);
   quatCurrentRotation.normalise();
   createMatrixBuffer();
}      
     
GL11.glLoadIdentity();
GL11.glTranslatef(0f, 0f, -10f);

createMatrixBuffer();
GL11.glMultMatrix(matrixBuffer);


createMatrixBuffer method is 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  
private void createMatrixBuffer(){
   float w,x,y,z;
   w = quatCurrentRotation.w;
   x = quatCurrentRotation.x;
   y = quatCurrentRotation.y;
   z = quatCurrentRotation.z;
   Matrix4f m = new Matrix4f();
     
   m.m00 = sq(w) + sq(x) - sq(y) - sq(z);
   m.m01 = 2 * x * y + 2 * w * z;
   m.m02 = 2 * x * z - 2 * w * y;
   m.m03 = 0;

   m.m10 = 2 * x * y - 2 * w * z;
   m.m11 = sq(w) - sq(x) + sq(y) - sq(z);
   m.m12 = 2 * y * z + 2 * w * x;
   m.m13 = 0;
   
   m.m20 = 2 * x * z + 2 * w * y;
   m.m21 = 2 * y * z - 2 * w * x;
   m.m22 = sq(w) - sq(x) - sq(y) + sq(z);
   m.m23 = 0;

   m.m30 = 0;
   m.m31 = 0;
   m.m32 = 0;
   m.m33 = sq(w) + sq(x) + sq(y) + sq(z);
     
   matrixBuffer.clear();
   m.store(matrixBuffer);
   matrixBuffer.flip();
}
   
private float sq(float x){
   return x*x;
}


I used the formula found Here

import universe.characteristics.Awesome
class kieve extends Awesome{
    public kieve(){
        beAwesome();
    }
    public void beAwesome(){
        while(true)awesomeness++;
    }
}
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.

TehJavaDev (17 views)
2014-08-28 18:26:30

CopyableCougar4 (26 views)
2014-08-22 19:31:30

atombrot (39 views)
2014-08-19 09:29:53

Tekkerue (36 views)
2014-08-16 06:45:27

Tekkerue (33 views)
2014-08-16 06:22:17

Tekkerue (22 views)
2014-08-16 06:20:21

Tekkerue (33 views)
2014-08-16 06:12:11

Rayexar (67 views)
2014-08-11 02:49:23

BurntPizza (45 views)
2014-08-09 21:09:32

BurntPizza (36 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!