【问题标题】:Calculate target range on circle Three.js计算圆圈 Three.js 上的目标范围
【发布时间】:2017-07-02 03:12:16
【问题描述】:

我遇到了一道数学题。

我有一个特定的弧度,我想将相机指向 (camera.rotation.y)。我想告诉用户他们是否需要向左或向右平移才能达到该弧度,容差为 PI/4。

我已经通过让相机 + 小车旋转来标准化平移编号

function getCurrentPan(){
    return dolly.rotation.y + camera.rotation.y > 0
        ? (dolly.rotation.y + camera.rotation.y)%Math.PI
        : ((dolly.rotation.y + camera.rotation.y)%Math.PI) + Math.PI;
}

所以它总是在 0 和 PI 之间。

我计划计算出可以检查的范围,以查看用户是否需要向左或向右平移才能达到该弧度。试试这段代码:

      point.leftRange = {};
        point.leftRange.min = point.pan - range > 0 ? point.pan - range : Math.PI - point.pan - range;
        point.leftRange.max = point.leftRange.min - Math.PI/2 - range > 0 ? (point.pan - range)%Math.PI : (Math.PI - (point.leftRange.min - Math.PI/2 - range))% Math.PI;

        console.log(point.leftRange);

        point.rightRange = {};
        point.rightRange.min = (point.pan + range) % Math.PI;
        point.rightRange.max = (point.rightRange.min + (Math.PI/2 - range)) % Math.PI;

        console.log(point.rightRange);

这给了我类似的东西

  • point.pan 1.464308692701496
  • left.min 1.071609611002772
  • left.max 0.8918857974908487
  • right.min 1.8570077744002202
  • right.max 3.0351050194963927

我尝试过的另一种方法是

             var opp = (point.pan - Math.PI/2)%Math.PI;
                opp = opp > 0 ? opp : Math.PI - opp;

                if(getCurrentPan() > point.pan && getCurrentPan() < opp)
                     console.log('greater');
                else if(getCurrentPan() < point.pan && getCurrentPan() > opp)
                     console.log('less');

如果 pan 像 0.5 并且左范围是 3 等(PI 的另一边),这也不会考虑。

抱歉,我没有很好地解释这一点,但如果有人能指出正确的方向,我将不胜感激!

【问题讨论】:

    标签: javascript math three.js radians


    【解决方案1】:

    这是过度设计的。如果您想确定目标是在相机的左侧还是右侧,请执行以下操作:

    d = camera.getworlddirection();
    diff = target.position - camera.position;
    theta = atan2(diff.x,diff.z) - atan2(d.x,d.z);
    

    这是您的相机正在观看的位置与对象相对于相机的位置之间的角度。

    所以:

    if(abs(theta) < someThreshold){
      if(theta > 0){
        //its to the right
      }
      else{
       //its to the left
      }
    }
    

    【讨论】:

    • 谢谢。这很有用。我遇到的问题不是它是一个物体……但这可能对此有好处-更多的是存储的视角。如果我不能根据角度来做,我也许可以在那个角度添加一个隐藏的对象来使用它..
    • target. positioncamera.position 的类型是vector3 的类型,我出错了。
    • “我遇到错误”可能意味着很多事情。位置是 THREE.Vector3 是的。控制台输出说什么?如果您有问题,请开始一个新问题。
    猜你喜欢
    • 1970-01-01
    • 2020-10-28
    • 1970-01-01
    • 2021-07-30
    • 1970-01-01
    • 1970-01-01
    • 2017-01-13
    • 2012-05-28
    相关资源
    最近更新 更多