Im working on a collision system for a simple pong game and I'm having some troubles..
this is my collisionCheck()
based off something I saw neo1 postCode:int collisionCheck(SDL_Rect A, SDL_Rect B) { //The sides of the rectangles unsigned int topA, leftA, bottomA, rightA; unsigned int topB, leftB, bottomB, rightB; // Calculate the sides for the SDL_Rect structs - Rect A topA = A.y; leftA = A.x; bottomA = A.y - A.h; rightA = A.x + A.w; // Calculate the sides for the SDL_Rect structs - Rect B topB = B.y; leftB = B.x; bottomB = B.y - B.h; rightB = B.x + B.w; // If Rect A is touching, or passing through, Rect B return true if( ((topA == bottomB) || (bottomA == topB)) && ((leftA == rightB) || (rightA == leftB)) ) return 1; // if (touching) // Otherwise, return false else return 0; // if (!touching) } // collisionCheck()
And here is my implementation inside a moveBall() function...
So the problem is that the ball moves (had some trouble with this at first) but it doesn't ricochet off the paddle the way it should.. instead it goes through it. Any thoughts?Code:/* PADDLE COLLISION, * OMG RUN BLARGH, BLARGH, BLARGH * */ if ( collisionCheck( game->p1, game->ball ) || collisionCheck( game->ball, game->p2 ) ) { // Add sound effect here? // Multiply x component of slope by -1 to change sign (direction); game->slope.dx *= -1; } // if (bouncing off paddle)
edit: the original pong game was from a tutorial at I think oreilly's website



LinkBack URL
About LinkBacks



