   Problems with Pong
  I am looking at the source code for a pong game, and I cannot understand some of the code. Well, I understand what the code is doing, but I don''t understand the logic behind it. I am hoping that some of you might be able to clarify it for me.   void ArtificialPlayer::CalculateTarget(){  Ball* b = GetBall();  assert(b); float bx = b->GetX();    float by = b->GetY();    float bxi = b->GetXinc();    float byi = b->GetYinc();float paddlePos = GetY(); // vertial distance from ball to paddle   float distY = static_cast<float>(fabs(paddlePos - by)); // update steps before interception  float distYInc = static_cast<float>(fabs(distY / byi)); // horitonal distance we must move to intercept  float distX = distYInc*bxi; // random "slop" int r = (rand()%(AIRandSlop*2)) - AIRandSlop;  // where we want to be to intercept ball   targetX = bx + distX + static_cast<float>(r);}This is a function for the Artificial Player class within a pong game. It is the function that is used to calculate the target where the artificial player paddle will move  in order to intercept the ball. The first function Getball() returns a pointer to the ball class, which is the class that will represent the ball itself. The variables bx and by represent the x and y locations of the ball. bxi and byi represent the increment on each axis that the ball will move. Now, I understand the math behind the distY varible, however I cannot understand the math behind the distX variable. It says that the distance the paddle must move along the x-axis to intercept the ball is equal to the amount that the ball travels along the y-axis within each frame (determined by the previous variable distYinc) multiplied by the amount that the ball increments along the x-axis. I am just confused as to how this equation determines the horizontal distance the paddle must move to intercept? I understand that this is confusing and any attempt at helping is greatly appreciated.Thanks,--BioagentX 
quote:Original post by bioagentX  It says that the distance the paddle must move along the x-axis to intercept the ball is equal to the amount that the ball travels along the y-axis within each frame (determined by the previous variable distYinc) multiplied by the amount that the ball increments along the x-axis. I am just confused as to how this equation determines the horizontal distance the paddle must move to intercept? I understand that this is confusing and any attempt at helping is greatly appreciated.Not quite.  distYInc is how long it will take the ball to be even with the paddle--it''s a time, not a distance.  It then multiplies this by the ball''s horizontal speed to get the horizontal distance the ball will travel in that time.  (Try writing down the units; it helps!)  After that, it does some randomizing to make the AI non-perfect, and sums the ball''s position with the distance it will travel to get the point the ball will be when it reaches the plane of the paddle.This function does not calculate anything about the paddle''s X coordinate--it does not determine how far the paddle must move.-Odd the Hermit ;  Wow, ok. That clears things up a bit. Thank you for the help.--BioagentX 