JavaFX Rotating Camera Around a Pivot

ぃ、小莉子 提交于 2019-12-12 16:32:07

问题


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:

This is 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:


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.



来源:https://stackoverflow.com/questions/48948092/javafx-rotating-camera-around-a-pivot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!