Hi,
I'm having trouble getting my collision detection code to work properly on a fast moving object.
currently am using time based movement to move a sprite at high velocity with another sprite on the screen. think pong.
my problem arises when the ball sprite is moving fast and goes straight through the bat. i'm using basic bounding box collision detection. here is some code...
bool check_collision( SDL_Rect A, SDL_Rect B )
{ //The sides of the rectangles
int leftA, leftB;
int rightA, rightB;
int topA, topB;
int bottomA, bottomB;
//Calculate the sides of rect A
leftA = A.x;
rightA = A.x + A.w;
topA = A.y;
bottomA = A.y + A.h;
//Calculate the sides of rect B
leftB = B.x;
rightB = B.x + B.w;
topB = B.y;
bottomB = B.y + B.h;
//If any of the sides from A are outside of B
if( bottomA < topB )
{ return false; }
if( topA > bottomB )
{ return false; }
if( rightA < leftB )
{ return false; }
if( leftA > rightB )
{ return false; }
//If none of the sides from A are outside B
return true;
}
thats my basic collision function.
while (gamerunning)
{
player.x = gameplayer->currentxpos();
player.y = gameplayer->currentypos();
player.w = 50;
player.h = 10;
object[0].x = ball[0].currentxpos();
object[0].y = ball[0].currentypos();
object[0].w = 14;
object[0].h = 14;
object[1].x = ball[1].currentxpos();
object[1].y = ball[1].currentypos();
object[1].w = 14;
object[1].h = 14;
float DeltaTime = gametimer->timeSinceLastFrame();
gamescreen->BeginScene();
handleinput();
for ( int x = 0; x < 1; x++)
{
// check balls for collision with player
if (check_collision( player,object[x]) == true)
{
if (!(ball[x].currentypos()+12 > gameplayer->currentypos()))
{
ball[x].Setspeed(ball[x].GetSpeedY()+70.0f);
ball[x].MoveUp();
}
}
Thats the start of my main game loop.
I believe my problem is related to the fact that the ball maybe moving at more than 10 pixels a frame. my players bat is 10 pixels thick. I'm just experimenting at the moment with the code but just can't seem to work out the problem.
debugging my code suggests that when I have the topspeed of the ball as 1000 the movement is still not 2 pixels a frame as my float gets rounded down from about 1.8 to an int of just 1.
Hope someone can push me in the right Direction! :)