简体   繁体   English

当以特定角度转弯时,什么可能导致此“无人机”抖动?

[英]What could be causing this “drone” to be jittery when turning at certain angles?

http://codepen.io/AlexKM/pen/JGEMjZ http://codepen.io/AlexKM/pen/JGEMjZ

turnTo: function (X, Y) {
    var angleDiff = 0,
        targetA = this.angleToPoint({
            x: X,
            y: Y
        }),
        dronA = this.angle;
    ///   If the angle difference can't be reached in a frame
    if (Math.abs(targetA - dronA) > this.turnSpeed * 1.25) {
        this.workingAngle = true;
        angleDiff = targetA - dronA;
        while (angleDiff < 0)
            angleDiff += 2 * Math.PI;
        while (angleDiff > 2 * Math.PI)
            angleDiff -= 2 * Math.PI;

        if (angleDiff >= Math.PI) {
            this.turningLeft = true;
        } else {
            this.turningRight = true;
        }

    } else ///  if the diff is negligible, reach it to save CPU
        this.angle = targetA;
},

Update function snippet dealing with actual turning: 更新处理实际转向的功能代码段:

    // apply turning
    if (this.turningLeft) {
        this.angle -= this.turnSpeed;
    }
    else if (this.turningRight) {
        this.angle += this.turnSpeed;
    }

Red dot - drone facing 红点-无人机面对
Orange dots - signal if the drone is turning left or right 橙色点-指示无人机是否向左或向右旋转
Cyan dot - signal if the drone is recalculating angles/doing trigonometry 青色点-表示无人机是否正在重新计算角度/正在执行三角测量

The code DOES contain a part which helps it smooth out, by basically simply setting to the mouse's angle if it can be reached within a frame from the testDrone.turnSpeed variable. 如果确实可以在testDrone.turnSpeed变量的一帧内到达鼠标角度,则代码DOES包含一个有助于平滑的部分。

About half the time, it turns and works smoothly. 大约有一半的时间,它会转动并正常工作。 The other half, it goes jittery, alternatively turns left and right and continuously calculates trig. 另一半抖动,交替向左和向右旋转并连续计算触发。

What could be the reason for this? 这可能是什么原因?

Nice script. 不错的脚本。 :) :)

I think the jitteriness may be a function of the difference between the (framerate ( fps ) + processing time) and the refresh rate of the monitor. 我认为抖动可能是显示器的(帧速率( fps )+处理时间)与刷新率之间差异的函数。 I say this because sometimes it seems smooth and sometimes it goes a bit lumpy, and there doesn't seem to be a particular pattern about that as far as mouse position/movement goes. 我之所以这样说,是因为有时它看起来很平滑,有时又有些肿块,而且就鼠标位置/移动而言,似乎并没有什么特别的模式。

Have you thought about using requestAnimationFrame() over setTimeout() ? 您是否考虑过在setTimeout()使用requestAnimationFrame() setTimeout() See MDN HERE , which states... 请参阅MDN HERE ,其中指出...

This will request that your animation function be called before the browser performs the next repaint. 这将要求您的动画函数在浏览器执行下一次重新绘制之前被调用。 The number of callbacks is usually 60 times per second, but will generally match the display refresh rate in most web browsers as per W3C recommendation. 回调次数通常为每秒60次,但按照W3C的建议,通常将与大多数Web浏览器中的显示刷新率匹配。

See markE 's response to this StackOverflow question about throttling requestAnimationFrame calls to a particular framerate. 请参阅markE 对此StackOverflow问题的答复, 该问题涉及如何限制对特定帧速率的requestAnimationFrame调用。

Hope that helps. 希望能有所帮助。 :) :)

What fixed it was making sure the angles are actually RESET to certain values rather than being inc/decremented by Pi when they go past a certain limit. 固定问题是确保角度实际上已重置为某些值,而不是在超过某个限制时被Pi增加或减少。 It was a design fault on my part, since the jitter elimination didn't work for angles "above" Pi or "below" Pi 这是我的设计失误,因为对于“高于” Pi或“低于” Pi的角度,消除抖动不起作用

This was meant to go in the update function: 这是为了进入更新功能:

update: function (ctx) {
    // reset angles below and above 2pi
    while (this.angle > Math.PI)
        this.angle = -Math.PI;
    while (this.angle < -Math.PI)
        this.angle = Math.PI;

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

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