【问题标题】:rotating around objects围绕物体旋转
【发布时间】:2015-05-29 21:30:38
【问题描述】:

我一直试图让一个物体绕另一个物体运行:

//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);

现在一切正常。解决了

【问题讨论】:

    标签: opengl rotation jogl coordinate orbit


    【解决方案1】:

    7.07 是您代码中r 的值,即您的点到原点的距离:

    sqrt(5 * 5 + 5 * 5) = sqrt(50) = 7.0711
    

    两个角度都为零时,所有cos() 的值都将为1.0,sin() 的值为0.0。这意味着newX 变为r,即7.07,newYnewZ 都变为0.0。这正是你得到的,所以这个结果并不神秘。

    您基本上所做的是将点放置在距原点的给定方向和距离处。距离与原始距离相同。方向由两个角度给定,其中两个角度均为 0.0 对应于 x 轴方向。

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

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

    【讨论】:

    • 感谢您的帮助。我不必制作旋转矩阵,只需将旧角度添加到新角度即可。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-27
    • 2015-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多