Need Pong AI help

BlitzPlus Forums/BlitzPlus Beginners Area/Need Pong AI help

Jester(Posted 2007) [#1]
I'm having 2 troubles with the following code. First the AI bat will move faster than the speed I have specified. And second it always hits the ball.


If I created a timer to slow down the executuion of the code would that help or am I totally off the mark here?

I have Seedrnd Millisecs() at the beginning with the graphics command,etc..

Thanks


Siopses(Posted 2007) [#2]
Why do you have a hit chance?


El Neil(Posted 2007) [#3]
im not sure about your first question, but for the 2nd:

how big is the bat? by the looks of it you have a screen 600 high, but the playing field limits are restricted to 55-525. even if the bat was as far away as possible (475 pixels) it would only take 118 frames at 4 pixels per frame to make it to the ball. at 60fps (on an average pc your game should be able to do that) this is just under 2 seconds, which means that the bat will make it in all but a few circumstances. these few circumstances are usually covered by the size of the bat. so every time the ball is hit, the bat is pretty much guaranteed to get there in time. problem 1: the bat is too good.

the other problem is in your checking. "if the bat has made it to the ball, move it 4 pixels away 75% of the time". fair enough, but if the bat was close to the ball in the first place, it will move away early but then try and find the ball again on the next frame. this will cause the bat to move 4 pixels at a time back and forth until the ball gets near, wherupon the bat makes a last attempt at moving out of the way by 4 pixels (75% of the time). problem 2: too little, too late. even if the bat does move, if it is bigger than 8 pixels it will hit the ball.


heres how i would solve it:
when the ball is hit by the player, determine the angle at which it was hit. from this angle, work out where the AI bat will need to be to hit the ball perfectly. you will need to allow for bouncing off the top and bottom edges - i'll leave this for you to calculate cos ive had a few beers and cant do it myself. if you're stuck, ask me tomorrow and ill help you.
once you know where the ball will end up, give the cpu bat a target destination, plus or minus a margin of error depending on the cpu skill level. all you have to do then is similar to what you did before:

if cpu_bat_ypos < target_ypos then

cpu_bat_ypos = cpu_bat_ypos + move_speed

else if cpu_bat_ypos > target_ypos then

cpu_bat_ypos = cpu_bat_ypos - move_speed

else

end if



all you have to do is simulate what would happen in a real game of tennis. you work out where you think the ball will end up and run there as fast as you can. if youre crap at judgement, you'll miss by miles, otherwise you'll get there 90% of the time.

neil


El Neil(Posted 2007) [#4]
the following code will create a while...wend timer in your game. adjust the FPS_LIMIT constant to, er, change the fps limit. creating a timer should make the bats move at a capped speed, providing your system is up to that speed. if they are still moving at different speeds the problem is elsewhere.

; somewhere at top of code...
const FPS_LIMIT = 60     ;"ish"
global oldtime


;top of main loop
oldtime = millisecs()




;absolute last thing in main loop...
While (MilliSecs()<oldtime + (1000/fps))
Wend





have fun

neil


Jester(Posted 2007) [#5]
Thanks for the help. I feel like such an idiot. All I had to do was change the speed that the bat moved to keep it from hitting the ball everytime. DOH!


El Neil(Posted 2007) [#6]
dont sweat it - we all come across these problems from time to time. good luck for the rest of the game

neil


Jester(Posted 2007) [#7]
Thank you.