Removing roll from camera matrix

I have a camera class which is just this:
import org.joml.*;

public class Camera {
public Matrix4f viewMatrix = new Matrix4f().setTranslation(new Vector3f(100, 200, 100));

public void rotate(float x, float y) {
viewMatrix.rotateXYZ(x, y, 0);
}
public void move(float x, float y, float z) {
viewMatrix.translate(x, y, z);
}
}
import org.joml.*;

public class Camera {
public Matrix4f viewMatrix = new Matrix4f().setTranslation(new Vector3f(100, 200, 100));

public void rotate(float x, float y) {
viewMatrix.rotateXYZ(x, y, 0);
}
public void move(float x, float y, float z) {
viewMatrix.translate(x, y, z);
}
}
and it works, but I want to eliminate the roll from the matrix somehow.
107 Replies
JavaBot
JavaBot3mo ago
This post has been reserved for your question.
Hey @𝓐pothicon! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
𝓐pothicon
𝓐pothiconOP3mo ago
(roll as in the tilting of the camera from left to right as shown here)
No description
JavaBot
JavaBot3mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
𝓐pothicon
𝓐pothiconOP3mo ago
. /reopen
JavaBot
JavaBot3mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
𝓐pothicon
𝓐pothiconOP3mo ago
open seasame
JavaBot
JavaBot3mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Noah | Nowipi
Noah | Nowipi3mo ago
Then don't rotate it Does the default constructor of Matrix4f return an identity matrix?
𝓐pothicon
𝓐pothiconOP3mo ago
Yes (but not sure why that matters) but i need to rotate the matrix so that i can well.. rotate the camera around, just need to somehow eliminate the roll aspect immediately afterwards
Noah | Nowipi
Noah | Nowipi3mo ago
I don't understand Just don't rotate on the roll axis
𝓐pothicon
𝓐pothiconOP3mo ago
what do you do to not rotate on the roll axis tho ive tried everything i can think of
viewMatrix.rotateXYZ(x, y, 0);
viewMatrix.rotateXYZ(x, y, 0);
its not like their seperate numbers im getting three axis of rotation just by rotating x and y
JavaBot
JavaBot3mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
𝓐pothicon
𝓐pothiconOP3mo ago
.
JavaBot
JavaBot3mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
𝓐pothicon
𝓐pothiconOP2mo ago
.
tjoener
tjoener2mo ago
That's euler rotation, those are done by first rotating x then y What you want is another rotation completely Or rotate in a different order pitch yaw roll is a different rotation definition from euler xyz
𝓐pothicon
𝓐pothiconOP2mo ago
ive tried doing both orders either way i get roll and i dont think its euler rotation cuz if it was then why would i be getting roll from just two axis also im not sure what you mean by "another rotation" do you mean a different rotate() method? i tried many different ones i just want some kind of equation to remove the roll cant find one anywhere
JavaBot
JavaBot2mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
𝓐pothicon
𝓐pothiconOP2mo ago
.
tjoener
tjoener2mo ago
There's axis-angle, quaternion, ...
𝓐pothicon
𝓐pothiconOP2mo ago
Isnt axis-angle just euler which should never be used And im pretty sure what im using is a quaternion just inside of a matrix
tjoener
tjoener2mo ago
no axis angle is not the same and quaternions have to be converted to matrix form, you can't "use a quaternion in a matrix" https://www.andre-gaschler.com/rotationconverter/
𝓐pothicon
𝓐pothiconOP2mo ago
the quaternion is the matrix3 inside of the matrix4 also not sure why this would be helpful
tjoener
tjoener2mo ago
Under some very specific conditions only
𝓐pothicon
𝓐pothiconOP2mo ago
i mean its always the matrix3 i never store it anywhere else
tjoener
tjoener2mo ago
and then add scale...
𝓐pothicon
𝓐pothiconOP2mo ago
scale? what would that do
tjoener
tjoener2mo ago
multiply the numbers in the diagonal
𝓐pothicon
𝓐pothiconOP2mo ago
but i dont want scale its a camera lol cant have a big camera or a small camera size isnt a thing
tjoener
tjoener2mo ago
I'm just saying that a 3by3 affine transormation matrix isn't always JUST rotation
𝓐pothicon
𝓐pothiconOP2mo ago
what else would it be
tjoener
tjoener2mo ago
It's called an affine transformation for a reason Like I literally just said, it could also have scale built in
𝓐pothicon
𝓐pothiconOP2mo ago
oh
tjoener
tjoener2mo ago
And if you go to 4x4 you can also have translation built in
𝓐pothicon
𝓐pothiconOP2mo ago
im not seeing how that would be possible tbh but it doesnt matter
tjoener
tjoener2mo ago
For a camera you don't need it But for transforming your objects in the world you'd need it give me some angles you set in that rotation method you have
𝓐pothicon
𝓐pothiconOP2mo ago
wdym?
No description
𝓐pothicon
𝓐pothiconOP2mo ago
x and y are just the x and y the mouse moved
𝓐pothicon
𝓐pothiconOP2mo ago
No description
tjoener
tjoener2mo ago
ok, so let's say 45 degrees x and y type those in on the left side in rotationconverter and set both sides to degrees You get { [ 0.6785983, 0.6785983, 0.2810846 ], 62.7994296 } as an axis angle result
𝓐pothicon
𝓐pothiconOP2mo ago
where
No description
tjoener
tjoener2mo ago
euler angles
𝓐pothicon
𝓐pothiconOP2mo ago
ah yeah i do get that result but why
tjoener
tjoener2mo ago
so axis angle is pretty easy to reason about basically it's a vector pointing in a certain direction (your axis) and the angle you rotate around that vector (the angle) See that by using euler angles, you're rotating around your own axis as well? So you won't get a level horizon this way https://dugas.ch/transform_viewer/index.html This one has some visualization built in
𝓐pothicon
𝓐pothiconOP2mo ago
i didnt even know i was using euler angles lol
tjoener
tjoener2mo ago
That's what rotateXYZ does
𝓐pothicon
𝓐pothiconOP2mo ago
well i tried pretty much every rotate method
tjoener
tjoener2mo ago
https://javadoc.io/doc/org.joml/joml/latest/org/joml/Matrix4f.html#rotateXYZ(float,float,float) you want pitch/yaw/roll you need something like the lookAt methods
𝓐pothicon
𝓐pothiconOP2mo ago
but i dont want it to look at anything specific
tjoener
tjoener2mo ago
Doesn't matter, you can set your look at point to a coordinate in front of you
𝓐pothicon
𝓐pothiconOP2mo ago
if i knew what "in front of me" was i wouldnt be having this problem lol
tjoener
tjoener2mo ago
I suggest this then
tjoener
tjoener2mo ago
LearnOpenGL - Camera
Learn OpenGL . com provides good and clear modern 3.3+ OpenGL tutorials with clear examples. A great resource to learn modern OpenGL aimed at beginners.
tjoener
tjoener2mo ago
There's math involved oh yeah, and probably read the chapter on coordinate systems as well The one right before Because your camera shouldn't move 🙂
𝓐pothicon
𝓐pothiconOP2mo ago
yes it should
tjoener
tjoener2mo ago
no, the camera stays at 0,0,0 and you rotate/move the world around it read the articles
𝓐pothicon
𝓐pothiconOP2mo ago
horrible idea
tjoener
tjoener2mo ago
ok, go tell the video game industry you know better than them
𝓐pothicon
𝓐pothiconOP2mo ago
updating millions of objects a frame instead of one
tjoener
tjoener2mo ago
no, update one matrix per frame
𝓐pothicon
𝓐pothiconOP2mo ago
thats what im doing
tjoener
tjoener2mo ago
Read the articles Then you'll understand
𝓐pothicon
𝓐pothiconOP2mo ago
i read em a few times before i dont agree with them
tjoener
tjoener2mo ago
Well, it seems like you're the smarty pants that knows everything, so you don't need any help. Good luck figuring out your completely custom camera system
𝓐pothicon
𝓐pothiconOP2mo ago
i just need some help on removing the roll from my existing already figured out camera system or changing the way i rotate to not add roll in the first place i dont need to change the entire structure of the game to account for "moving the world" and i DEFINITELY dont need to convert the camera to use euler angles like you suggested by linking this euler = bad it even says in the article dont see why you care if i store the position in the camera matrix anyways its the same result that part is fully working
tjoener
tjoener2mo ago
"viewMatrix.rotateXYZ(x, y, 0);" <- euler
𝓐pothicon
𝓐pothiconOP2mo ago
yeah i didnt know that was euler so I need other rotate method been weeks trying to find the right one although its weird how thats euler yet im not getting any gimbal lock the camera works flawlessly aside from having roll if only there was a way to like rotate it globally instead of locally
JavaBot
JavaBot2mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
𝓐pothicon
𝓐pothiconOP2mo ago
.
JavaBot
JavaBot2mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
𝓐pothicon
𝓐pothiconOP2mo ago
.
JavaBot
JavaBot2mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
𝓐pothicon
𝓐pothiconOP2mo ago
.
JavaBot
JavaBot2mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
𝓐pothicon
𝓐pothiconOP2mo ago
.
JavaBot
JavaBot2mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
𝓐pothicon
𝓐pothiconOP2mo ago
.
JavaBot
JavaBot2mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
𝓐pothicon
𝓐pothiconOP2mo ago
.
JavaBot
JavaBot2mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
𝓐pothicon
𝓐pothiconOP2mo ago
.
JavaBot
JavaBot2mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
𝓐pothicon
𝓐pothiconOP2mo ago
.
JavaBot
JavaBot2mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
𝓐pothicon
𝓐pothiconOP2mo ago
.
ayylmao123xdd
ayylmao123xdd2mo ago
148 messages is crazy anyway idk much about cameras in java but u could just not use the axis for roll i think its y that should technically fix it lol or it was z not sure now try both of these
JavaBot
JavaBot2mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
𝓐pothicon
𝓐pothiconOP2mo ago
Im already only rotating with two axis It just ends up rotating on 3 because the 2nd rotation is relative to the 1st or smth like that
JavaBot
JavaBot2mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
𝓐pothicon
𝓐pothiconOP2mo ago
.
ayylmao123xdd
ayylmao123xdd2mo ago
are u sure ur using the right parameters degrees not radians or vice versa try to use degrees instead of radians
JavaBot
JavaBot2mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
𝓐pothicon
𝓐pothiconOP2mo ago
Someone else verified that I was using euler angles The problem is just the rotate methods are all relative to existing rotations so you cant rotate on two axes without rotating on three And idk how to make a method to either rotate non-relatively or remove the roll after-the-fact
ayylmao123xdd
ayylmao123xdd2mo ago
what you could also do is apply the rotation twice in separate methods you might have to use some other method than the one that sets stuff itself btw so it sets only 1 value regardless
JavaBot
JavaBot2mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
𝓐pothicon
𝓐pothiconOP2mo ago
yeah ive tried that tried basically every rotation method thats built-in
Daniel Santos
Daniel Santos2mo ago
in the rotate method set the rotate angle in just one axis. If Z is up set the up vector to (0, 0, 1) and rotate by an angle (are you using OpenGL ?) And if there is a translation do it after the rotation
JavaBot
JavaBot2mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
𝓐pothicon
𝓐pothiconOP2mo ago
setting the up vector doesnt prevent roll sadly
𝓐pothicon
𝓐pothiconOP2mo ago
No description
𝓐pothicon
𝓐pothiconOP2mo ago
it just makes the camera controls very weird (And im already rotating by angles)
ayylmao123xdd
ayylmao123xdd2mo ago
have you thought of doing this finalrotation = rotationx * rotationy that should work or x and z i forgot which one is for roll
ayylmao123xdd
ayylmao123xdd2mo ago
Stack Overflow
Quaternion-based camera unwanted roll
I created a camera based on quaternions, but when I turn the camera, an unwanted roll appears. I would not like to lose my freedom of movement using, for example, Euler angles, since there is a ne...
ayylmao123xdd
ayylmao123xdd2mo ago
check here
JavaBot
JavaBot2mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
𝓐pothicon
𝓐pothiconOP2mo ago
viewMatrix.rotate(new Quaternionf().rotateX(x).mul(new Quaternionf().rotateY(y)));
viewMatrix.rotate(new Quaternionf().rotateX(x).mul(new Quaternionf().rotateY(y)));
it doesnt introduce any weird issues, but it doesnt actually change any behavior either (theres still roll) OHHH I GOT IT after 21 days this was the solution:
import org.joml.*;

public class Camera {
private final Matrix4f viewMatrix = new Matrix4f().setTranslation(new Vector3f(100, 150, 100));
public Quaternionf pitch = new Quaternionf();

public Matrix4f getViewMatrix() {
return new Matrix4f(viewMatrix).rotate(pitch);
}
public void rotate(float x, float y) {
pitch.rotateX(x);
viewMatrix.rotateY(y);
}
public void move(float x, float y, float z, boolean countRotation) {
if (countRotation) {
viewMatrix.translate(x, y, z);
} else {
Vector3f prevTranslation = new Vector3f();
viewMatrix.getTranslation(prevTranslation);
viewMatrix.setTranslation(new Vector3f(x+prevTranslation.x, y+prevTranslation.y, z+prevTranslation.z));
}
}
}
import org.joml.*;

public class Camera {
private final Matrix4f viewMatrix = new Matrix4f().setTranslation(new Vector3f(100, 150, 100));
public Quaternionf pitch = new Quaternionf();

public Matrix4f getViewMatrix() {
return new Matrix4f(viewMatrix).rotate(pitch);
}
public void rotate(float x, float y) {
pitch.rotateX(x);
viewMatrix.rotateY(y);
}
public void move(float x, float y, float z, boolean countRotation) {
if (countRotation) {
viewMatrix.translate(x, y, z);
} else {
Vector3f prevTranslation = new Vector3f();
viewMatrix.getTranslation(prevTranslation);
viewMatrix.setTranslation(new Vector3f(x+prevTranslation.x, y+prevTranslation.y, z+prevTranslation.z));
}
}
}
the camera matrix stores position & yaw and theres a seperate pitch quaternion that is rotated with the matrix when you do getViewMatrix() peak camera class
JavaBot
JavaBot2mo ago
Post Closed
This post has been closed by <@465515365108154380>.

Did you find this page helpful?