To be clear, my AI functions correctly. It's doing what it's supposed to do, I just can't get rid of the paddle jitter. I've seen this kind of question answered before, but it always seems to have something to do with the paddle chasing the ball. And that isn't what my AI does. It performs a midpoint extrapolation between itself and the ball's most recent deflection point, and that extrapolation is stored in the Y_commit
variable:
Y_commit = (ball.y * 2) - refl_Y
This value never changes until there's another deflection. The paddle isn't chasing anything. It should move to that location and stay. Yet, for some reason, it jitters back and forth off of that position by about +2/-2.
At first, I thought it might have been to do with the false midpoint condition at the beginning of the function. But I've commented that out, and I still get the same result. Can anyone please explain this?
My code:
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
question from:
https://stackoverflow.com/questions/65837022/paddle-2-in-my-pong-ai-is-jittering-but-it-isnt-chasing-the-ball-its-moving 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…