JavaFX Rotating Camera Around a Pivot

2019-07-14 18:34发布

Last year I posted a similar question, but due to it not being very descriptive and overall a mess I decided to rewrite it completely.

I am making a simple 3D editor in JavaFX that has a camera movement similar to Blender or pretty much any other one. I added the rotation around the x-axis and then using trigonometric functions I calculated the other 2 rotations (y and z-axis). Both x and y-axis rotations are made relative to the pivot point, but the z rotation is always made relative to the screen/camera, not its pivot point.

For a better understanding of the problem, I sketched where the axis is and where I'd like to have them:

This is how angles are applied at the moment: Current axis

This is how I'd like them to be: How I'd like them to be

Here is the part of the code I use to calculate the angles (although from my observations it seems irrelevant to my problem):

//variables angleY and angleX are calculated according to mouse drags on the screen
rotateY.setAngle(angleY);
rotateX.setAngle(angleX*Math.cos(Math.toRadians(angleY)));
rotateZ.setAngle(angleX*Math.sin(Math.toRadians(angleY)));

To make testing easier, I have uploaded the code on Pastebin.

Edit: I could achieve this same effect by rotating everything, but the camera (which is exactly what the camera movement does anyway) and using x-axis for elevation, but I'm looking for a more elegant solution.

1条回答
迷人小祖宗
2楼-- · 2019-07-14 18:54

I decided to use the solution I mentioned above. I created a group for every single element, but the camera and then rotated the elements on the y-axis. For elevation (x-axis), I rotate the camera. It turned out to be a very good solution for my problem.

Edit: As this isn't really an answer to the question I asked above, I decided to find out what really went wrong. I was playing around with the transformations and found out that they get applied in the same order you add them to the transformations array. The real issue was that I applied rotations in the XYZ -order, therefore the x-rotation was done before the y-rotation, so it wasn't relative to it. Changing the order of transformations from XYZ to YXZ fixed all the issues.

查看更多
登录 后发表回答