Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Opengl 围绕对象旋转_Opengl_Rotation_Jogl_Coordinate_Orbit - Fatal编程技术网

Opengl 围绕对象旋转

Opengl 围绕对象旋转,opengl,rotation,jogl,coordinate,orbit,Opengl,Rotation,Jogl,Coordinate,Orbit,我一直在尝试让一个物体围绕另一个物体旋转: //childX,childY,childZ are my starting coordinates //here I count distance to the middle of my coordinate plane float r = (float) Math.sqrt(Math.pow(childX, 2)+Math.pow(childY, 2)+Math.pow(childZ,2)); //here i convert my angles

我一直在尝试让一个物体围绕另一个物体旋转:

//childX,childY,childZ are my starting coordinates
//here I count distance to the middle of my coordinate plane
float r = (float) Math.sqrt(Math.pow(childX, 2)+Math.pow(childY, 2)+Math.pow(childZ,2));

//here i convert my angles to radians
float alphaToRad = (float) Math.toRadians(findParent(figure.parentId).rotate[1]);//up_down
float betaToRad = (float) Math.toRadians(findParent(figure.parentId).rotate[0]);//left_right


float newX = (float) (r*Math.cos(betaToRad)*Math.cos(alphaToRad));
float newY = (float) (r*Math.cos(betaToRad)*Math.sin(alphaToRad));
float newZ = (float) (r*Math.sin(betaToRad));'
我有起点的坐标(5,5,0)和角度0°和0°,所以这意味着,在计算新的坐标后,坐标不应该改变。但结果是:

newX: 7.071068 newY: 0.0 newZ: 0.0
我试图计算新坐标的每一种方法都会出现这种奇怪的结果。7.07是什么?我怎样才能得到正确的结果

@编辑

为了使我的新点相对于旧点,我只是将旧点的角度添加到新点:

float alphaToRad = (float) Math.toRadians(findParent(figure.parentId).rotate[1]) + Math.atan(childY/childX);
float betaToRad = (float) Math.toRadians(findParent(figure.parentId).rotate[0]) + Math.asin(childZ/r);

现在一切正常。已求解的7.07是代码中
r
的值,即点到原点的距离:

sqrt(5 * 5 + 5 * 5) = sqrt(50) = 7.0711
当两个角度都为零时,所有的
cos()
值将为1.0,而
sin()
值将为0.0。这意味着
newX
变为
r
,为7.07,
newY
newZ
都变为0.0。这正是你得到的结果,所以这个结果并不神秘

基本上,你要做的就是把点放在一个给定的方向上,并与原点保持一定的距离。该距离与原始距离相同。方向由两个角度给出,其中两个角度均为0.0,对应于x轴方向

换句话说,缺少的是没有考虑点相对于原点的原始方向。根据两个角度,将点放置在绝对方向,而不是相对于点的原始方向


要按给定角度旋转点,最简单的方法是从这些角度构建旋转矩阵,并将它们应用于点。

谢谢您的帮助。我不必制作旋转矩阵,我只是在新角度上添加了旧角度,效果很好。