   pong collision detection and reaction    
  I think i've got a pretty good idea on the collision detection and the reaction but it doesnt seem to be working well:  int xpos = 600;int ypos = 200;int xpos2 = 20;int ypos2 = 200;int xball = 300;int yball = 100;int ballvecx = 3;int ballvecy = 3;//other code...   xball += ballvecx;   yball += ballvecy;   if(xball == xpos2)   {   xball -= ballvecx;   yball -= ballvecy;   }   if(yball == ypos2)   {   xball -= ballvecx;   yball -= ballvecy;   }   if(xball == xpos)   {   xball += ballvecx;   yball += ballvecy;   }   if(yball == ypos)   {   xball += ballvecx;   yball += ballvecy;   }    DrawBG();    DrawScene();    got the ball moving and the padels moving perfectly, just that this doesnt seem to work, i figure its becuase evry time it checks the if statments the balls possision wont always be equal to the padels possition so it will only reverse coarse for a brief moment. Anyway round this?Oh the those statments are in a game loop.[edited by - com on December 23, 2002 5:29:44 AM]
  Maybe this is the wrong way to go about it? What has everybody else done for a simple collision detection and reaction?  ;  Well for one, your checking the position of the ball against a point.  Instead, I would check it against a range.  Also, you might do well with a struct or two...  Here's a quick example:   struct spaddle{  int x;  int y;  int width;}struct sball{  int x;  int y;  int dx;  int dy;}//initialize the vars any way you see fit...// I chose this:spaddle paddle1;spaddle paddle2;sball ball;// then the collision code...if((ball.x > paddle1.x) && (ball.x < (paddle1.x - paddle1.width) && (ball.y < paddle1.y)){  ball.dy *= -1;  ball.y += ball.dy;  ball.x += ball.dx;}    Repeat that against paddle2 as well.  What this does is check to see if the ball is lined up with the paddle on the x coordinates, then checks if the ball is touching the paddle ont he y.  This assumes that paddle1 is on the top of the screen, since we are checking if ball.y is less than paddle1.y.  For the bottom of the screen you would check if it is greater.  If you want a side to side pong, then you have to change what variables you are checking.Hope that helps! EDIT: Wrong tag on code...  oops  Always remember, you''re unique.  Just like everyone else.  Greven[edited by - Greven on December 23, 2002 9:31:38 AM];  im not quite sure what the variables stand for, but i will try and  have a guess, i think the problem is that youre checking for the exact values and unless the position is only being incremented 1 at a time then at sometimes the conditions youre using to check will not be metie.int xball = 598;xpos = 600;int ballvecx = 3;xball += ballvecx; // ie xball = 601if(xball == xpos)  // this condition is not met !perhaps this is the way you should do be doing itint xball = 598;int xpos = 600;int ballvecx = 3;xball += ballvecx;if(xball >= xpos)    int xpos = 600;int ypos = 200;int xpos2 = 20;int ypos2 = 200;int xball = 300;int yball = 100;int ballvecx = 3;int ballvecy = 3;//other code...   xball += ballvecx;   yball += ballvecy;   if(xball <= xpos2){  xball -= ballvecx;   yball -= ballvecy;   }   if(yball >= ypos2)   {   xball -= ballvecx;   yball -= ballvecy;}   if(xball >= xpos)   {   xball += ballvecx;   yball += ballvecy;   }   if(yball <= ypos)   {   xball += ballvecx;   yball += ballvecy;   }DrawBG();    DrawScene();   hope this works sorry if it dont i have only been programming for 8 daysah well someone beat me to it with a better solution :D[edited by - deal on December 23, 2002 9:54:58 AM];  well the varibles are as follows:xpos = x coord of first paddelypos = y coord of first paddelxpos2 = x coord of secound paddelypos2 = y coord of secound paddelxball = x coord of ballyball = y coord of ballballvecx = velocity, x coordballvecy = velocity, y coordIn reply to deal thx for the reply but it unfortunatly didnt work ;(  ;  ah k try these little bits of code, insert them at the appropriate place.   // variables needed---------------------------------------------int paddleSize = 40; // change this variable to match size of paddle graphicint topwall = 400;   // this variable is the y co-ord of the ''ceiling''int botwall = 0; // ''floor''int xpos = 600; //paddle1int ypos = 200; //bottom edge of paddleint yposb = ypos+paddleSize;//topedge of paddleint xpos2 = 20; //paddle 2int ypos2 = 200;int ypos2b = ypos2+paddleSize; //240int xball = 300; //ball x posint yball = 200; //ball x posint ballvecx = 3; // x velocityint ballvecy = 3;//---------update ball position----------------xball += ballvecx;yball += ballvecy;//------------check for collision on left paddle----------------if(xball <= xpos2  && ( yball <= ypos2b && yball >= ypos2) ){  ballvecx = ballvecx * -1; }//--------------check for collision on right paddle-------------if(xball >= xpos && ( yball <= ypos2 && yball >= ypos ) ){ballvecx = ballvecx * -1;}//----------------- check for collision on ceiling-------------if (yball >= topwall){ballvecy = ballvecy * -1;}//-------------------- check for collision on floor-----------if (yball <= botwall){ballvecy = ballvecy * -1;}  hopefully this should work i tested the collision detection by manually changing xball and yball values and using couts triggered if the if conditions were met.  ;  thank you very mutch  it works now, one or 2 missing collosions for the other walls but nothing i cant fix now thx to you deal ;  wow i actually helped someone, and np i enjoyed solving the problem, good practice [edited by - deal on December 23, 2002 1:02:55 PM];  Greven, your idea is great, a simple sphere collides perfectly + the use of structures (which I didn''t know how to use properly). You''ve helped a lot man, so thanks a lot.   Linux is like a Wig-Wam: no Gates, no Windows, Apache inside. ;  I thought I should post some code from a Pong game I once wrote in Dark Basic. This is all collision detection code.. NOTE: ** I Consider myself immune to all Flames!! This is cuz I wrote this as fast I could.. I just took a couple of houers to write the whole game, and then I had to learn Dark Basic and stuff too ** IF rpady# > roof THEN rpady# = roofIF rpady# < floor THEN rpady# = floorIF lpady# > roof THEN lpady# = roofIF lpady# < floor THEN lpady# = floorcollision = OBJECT COLLISION(1,0)   rem --- Collision with right pad ---IF collision = 2ballspeedx# = 0.7 ballspiny# = ballspiny# + 2.2deltay# = bally# - lpady# ballspeedy# = ballspeedy#/2 + deltay#/8.0ENDIFrem --- Collision with left pad ---IF collision = 3ballspeedx# = -0.7ballspiny# = ballspiny# - 2.2deltay# = bally# - rpady#ballspeedy# = ballspeedy#/2 + deltay#/8.0ENDIFrem tag och golvIF collision = 4ballspeedy# = -1.0 * ballspeedy#ballspinx# = ballspinx# + 2.2ENDIFIF collision = 5ballspeedy# = -1.0 * ballspeedy#ballspinx# = ballspinx# + 2.2ENDIFballx# = ballx# + ballspeedx#bally# = bally# + ballspeedy#IF ballx# > 25POSITION OBJECT 1, 0, 0, 0ballx# = 0bally# = 0ballspeedx# = -0.6ballspeedy# = 0.0ballrotx = 0ballroty = 0ballspinx# = 0ballspiny# = 0score1 = score1 + 1SLEEP 1000ENDIFIF ballx# < -25POSITION OBJECT 1, 0, 0, 0ballx# =0bally# =0ballspeedx# = 0.6ballspeedy# = 0.0ballrotx = 0ballroty = 0ballspinx# = 0ballspiny# = 0score2 = score2 + 1SLEEP 1000ENDIF ----------------------------------------------Petter Nordlander"There are only 10 kinds of people in the world. The who understand binary and those who don''t" 