-
Collision Detection
Hi all...
I'm prorgramming a breakout/paddle game and its comming together nicely, but now I have gotten to the Collision Detection(when the ball hits the blocks)...
I create objects(BOB's) using my engine which has a lot of wrapper functions, fx Collison_BOBS:
PHP Code:
int Collision_BOBS(BOB_PTR bob1, // Pointer to first BOB
BOB_PTR bob2); // Pointer to second BOB
The function returns TRUE is the two bounding rectangles overlap.
So i have tried to make for loop to check whenever the ball BOB hits the box[28] BOB, but my machine hanged....:(
I have also tried to do it manually like this
PHP Code:
if(Collision_BOBS(&ball, &box[1])){
......
}
etc.....
Hanged again....anyone have any ideas....my main game loop look like this:
PHP Code:
int Game_Main(void *parms)
{
// check of user is trying to exit
if (KEY_DOWN(VK_ESCAPE))
PostMessage(main_window_handle, WM_DESTROY,0,0);
if (KEY_DOWN(VK_LEFT)) {
if (paddle.x >5) {
paddle.x = paddle.x -10;
Move_BOB(&paddle);
}
} else if(KEY_DOWN(VK_RIGHT)) {
if(paddle.x + paddle.width < SCREEN_WIDTH) {
paddle.x = paddle.x +10;
Move_BOB(&paddle);
}
}
// start the timing clock
Start_Clock();
// clear the drawing surface
DD_Fill_Surface(lpddsback, 0);
Draw_BOB(&paddle,lpddsback);
Draw_BOB(&ball,lpddsback);
for (int draw = 0; draw<28; draw++)
Draw_BOB(&box[draw],lpddsback);
ball.xv = xangle;
ball.yv = yangle;
Move_BOB(&ball);
//the ball has gone outside the screen
if(ball.x <= 0 | ball.x >= SCREEN_WIDTH - ball.width ){
xangle = -xangle;
}
// the ball has gone outside the screen
if(ball.y <= 0 |ball.y == SCREEN_HEIGHT - ball.height){
yangle = -yangle;
}
// the ball has hit the paddle
if(Collision_BOBS(&ball,&paddle)){
yangle=-yangle; // change direction
}
// flip the surfaces
DD_Flip();
// sync to 30ish fps
Wait_Clock(30);
// return success
return(1);
} // end Game_Main
please let me know if you need more source code...
Thanks in advance! :)
-
box[28] doesnt exist. Remember, in C++, 28 elements go from 0..27.
Z.
-
oh typo...but it still doesn't do it right, because when I test for collision, the game might be testing block 5 and the has just hit block 20......thats my problem....
-
You need to step through your code line by line, and find out EXACTLY where it crashes. From what I can see above, the problem may be in your Collision function.
Z.
-
The Collision function works just fine, but I was trying to step through the code using a loop but it didn't work because when the loop what testing block 5 I might hit block 26 with ball, and it also crashes.....any suggestions...
The loop I was using:
PHP Code:
for(int counter; counter<27; counter++)
if(Collision_BOBS(&ball,&box[counter])){
......//Do whatever with the block
}
-
The collision function obviously does not work fine, as it crashes. How are you defining the box variable, also?
Z.
-
-
Change it to a 28. You attempt to access 27, which is one beyond the number that you have.
Z.
-
Now I have got the loop running without hanging, but the collision still isn't good...
PHP Code:
#define LAST_HIT_RIGHT 1
#define LAST_HIT_LEFT 2
#define LAST_HIT_TOP 3
#define LAST_HIT_BOTTOM 4
int lasthit;
PHP Code:
for(int counter = 0; counter<28; counter++)
if(Collision_BOBS(&ball,&box[counter])){
if(lasthit == LAST_HIT_LEFT){
if(xangle <0){
xangle = xangle;
}
if(xangle >0 ){
xangle = -xangle;
}}
if(lasthit == LAST_HIT_RIGHT){
if(xangle <0){
xangle = xangle;
}
if(xangle > 0){
xangle = -xangle;
}}
if(lasthit == LAST_HIT_TOP){
if(yangle < 0){
yangle = yangle;
}
if(yangle > 0){
yangle = -yangle;
}}
if(lasthit == LAST_HIT_BOTTOM){
if(yangle < 0){
yangle = yangle;
}
if(yangle > 0){
yangle = -yangle;
}}
}
Can you see what I am doing wrong?
The defines is to detect if the ball last hit the left or right side, the top or the paddle
-
1 Attachment(s)
If it helps, here is the complete source code