开发者

How to extrapolate position in pong?

Ok I have another pong related question. Now I'm trying to improve "AI". I read on the internet that I should predict ball's x and y and move there paddle.

Heres my equations.

How to extrapolate position in pong?

开发者_如何学JAVA
y=ax+b
a1=(y1-y2)/(x1-x2) - a of the circles line, x1, y1 are taken before movent and x2 y2 after.
b1=y1-ax1

then I calculated coord for the line of paddle movement using constants like pos 0 0 and screen height, width.

To calculate point of intersetcion I made equation: a1x4+b1=a2x4+b2. a1 b1 b2 a2 are things I calculated before. And it doesnt work :P What's wrong ?


It seems like you are on the right track. Basically you are wanting to figure out where the balls position will be when it gets to where the paddle is.

You can do this easily by finding the x and y velocities and keeping track of where the upper and lower walls are.

To find the x and y velocity you basically find how far the ball traveled in the x direction and the y direction over set interval of time. So lets say that you find the location to be (100(x), 50(y)) at t = 0... you then see that at t = 1 the ball is at (110, 65) this means the ball is moving 10(pixels) per unit of time in the x direction and 15(pixels) per unit of time in the y direction.

So lets set our room size to use for reference. lets make our room 200(x) by 150(y) so the furthest paddle is at 200 and the closest one is at 0 on the x... and the base of the room is at 150 and the top is 0, on the y.

If your paddle is at x = 200, it will take (200-110)(pixels)/10(pixels per time) to get there. Now that you know how much time it will take for the ball to reach the paddle, you can figure out where the ball is on the y-axis. Using the paddle is at x=200, we find that it will take 9 more units of time to reach the paddle. So we simply multiply the amount of time by the rate of the change in the y direction to find how far the ball gos in the y direction. So with a quick calculation, we get that the ball moved 135(pixels) on the y axis.

But, we can look back and see that our room is only 150 pixels high. We started at y=65 and 65+135 is 200 which is greater than our limit of 150. So we must have a bounce in there. So we can find when the ball will have made it to y = 150 by simply using a linear equation in the form of y=mx+b. Where our m is the y speed and our b is the starting y point. X is measured in units of time and y is in our units of distance.

So we know our y where the collision happens is at y=150 so we plug that in along with m as our rate of travel and our b which is our original y position.

150=(15)(x)+65

We can quickly solve for x to get 5 and 2/3 units of time. So we can now subtract this from our total time till we hit the paddle (9 units)-(5 and 2/3 units) = (3 and 1/3 units of time). After our bounce, we will now have a negative y velocity but at the same speed assuming a simple bounce. So we can use our y=mx+b equation again but now to determine the final y position of our ball.

y=(-15)(3 and 1/3)+150

y is the final y position of the ball, our 15 for our velocity is now negative and our time is the time after the bounce from above, the b is 150 because we are starting at the bottom of the screen. By quickly solving this, we can see that we are now at y = 100.

So we can see that when we are at x = 200, or the x position of the paddle, our y position will be at 100 and this is where we can hit the ball from with the paddle.

Hope this is all correct, trying to finish this quickly before going to sleep. I can answer any more questions about this that you have. This is all just the math and physics for you to understand, you will need to interpret this into whatever language you intend to use.


Years ago I wrote a function to calculate leading a moving target as a cheat for a "Missile Command" clone somebody wrote. Your situation is different so this methodology isn't entirely applicable, but your problem is similar so seeing this could be helpful to you.

I just searched for that old forum post and amazingly the code's still there:

Function Cheat()
  Color 255,0,0
  For M.Meteor = Each Meteor
    distance#=M\m
    x#=(M\Vector\x*distance)+M\ox
    y#=(M\Vector\y*distance)+M\oy
    For i=1 To 6
      distance=M\m+M\speed*Sqr((x-gunx)*(x-gunx)+(y-guny)*(y-guny))/bulletspeed
      x=(M\Vector\x*distance)+M\ox
      y=(M\Vector\y*distance)+M\oy
    Next
    Oval x,y,4,4
  Next
End Function

(My apologies if that code is really hard to follow, it was written in a pretty obscure dialect of Basic.)

Basically I use a little trigonometry to calculate where the target will be by the time the shot crosses its path if I shoot directly at the target. Then I repeat the calculation but this time figuring out where the target will be if I fire at the location I determined in the previous calculation. I repeat this several times (6 times in the code above, but the number of times you will need to do it will vary; enough times to be accurate, but not too often that you're slowing down the program execution,) narrowing in on the correct leading, to determine where to shoot at. In my code I draw a red dot at the spot, but obviously you could do whatever you want with the information once you've figured out the coordinates.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜