JavaFX旋转相机围绕一个枢轴 [英] JavaFX Rotating Camera Around a Pivot

查看:513
本文介绍了JavaFX旋转相机围绕一个枢轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

去年我发布了一个



这就是我喜欢它们的方式:



这是我用来计算角度的代码的一部分(虽然根据我的观察,它似乎与我的问题无关):

  //变量angleY和angleX根据屏幕上的鼠标拖动计算
rotateY.setAngle(angleY);
rotateX.setAngle(angleX * Math.cos(Math.toRadians(angleY)));
rotateZ.setAngle(angleX * Math.sin(Math.toRadians(angleY)));

为了方便测试,我上传了 Pastebin上的代码



编辑:我可以通过旋转所有内容来实现同样的效果,但相机(其中)这正是相机运动所做的事情)并使用x轴进行提升,但我正在寻找更优雅的解决方案。

解决方案

我决定使用上面提到的解决方案。我为每个元素创建了一个组,但是相机然后在y轴上旋转了元素。对于高程(x轴),我旋转相机。对我的问题来说,这是一个非常好的解决方案。



编辑:由于这不是我上面提到的问题的答案,我决定找出答案真的出了什么问题。我正在玩变换,发现它们按照你将它们添加到变换数组的顺序应用。真正的问题是我在XYZ-order中应用了旋转,因此x旋转在y旋转之前完成,因此它与它无关。更改从XYZ到YXZ的转换顺序修复了所有问题。


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.

解决方案

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.

这篇关于JavaFX旋转相机围绕一个枢轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆