0
votes

I have a very simple 3D scene - single object is laying in the middle of coordinate center. Not rotate, not translated. I.e. a watch laying on some table. Camera is straigh above it, and looking directly into it. Everything is displayed well so far.

Now, what I try to accomplish is to read phone sensors for rotating device and rotate camera around the object based on those values. I'm reading those values like:

    float xRotation = Gdx.input.getAccelerometerX()*9;
    float yRotation = Gdx.input.getAccelerometerY()*9;

And I'm getting ok values. xRotation is value of device rotation left / right and yRotation of rotatin up / down.

Then I'm using those values to rotate camera around the object. Since I don't know how to read current camera angle I'm saving it and every time I change rotation I first do negative rotation by saved value and then do the new rotation. That also works well:

    float currentAngle = cameraYAngle + (angle - cameraYAngle) * (Gdx.graphics.getDeltaTime()*5);
    cam.rotateAround(target, Vector3.X, -cameraYAngle); // returning camera back to 0 angle
    cameraYAngle = currentAngle;  // saving new angle
    cam.rotateAround(target, Vector3.X, currentAngle); // setting new angle

So I'm kinda slowing down movement to avoid twitching (which can be ignored for this issue). Similar code is for second (Z) axis. So rotation is working pretty well like this, except one part:

If I move device in circles (i.e. like phone is laser pointer and I'm drawing circles on some wall) if I do it clockwise I get rotation around 3rd axis in one direction ?!? If I do that in counter clockwise direction I get 3rd axis rotation in other direction?!?

So I'm making rotation around X axis to make up/down rotation and around Z axis to get rotation left/right. I'm not rotating around Y axis any time, but combination of those 2 somehow affect rotation around Y axis as well?!?

That is, if camera is rotated around i.e. X and then rotated around Z somehow rotation around Y axis is also made. When I rotate back (by using saved values) to 0,0 rotation that I didn't made, around Y axis stays.

I have feeling that it's not coding problem, but that I don't understand fully how 3D rotation is made.

1

1 Answers

0
votes

Ok, found how to fix that behavior. With every new frame I was:

reverting Z rotation -> making new Z rotation -> reverting X rotation -> making new X rotation

So I had revert, rotate, revert, rotate and when ever I do new rotation other was already applied.

Now I changed it to:

reverting Z rotation, reverting X rotation, making X rotation, making Z rotation

So if I rotate backward in opposite order than I made rotation it works well. After resetting 2 times at start I get back to 0,0,0 for each frame.

I assumed that if values of reverting rotation by axis are the same I'll get original 0,0,0 position, but it's not the case. Order of applying rotation is also important.