| | |
Collision Detection logic
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Apr 2008
Posts: 14
Reputation:
Solved Threads: 1
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.
C++ Syntax (Toggle Plain Text)
void checkbox(){ //check for collision of box one if (ballx1 >= boxes[0][0] && ballx1 <= boxes[0][2]){ box[0] = 0; dirx = dirx * -1; } else if (bally1 >= boxes[0][1] && bally1 <= boxes[0][3]){ box[0] = 0; diry = diry * -1; } //check for collision of box two else if (ballx1 >= boxes[1][0] && ballx1 <= boxes[1][2]){ box[1] = 0; dirx = dirx * -1; } else if (bally1 >= boxes[1][1] && bally1 <= boxes[1][3]){ box[1] = 0; diry = diry * -1; } //check for collision of box three else if (ballx1 >= boxes[2][0] && ballx1 <= boxes[2][2]){ box[2] = 0; dirx = dirx * -1; } else if (bally1 >= boxes[2][1] && bally1 <= boxes[2][3]){ box[2] = 0; diry = diry * -1; } //check for collision of box four else if (ballx1 >= boxes[3][0] && ballx1 <= boxes[3][2]){ box[3] = 0; dirx = dirx * -1; } else if (bally1 >= boxes[3][1] && bally1 <= boxes[3][3]){ box[3] = 0; dirx = dirx * -1; } //check for collision of box five else if (ballx1 >= boxes[4][0] && ballx1 <= boxes[4][2]){ box[4] = 0; dirx = dirx * -1; } else if (bally1 >= boxes[4][1] && bally1 <= boxes[4][3]){ box[4] = 0; dirx = dirx * -1; } }
Last edited by noey699; Oct 25th, 2009 at 10:21 pm. Reason: made a misteak
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
•
•
Join Date: Nov 2008
Posts: 397
Reputation:
Solved Threads: 72
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
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.
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
c++ Syntax (Toggle Plain Text)
if (ballx1 >= boxes[0][0] && ballx1 <= boxes[0][2] && bally1 >= boxes[0][1] && bally1 <= boxes[0][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
•
•
Join Date: Apr 2008
Posts: 14
Reputation:
Solved Threads: 1
0
#5 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
c++ Syntax (Toggle Plain Text)
if (ballx1 >= boxes[0][0] && ballx1 <= boxes[0][2] && bally1 >= boxes[0][1] && bally1 <= boxes[0][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.
but I understand my mistake now, thank you I should need no more help now.
![]() |
Similar Threads
- Collision Detection with rectangles (C++)
- simple collision detection (VB.NET)
- DirectX - Collision detection (Game Development)
- Collision Detection - Micro Java (Java)
- General Collision Detection (Game Development)
- Particle System problem (collision detection and handeling) (Java)
- Collision Detection (C++)
Other Threads in the C++ Forum
- Previous Thread: Ugh... Need some help with allegro and stuff
- Next Thread: Help with Functions
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






