Hot questions for Using Lightweight Java Game Library in rotation

I'm trying to create a FPS game in Java using LWJGL 3.0. I've set up a camera class, that has a pitch and yaw (roll is not being used). The camera itself extends Entity, as it has a model. This model I would like to appear to always be "in front" of the camera, wherever the camera is pointing. Each Entity has a method getTransformationMatrix() which returns a Matrix4f, that is then passed into the entity shader.

Problem

The model needs to point in the direction of the camera, as well as rotate around the camera, such that it is always in front. The object in this situation is hands with a gun, as shown in the photo below.

My Attempt

I am aware of basic trigonometry, so I got the object to rotate correctly for pitch and yaw, separately. This is my current implementation:

I have done some research but I fear I have been stuck on this too long and need some fresh eyes. When I try to combine these 2 calculations, the model seems to move in the shape of a graph when looking at any yaw angle other than 0. Below is my attempt of combining these:

A friend suggested creating a unit vector that points in the direction of up, (ie. new Vector3f(0, 1, 0)) rotating the Vector by the pitch and yaw, then multiplying the Vector by the radius and adding it to the camera's position. I tried this, but I don't know how to rotate a Vector by an angle, and there seems to be no Vector3f.rotate() method in the slick-utils Vector3f class. Any help is is thoroughly appreciated as this has been giving me a headache for the past few days. Thanks!

Answer:

What we normally do is, yes, take a unit-length vector and use it as our "axis". In 2D rotation we use an axis - the Z axis - all the time.

3D rotation

If you were to look at the axis, like in 2D, you would see something like this

2D rotation

So, to rotate a point in 3D you can use a matrix or a vector. I recommend the vector first so you can get an idea of how 3D rotation works. It blows your mind!

I'll drop the code from a Vector3f class from theBennyBox . If you're interested in more of this math check out theBennyBox on Youtube.

Long story short, I'm making an LWJGL engine, and am drawing a basic QUAD. When I draw this QUAD and use the Keyboard listener, it moves perfectly, up/down/left/right.

When I translate and rotate however, it also pivots around its center point just fine, use both of them together, and then you have a problem. Rotation starts to go off axis, and everytime you move, you pivot around a strange point somewhere else.

How can I make it so I can move the QUAD (not relative to rotation), aswell as rotate it?

Edit 1

I have found out that a major problem with this QUAD, is that when I goto rotate it, my entire screen, (text included) rotates...

My current results:

Before Movement

After Movement (Hopefully you notice that it moved in a circle, instead of just LEFT when I move LEFT.)

Any help will be gratefully appreciated, if you are going to downvote, please give a reason so I can improve this question, or even better, comment instead.

Answer:

The problem here is the order in which you call the translate and rotate functions of OpenGL in your setRotationfunction . The order of calling must be Translation(if any)-> Rotation(if any)->Scaling. You cannot change this order of calling or mix them up in anyway. Or else you'll not get the desired results. This is because the way in which these commands are processed internally by OpenGL.

I'm currently following the OpenGL tutorials over at https://learnopengl.com/ except I'm doing it in Java instead of C++. It's going quite well but I've stumbled upon something that may be caused by a difference in implementation, unless I'm doing something wrong.

I'm at the part where a cube has just been rendered and is rotating. The tutorial gives the following code:

To elaborate on this, model is the model matrix. The GLM library is used to rotate this matrix, using glfwGetTime() which returns the time in seconds since initialization. The part that I'm curious about is the vec3(0.5f, 1.0f, 0.0f). Here they specify which Euler angles/axes to rotate around, and by the looks of it, specifying 0.5f for the X axis rotates it half the amount on X compared to Y here. The resulting cube looks like this:

https://i.imgur.com/caK1BTS.mp4

Back to Java, I have set up a similar thing. I run the following on the model matrix:

The difference here is that instead of doing "half" an axis, I instead do half the degrees on a full axis.

My question then is: Is it wrong of me to attempt to rotate around an axis that is not set to 1.0f? Is the GLM implementation of the 4x4 matrix rotate function different from the LWJGL-Utils' Matrix4f class?

PS: I'm using LWJGL 2 and Java 8u141. I'd be happy to provide more code about my setup if requested, but the entire process for rendering a cube is extensive. I've done the tutorial 2-3 times from scratch with the same result, so I think there's something going on here.

Also, I'm not shure that you're right when calling rotation axis as 'Euler angles'. Euler angles may define rotation about first axis and then about derived second axis and so on. They basically define rotation as combination of three rotations about two or three base axes. Here you have one rotation axis and rotation angle.

Wikipedia: Axis–angle representation. It says that only two components of rotation axis are required because of unit length, but to describe rotation axis in cartesian coordinates you will need 3-component unit length vector.

I have an object that can rotate on all axes, my problem is that if you rotate the object on the y axis (eg 90 °) the rotation on the z axis is incorrect because it rotates over the x axis,Instead, rotation on the x axis continues to function perfectly, If the y axis is set to 0 °, the rotation of the z axis returns to function correctly.

I've been having a few problems with my fragment shader. After a bit of research, it appears that drivers clean up unused variables (ones which have no effect on the output?), which can cause glSetUniform and glGetUniform to return -1.

My current problem is that I'm attempting to rotate a texture 180 degrees, but it appears that I'm doing something incorrectly, as the uniform int "top" appears to be garbage collected, and cannot be found. The texture is not rotated at all, but still renders. The uniform "top" returns -1, which should not happen.

The compiler can not only eliminate completely unused variables, but also values that do not contribute to the result. In your code, top is used here:

if (top == 1) {
myTc.y = top - myTc.y;
}

But myTc is not used later in the shader. So the value of myTc has no effect on the output of the shader. Which in turn means that the value of top has no effect. Or in other words, the result of the shader is the same, independent of the value of top.

This changes if myTc is used later in the shader, in a way that influences the result. I actually suspect that you meant to do that, and use it as the texture coordinates to sample your texture. So instead of:

I have been trying to make a 3D game engine. So far everything works except rotating bounding box. I have tried different ways to do it that I found here. Almost everything is the same. "Make rotation matrix and apply it to your bounding boxes min and max." That is the way I am doing it now, but there is all kinds of problem. I think it is rotating around the worlds zero point. Also I have two objects showing the min and max points and after rotating it is not colliding between these two things.

Unanswered Questions

We will update and show the full solutions if these questions are resolved.

mouse movement FPS style in Java

So I'm trying to implement a simple mouseLook method for exploring a game world in LWJGL. For now I just want it to work on the rotation-Y axis. The movement at first was limited because of the ...

How to rotate Shape which is aggregation of rotated individual parts?

My drawing which is again aggregation of small lines or circle which i am rotating (mostly lines) at various angels.Now I want to rotate this entire drawing to certain angel.You can see what i want ...

How do I rotate a 3D object about an axis without changing it's location? (LWJGL)

I'm currently trying to get a composite 3D object that consists of two 3D pyramids to rotate about a given axis by an angle in radians. However, one of the pyramids of the composite object is not ...