Hello Guest

Diagonal rotation? glRotatef() function problem

  • 8 Replies
  • 7252 Views
Diagonal rotation? glRotatef() function problem
« on: May 01, 2016, 09:36:20 »
Hey guys.. I'm Don, I need help with one feature that's doing something funny.. I wanted to make a camera rotation system.. and I did make one using glRotatef() but.. when I move my mouse in a circle.. the camera tilts in one angle.. I didn't do anything else I just wrote these..

Code: [Select]
GL11.glRotatef((float)Math.toRadians(rY*5), 0.5f, 0.0f, 0.0f);
GL11.glRotatef((float)Math.toRadians(rX*5), 0.0f, 0.5f, 0.0f);

A screenshot if you don't understand what I am talking about



http://imgur.com/t1awJRu

please help me out guys.. I want your help.. I already know how to code 2D games.. but 3D is my first.. So.. please help me guys I am learning

*

Kai

Re: Diagonal rotation? glRotatef() function problem
« Reply #1 on: May 01, 2016, 11:30:52 »
This is because the previous rotation always changes the frame of reference for the other rotation.
Both rotations do not happen simultaneously in the same local coordinate system.
You'll notice that when you change the direction of your mouse rotation (counter-clockwise vs. clockwise) it will also change in which direction your quad tilts.
To help you out with this, it'll be helpful to know what exactly you are trying to achieve.

Re: Diagonal rotation? glRotatef() function problem
« Reply #2 on: May 01, 2016, 12:15:53 »
I'm sorry I got you wrong.. I don't want to make the quad rotate. I just want to rotate the CAMERA. I am trying to achieve a first person camera perspective. But its not just working.. I understood that the frame of reference changes. But how to keep the frame of reference adjusted to a constant?
Please note that I want to change the rotation of the camera and not the quad

*

Kai

Re: Diagonal rotation? glRotatef() function problem
« Reply #3 on: May 01, 2016, 14:44:52 »
The solution is not to accumulate/concatenate all rotations over all frames via glRotatef, but instead to remember the angles around the Y and X axes, respectively, and do the two glRotatef on an identity matrix each frame.

Re: Diagonal rotation? glRotatef() function problem
« Reply #4 on: May 01, 2016, 15:23:36 »
and how do you do that? Please tell detailly..

*

Kai

Re: Diagonal rotation? glRotatef() function problem
« Reply #5 on: May 02, 2016, 08:10:46 »
glLoadIdentity();
glRotatef(angle around x axis);
glRotatef(angle around y axis);

Re: Diagonal rotation? glRotatef() function problem
« Reply #6 on: May 02, 2016, 11:25:33 »
oh makes sense.. let me try that out... let's try if it works.. I totally forgot that glLoadIdentity resets stuffs :) Thanks for the information though.. let me check it out.. Be right back

Re: Diagonal rotation? glRotatef() function problem
« Reply #7 on: May 02, 2016, 12:56:44 »
Nopes... its not working.. I was thinking about the Matrix4f Class... we have some methods for rotate() right? How can I use the rotate() method in my program to achieve this FPS camera Rotation thingie?

*

Kai

Re: Diagonal rotation? glRotatef() function problem
« Reply #8 on: May 02, 2016, 13:48:16 »
I don't know what Matrix4f class you are exactly talking about. You are using LWJGL 2.x?
Yes, you could use the matrix class. However, that is in no way different to what you can achieve with the glRotate/glTranslate/glLoadIdentity methods.
You just need to understand that you need to do:
Code: [Select]
glRotatef(x rotation)
glRotatef(y rotation)
glTranslatef(-playerPosition)
for a first-person camera.

An example with LWJGL3 using JOML with its Matrix4f class can be seen here: https://github.com/JOML-CI/joml-lwjgl3-demos/blob/master/src/org/joml/lwjgl/FirstPersonCameraDemo.java
But the actual implementation is irrelevant. The only thing that counts is those three basic transformations above in that order.
In the mentioned demo it is these lines.
« Last Edit: May 09, 2016, 10:29:15 by Kai »