我的 Pong AI 中的 Paddle 2 抖动,但它没有追球它正在转向一个静态值为什么会这样?

问题描述

需要说明的是,我的 AI 功能正常。它正在做它应该做的事情,我只是无法摆脱桨抖动。以前看过有人回答过这种问题,不过好像总跟球拍追球有关系。这不是我的 AI 所做的。它在自身和球最近的偏转点之间执行中点外推,并且该外推存储在 Y_commit 变量中:

Y_commit = (ball.y * 2) - refl_Y

这个值永远不会改变,直到有另一个偏转。桨没有追逐任何东西。它应该移动到那个位置并停留。然而,出于某种原因,它从那个位置来回抖动了大约 +2/-2。

起初,我认为这可能与函数开头的错误中点条件有关。但是我已经注释掉了,我仍然得到相同的结果。谁能解释一下?

我的代码

function AI(refl_X,refl_Y)
    -- If deflection coordinates are even,offset by 1 to create a false midpoint
    if refl_X % 2 == 0 then
        refl_X = refl_X + 1
    end

    if refl_Y % 2 == 0 then
        refl_Y = refl_Y + 1
    end

    -- If the ball deflects from a surface,recalculate midpoint extrapolation,toggle detectRefl
    if detectRefl then
        --IF ball.x is half way between refl point and paddle.x THEN
        if ball.x >= ((refl_X + paddle2.x) / 2) + 1 then
            -- Find projected endpoint of the ball's path
            Y_commit = (ball.y * 2) - refl_Y
            -- If the ball has been returned enough times,approximate Y_commit
            if returnServe >= returnLimit then
                Y_commit = math.random(Y_commit+5,Y_commit-25)                
            end
            -- Toggle deflection detection trigger
            detectRefl = false
        end
    end
    
    -- Move paddle2 to Y_commit position,hold if position is reached
    if paddle2.y ~= Y_commit then
        if paddle2.y >= Y_commit then
            paddle2.dy = -PADDLE_SPEED
        elseif paddle2.y <= Y_commit then
            paddle2.dy = PADDLE_SPEED
        end
    elseif paddle2.y == Y_commit then
        paddle2.dy = 0
    end
end

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)