Pong paddle AI?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Nexion
Prole
Posts: 46
Joined: Fri Sep 05, 2008 1:40 pm

Pong paddle AI?

Post by Nexion »

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

Code: Select all

        
        local diff = math.abs(bBody:getY()-pPadBody:getY())
	local xVel, yVel = bBody:getVelocity()
	cPadBody:applyImpulse(0,diff^2 * 100 * (yVel / 10))
Where bBody is the ball and pPadBody is my paddle.

Can anyone thing of a good way to automatically move the paddle to the ball without it being TOO exact so you can never score?
User avatar
qubodup
Inner party member
Posts: 775
Joined: Sat Jun 21, 2008 9:21 pm
Location: Berlin, Germany
Contact:

Re: Pong paddle AI?

Post by qubodup »

when the paddle is 40 pixels wide

Code: Select all

+ math.random(41) - 20
?
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)
User avatar
Nexion
Prole
Posts: 46
Joined: Fri Sep 05, 2008 1:40 pm

Re: Pong paddle AI?

Post by Nexion »

Huh? There's nothing to do with the paddle width. I'd have to incorporate the height into it if anything.
User avatar
cag
Citizen
Posts: 65
Joined: Sun Jun 29, 2008 5:09 am

Re: Pong paddle AI?

Post by cag »

what does it do with the current code?
User avatar
Nexion
Prole
Posts: 46
Joined: Fri Sep 05, 2008 1:40 pm

Re: Pong paddle AI?

Post by Nexion »

I changed up the code a little and it works a LITTLE better but its still not quite where I'd like it to be

Code: Select all

	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)
User avatar
qubodup
Inner party member
Posts: 775
Joined: Sat Jun 21, 2008 9:21 pm
Location: Berlin, Germany
Contact:

Re: Pong paddle AI?

Post by qubodup »

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)
MrPickle
Prole
Posts: 24
Joined: Thu Jul 10, 2008 11:43 pm
Location: United Kingdom, Lincolnshire

Re: Pong paddle AI?

Post by MrPickle »

Here's how I controled the AI paddle in my pong game:

Code: Select all

-- 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
TacticalPenguin
Prole
Posts: 15
Joined: Thu Dec 11, 2008 5:44 am

Re: Pong paddle AI?

Post by TacticalPenguin »

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. "
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Semrush [Bot] and 65 guests