1
General / Collision detection(Pong game)
« on: February 09, 2012, 08:05:56 pm »
Okay guys I found the problem and thank you for your help!
The problem occured because of the shape of my ball which is square. I was comparing the top left y-coordinate of the ball(for both vy > 0 and vy <0), however, I should have compared like fallowing:
if(vy > 0)
compare lower-left corner(bouncerPosition.y + ballHeight_)
else if(vy < 0)
compare top-left corner(bouncerPosition.y)
here is my code for solution:
Thank you again!
The problem occured because of the shape of my ball which is square. I was comparing the top left y-coordinate of the ball(for both vy > 0 and vy <0), however, I should have compared like fallowing:
if(vy > 0)
compare lower-left corner(bouncerPosition.y + ballHeight_)
else if(vy < 0)
compare top-left corner(bouncerPosition.y)
here is my code for solution:
Code: [Select]
if(velocity_.x < 0.0f)
{
if(bouncerPosition.x <= leftRacketPosition.x + racketWidth)
{
if(velocity_.y < 0.0f)
{
if(bouncerPosition.y <= leftRacketPosition.y + racketHeight &&
bouncerPosition.y >= leftRacketPosition.y)
{
return COLLIDING_WITH_LEFT_PADDLE;
}
}
else
{
if(bouncerPosition.y + ballHeight_ >= leftRacketPosition.y &&
bouncerPosition.y + ballHeight_ <= leftRacketPosition.y + racketHeight)
{
return COLLIDING_WITH_LEFT_PADDLE;
}
}
}
}
else if(velocity_.x > 0.0f)
{
if(bouncerPosition.x + ballWidth_ >= rightRacketPosition.x)
{
if(velocity_.y < 0.0f)
{
if(bouncerPosition.y <= rightRacketPosition.y + racketHeight &&
bouncerPosition.y >= rightRacketPosition.y)
{
return COLLIDING_WITH_RIGHT_PADDLE;
}
}
else
{
if(bouncerPosition.y + ballHeight_ >= rightRacketPosition.y &&
bouncerPosition.y + ballHeight_ <= rightRacketPosition.y + racketHeight)
{
return COLLIDING_WITH_RIGHT_PADDLE;
}
}
}
}
Thank you again!