简体   繁体   English

JavaFX围绕轴旋转相机

[英]JavaFX Rotating Camera Around a Pivot

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. 我正在用JavaFX开发一个简单的3D编辑器,它的摄影机动作类似于Blender或几乎其他任何动作。 I added the rotation around the x-axis and then using trigonometric functions I calculated the other 2 rotations (y and z-axis). 我添加了绕x轴的旋转,然后使用三角函数计算了另外2个旋转(y和z轴)。 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. x和y轴旋转均相对于枢轴点进行,但z旋转始终相对于屏幕/相机而不是其枢轴点进行。

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. 为了简化测试,我将代码上传 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. 编辑:我可以通过旋转所有东西来实现相同的效果,但是要旋转相机(无论如何,这正是相机的运动),并使用X轴进行标高,但是我正在寻找更优雅的解决方案。

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. 我为每个单个元素创建了一个组,但是对了相机而言,然后在y轴上旋转了这些元素。 For elevation (x-axis), I rotate the camera. 对于仰角(x轴),我旋转相机。 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. 真正的问题是我以XYZ顺序应用旋转,因此x旋转在y旋转之前完成,因此它不是相对于它的。 Changing the order of transformations from XYZ to YXZ fixed all the issues. 将转换顺序从XYZ更改为YXZ可以解决所有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM