Search code examples
javaopengl3dprocessing

How to rotate an object in processing according to yaw, pitch and roll values


I am trying to rotate an object which was imported as .obj file according to pitch and roll values.

I actually achieved this with this code

object.rotateX(ConvertToRadians(pitch));

object.rotate(ConvertToRadians(roll), 0, 0, 1); // This line is basically the same thing with rotateZ function. I used it because rotateZ gives a weird error.

However, when new pitch and roll values arrive with the socket connection and I use these lines of code. It adds to the previous rotation. To give an example, when the object is rotated around X-axis for 30 degrees and I try to make it rotate to 10 degrees. It rotates it to 40 degrees.

I tried saving the previous rotation and undoing it before giving the new rotation with this line

pieta.rotateX(ConvertToRadians(pitch-prevpitch));

It seems to work partially but after some time the original position (pitch=0 and roll = 0 is not flat anymore. It tilts to some angle. I don't know the reason). I think resetting the object's position to the original will work.

How can I achieve my aim?

This is my full code if you want to recreate it Project File


Solution

  • Use resetMatrix() to setup a completely new matrix in every frame:

    object.resetMatrix();
    object.rotateX(ConvertToRadians(pitch));