Diagonal rotation? glRotatef() function problem

Started by DeepuDon, May 01, 2016, 09:36:20

Previous topic - Next topic

DeepuDon

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..

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

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.

DeepuDon

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

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.

DeepuDon

and how do you do that? Please tell detailly..

Kai

glLoadIdentity();
glRotatef(angle around x axis);
glRotatef(angle around y axis);

DeepuDon

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

DeepuDon

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

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:
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.