A.I using ray lines

Started by
1 comment, last by vomiting_goblin 19 years, 10 months ago
Hi all, I just started with c++ at the start of this year, and I''ve been working my way through making a PONG kind of game. At the moment my AI is extremely simple. If the ball is to the left of the A.I paddle, move left. If its to the right, move right (although the A.I also knows where it should hit the ball to make it hardest for the player). The problem with doing the A.I this way, is that the A.I can''t predict if the ball is going to bounce off the walls or not. I was told that the best way of doing PONG A.I was to draw a ray line in the direction of the balls velocity (which will reverse its X velocity if it hits the wall), and make the A.I move to the spot where the ray line intersects with the A.I paddles line of movement. Is this right? If so, how should I do it? I dont know where to start! Any help would be really good. Thanks. Ewan Short
Ewan Short
Advertisement
Ok, your ball has velocity (vx,vy) and you get the reflected velocity (vx,-vy) (assuming Y is the vertical axis in the classic pong). Now, your top wall is at y1=0 and your bottom horizontal wall is at say, y2=1.0. With vy, you know if your ball is moving to the top or bottom wall. You need to find the intersection of the ball with the wall. It''s a simple line equation in 2D, if your ball is at (x,y) and the intersection is (cx,cy), you can write :

cx = x + vx*t
cy = y + vy*t

You need to solve ''t'' when cy=0.0 (if top wall) or cy=1.0 (if bottom wall). Ok, with the bottom wall :

cx = x + vx*t
0 = y + vy*t ==> (-y/vy) = t

Just replace the t in cx and you''ll get the collision with the wall. Now with the reflected vector, you can perform the same stuff but with vertical axis this time (cx=0.0 or cy=1.0).


//assuming the AI controlled paddle is at the topp = ball’s positionv = ball’s velocitywhile ray p,v does NOT intersect the segment representing the AI paddle’s movement{ if ray p,v intersects the segment representing the player paddle’s movement {	p = intersection point of ray p,v and the segment representing the player paddle’s movement	v.y = - v.y } else if ray p,v intersects the segment representing the left wall {	p = intersection point of ray p,v and left wall	v.x = - v.x } else {	p = intersection point of ray p,v and right wall	v.x = - v.x }}p = intersection point of ray p,v and the segment representing the AI paddle’s movement//the AI paddle should now move toward point p


How does that look? I'm sure you can find some ray - segment or segment - segment intersection code somewhere in the Maths and Pysics forum to deal with the necisary intersection tests (or I can give you mine).
Having the AI paddle aim to a position away from the oposing paddle is slightly more difficult.
Oh, and by the way, I didn't quite say that this method is the *best* way to do it. I'm sure there are many ways to program pong AI. I merely expressed the first logical idea that came to mind - which should work perfectly fine, anyhow.
Talk to you later .

[edited by - jack_1313 on June 2, 2004 6:23:45 AM]

This topic is closed to new replies.

Advertisement