Collision Detection logic

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2008
Posts: 14
Reputation: noey699 is an unknown quantity at this point 
Solved Threads: 1
noey699 noey699 is offline Offline
Newbie Poster

Collision Detection logic

 
0
  #1
Oct 25th, 2009
First of all I would like to point out that all my values are correct in boxes[5][4] which contains the two x and y values needed for each box. boxes[0][0] and boxes[0][1] are the x and y value for the top left of the rectangle. boxes[0][2] and boxes[0][3] are the x and y vales for the bottom right of the rectangle. This function should see if my ball has hit any of the five boxes. When running my ball just looks like its bouncing off of countless "ghost" boxes and I cannot figure out why. Any help I would be grateful of, and if you need any more code or any more info on the program just ask.

  1. void checkbox(){
  2. //check for collision of box one
  3. if (ballx1 >= boxes[0][0] && ballx1 <= boxes[0][2]){
  4. box[0] = 0;
  5. dirx = dirx * -1;
  6. }
  7. else if (bally1 >= boxes[0][1] && bally1 <= boxes[0][3]){
  8. box[0] = 0;
  9. diry = diry * -1;
  10. }
  11.  
  12. //check for collision of box two
  13. else if (ballx1 >= boxes[1][0] && ballx1 <= boxes[1][2]){
  14. box[1] = 0;
  15. dirx = dirx * -1;
  16. }
  17. else if (bally1 >= boxes[1][1] && bally1 <= boxes[1][3]){
  18. box[1] = 0;
  19. diry = diry * -1;
  20. }
  21.  
  22. //check for collision of box three
  23. else if (ballx1 >= boxes[2][0] && ballx1 <= boxes[2][2]){
  24. box[2] = 0;
  25. dirx = dirx * -1;
  26. }
  27. else if (bally1 >= boxes[2][1] && bally1 <= boxes[2][3]){
  28. box[2] = 0;
  29. diry = diry * -1;
  30. }
  31.  
  32. //check for collision of box four
  33. else if (ballx1 >= boxes[3][0] && ballx1 <= boxes[3][2]){
  34. box[3] = 0;
  35. dirx = dirx * -1;
  36. }
  37. else if (bally1 >= boxes[3][1] && bally1 <= boxes[3][3]){
  38. box[3] = 0;
  39. dirx = dirx * -1;
  40. }
  41.  
  42. //check for collision of box five
  43. else if (ballx1 >= boxes[4][0] && ballx1 <= boxes[4][2]){
  44. box[4] = 0;
  45. dirx = dirx * -1;
  46. }
  47. else if (bally1 >= boxes[4][1] && bally1 <= boxes[4][3]){
  48. box[4] = 0;
  49. dirx = dirx * -1;
  50. }
  51. }
Last edited by noey699; Oct 25th, 2009 at 10:21 pm. Reason: made a misteak
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,432
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 186
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso
 
0
  #2
Oct 25th, 2009
where is the position of the box relative to ? Its center ? Edge ?
1) Prove that the area of a circle is pi*r^2, where "r" is the radius of the circle.
2) Problem 2[b]solved by : jonsca
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 397
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is offline Offline
Posting Whiz
 
0
  #3
Oct 25th, 2009
This is code absolutely begging for a loop!!

Your actual problem is that you test x then y. That is incorrect. (I think)

Consider a box with coordinates (0,0) and (10,10). I.e it is a square of area 10. Now what happens when you ball is at
(5,23) obviously that misses BUT in your code the first test accept the
point as if only checks the x value and not the x and y simultantiously

You need
  1. if (ballx1 >= boxes[0][0] && ballx1 <= boxes[0][2] &&
  2. bally1 >= boxes[0][1] && bally1 <= boxes[0][3])
  3. { }

Then just put all 5 box test in a loop please!!

The reason that you should use a loop is that in the copy paste you have made a mistake with boxes[4] and boxes[3].
Note: that line 49 says dirx and I am 99% sure that should say diry.
same with line 39.
Last edited by StuXYZ; Oct 25th, 2009 at 10:28 pm.
experience is the most expensive way to learn anything
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 14
Reputation: noey699 is an unknown quantity at this point 
Solved Threads: 1
noey699 noey699 is offline Offline
Newbie Poster
 
0
  #4
Oct 25th, 2009
Originally Posted by firstPerson View Post
where is the position of the box relative to ? Its center ? Edge ?
the first x and y value for each box is the top left corner
boxes[0][0] and boxes[0][1]
and the second x and y value is the bottom right corner
boxes[0][2] and boxes[0][3]
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 14
Reputation: noey699 is an unknown quantity at this point 
Solved Threads: 1
noey699 noey699 is offline Offline
Newbie Poster
 
0
  #5
Oct 25th, 2009
Originally Posted by StuXYZ View Post
This is code absolutely begging for a loop!!

Your actual problem is that you test x then y. That is incorrect. (I think)

Consider a box with coordinates (0,0) and (10,10). I.e it is a square of area 10. Now what happens when you ball is at
(5,23) obviously that misses BUT in your code the first test accept the
point as if only checks the x value and not the x and y simultantiously

You need
  1. if (ballx1 >= boxes[0][0] && ballx1 <= boxes[0][2] &&
  2. bally1 >= boxes[0][1] && bally1 <= boxes[0][3])
  3. { }

Then just put all 5 box test in a loop please!!

The reason that you should use a loop is that in the copy paste you have made a mistake with boxes[4] and boxes[3].
Note: that line 49 says dirx and I am 99% sure that should say diry.
same with line 39.
the problem with checking them both at the same time is i cannot update the direction as needed ( the variable dirx and diry )

but I understand my mistake now, thank you I should need no more help now.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC