   Pong Game collision detection
  Hi everyone this is my first post in gamedev.net. So I was inspired by this webpage to write a simple pong game. Howeverwhen it comes down to collision checking my code acts kinda goofy and I don't really know whyThis is the codeint loop(){DrawMap();  CheckColission();return 1;}void CheckCollission(){if((ball.getx()&lt;=100)||(ball.getx()&gt;=833)){    ball.setv(ball.getv()*-1);    int k = rand()%2;    if(k==1)  ball.setl(tan(atan(ball.getl())+(45 * 0.01745329)));    else ball.setl(tan(atan(ball.getl())/(45 * 0.01745329)));    int t = (rand() % 51) -25;    ball.setl(ball.getl() + (ball.getl()*t)/100);}else if((ball.gety()&gt;=430)||(ball.gety()&lt;=18)){    ball.setl(-1/ball.getl());    int t = (rand() % 51) -25;    ball.setl(ball.getl() + (ball.getl()*t)/100);}}Does anybody have any suggestions?[Edited by - strarpo on February 5, 2008 8:22:24 AM]
  I suggest you fix the formatting (stick that code between some [ source ] [ /source ] or   tags, only without the spaces inside the brackets).You can also work on giving your methods more meaningful names, ball.setl for example doesn't tell me anything about what that method does. Could it be a setLength method or a setLeftPosition method?Another thing is that magic numbers are evil, 0.01745329 is just a random number to me and anyone else.  Create a constant and give that number some meaningful alias: const float MY_MEANINGFUL_VALUE = 0.01745329;Lastly, I'm sure you can refactor a lot of that code, I can see the same few lines repeated over and over, so refactor them out to a different level of scope so that you only have to write them once. You might be able to write them once at the end of an if-statement or you might have to write a new function and put that code in there (I don't know which is the most appropriate as it's not immeadiately obvious due to the lack of formatting [smile]).Once you've done some or all of the above steps it'll be a whole lot easier to spot the error.   ;  For code, you should use the source of code tags, as described in the GDNet Forums FAQ. It preserves formatting and adds syntax highlighting, which makes it much easier to read.As for your problem, how does it have to behave and what does it actually do? In other words, what's going wrong? Did you step through it with a debugger already? What language are you using, anyway?As for your code, it contains a lot of so-called 'magical numbers'. What do these 45, 51, 833, 0.01745329 and such stand for? It's a good idea to create properly named variabeles for them (int leftBorder, float maximumAngle, whatever purpose they have, make it clear) and use these throughout your code instead. Changing some values becomes a lot easier and your code becomes a lot more clear.Bytheway, it's collision, not colission. You'll probably also want to draw the map after you've checked and handled collisions, otherwise the display will always 'run behind' a bit. Also, you're only checking for collisions with the top and bottom sides if the ball didn't collide with the left and right side. That might be an issue, assuming that both can occur at the same time.   ;  Thank you all for your timeFirst of all the code is c++. 45 * 0.01745329 is just a way to convert degrees to radians nothing special there. The tan sin cos methods want radians  that's all.Now getl() return the l float of my Ball class. You see theoreticaly the ball is moving on straight line. Mathematically the line is y = lx + b. setv() set the velocity of the ball. The velocity also determines the direction of the ballSo basically l is the tan of the angle that is being formed by the line and the x-axis.Whenever a collision occurs the line must change. Therfore the l factor changes. The rest is just some randomness added so that the game isn't predictable.The numbers 100 833 18 430 are just the pixel that I a have manually set as my guardian value.Now the problem is that occasionally the ball will not obey this guardian (the 100 pixel for example) and will trail of the screen or keep bouncing until it somehow finds its way into the "ring".Here is link to a .rar file that contains the .exe and the bmps used. If anyone feels like going through the whole code just let me know.my_pong_beta [Edited by - strarpo on February 5, 2008 8:06:53 AM];  You should check if the ball velocity doesn't get reversed more than once during the collision . Not sure if that's the problem, your exe runs but it hangs on my comp   ;  aarbron what do you mean it hangs?By the way the code contains windows specific files so it's windows-only   