I have most of my pong base done, I just need to get some decent paddle movement down.
I have yet to come across a decent combination of GetVelocity and the difference between the Y values of the ball and the paddle to get it to work decently.
I've tried things along the lines of
lg.newImage("cat.png") -- made possible by lg = love.graphics
-- Don't force fullscreen (it frustrates those who want to try your game real quick) -- Develop for 1280x720 (so people can make HD videos)
local diff = math.abs(bBody:getY()-cPadBody:getY())
local xVel, yVel = bBody:getVelocity()
cPadBody:applyImpulse(0,diff * 200 * yVel - 50) --Difference in Y's * the balls Y velocity * scalar adjustment - 1/2 of the paddles height
I noticed a syntax error which really screwed with things before which is why it wasn't working.
With that it follows the ball pretty well, until it has a rapid change in direction (such as when you hit it against a wall)
I mean that you should add my code above to the end position of the paddle, so that there is a 2 in 42 chance that it will be 1 px too far from the ball
lg.newImage("cat.png") -- made possible by lg = love.graphics
-- Don't force fullscreen (it frustrates those who want to try your game real quick) -- Develop for 1280x720 (so people can make HD videos)
-- If ball is below paddle, move down
local ComDelta_y = Bars["Computer"].y
if Ball.y < Bars["Computer"].y+75 then -- 75 Was the height
Bars["Computer"].y = Bars["Computer"].y - (Ball.Speed/2)*dt
if Bars["Computer"].y < 0 then
Bars["Computer"].y = ComDelta_y
end
end
--If ball is above paddle, move up
if Ball.y > Bars["Computer"].y+75 then
Bars["Computer"].y = Bars["Computer"].y + (Ball.Speed/2)*dt
if Bars["Computer"].y > 450 then
Bars["Computer"].y = ComDelta_y
end
end
Sorry to bump up such an old (oh whoohoo 2.5 months eh maybe not THAT old) thread but I remembered a pong AI outline I made a while back. It is not PERFECT, but the perfect that is better than it requires a hell of a lot more work and only gets a percent or two better. this won't account for a situation where, say, there are three balls coming at you and you could line up to block either one of the first two, but if you block the first one you cant hit the 2nd or 3rd while if you blocked the 2nd one you could block the 3rd as well; it will slip at that, but that rarely happens. in general it works well.
copypaste time
"First let me say that very good very complete multiball pong AI that takes all possible variables into effect requires a good bit of work/thinking. However, for easier AI that still works well, I would follow this logic pattern:
1-Find out which balls are moving toward the AI player
2-Calculate an estimated frames to arrival for each by taking the distance between it and the AI player and dividing that by the X movement speed of the ball
3-Starting with the ball with the lowest EFA, find its EYA - estimated Y position at arrival. Make sure to account for bouncing off walls.
4-Check if it is possible for the AI player to move from its current Y to a Y where the paddle will block the ball in a number of frames equal to or less than the ball's EFA
5-If yes, this is the ball you want to attempt to block - save it's table index as the ball being blocked for the movement part to move toward
6-If no, repeat from step 3 for the ball with the next lowest EFA
7-Once the ball to block has been identified, move the AI player to the place it needs to be to block the ball
8-When that ball bounces off the AI player, repeat steps 1-7
This will account for and ignore balls that will arrive too soon to be blocked and will also attempt to block the ball that will arrive soonest rather than the ball that is closest in distance. "