Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

artificial intelligence - Paddle 2 in my Pong AI is jittering, but it isn't chasing the ball. It's moving to a static value. Why is this happening?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...