collision detection problem solved!
I decided to go back to the roots of where all this came from, with all the trigonometry and calculus and whatnot.
I decided to add a few structs to help my cause, ray and plane.
I learned that a ray is nothing but a point origin and basically another point which is found by subtracting point a from b.
I learned that I actually need the dot product of that specific ray's directional vector.
Among other numerous vector math stuff.
I applied my knowledge and came up with this:
Perfect!
Code:
bool planeCollision2d(plane & boundingPlane, vector2 & objectPosition)
{
ray line_between_objects(objectPosition,boundingPlane.position);
double DotProduct=line_between_objects.direction.dot_product(boundingPlane.normal);
double l2;
//determine if ray paralle to plane
if ((DotProduct<0)&&(DotProduct>-0))
return false;
l2=(boundingPlane.normal.dot_product(boundingPlane.position-objectPosition))/DotProduct;
if (l2<-0)
return false;
return true;
}
Ahhh the beauty and simplicity of pong logic.
Code:
if(ball->getVelocity().x > 0)
{
if (collision2d(ball->getMinimum(), ball->getMaximum(), player2->paddle->getMinimum(), player2->paddle->getMaximum()))
{
reverse_x_velocity(ball->getVelocity());
}
else if(planeCollision2d(right, ball->getLocation()))
{
reverse_x_velocity(ball->getVelocity());
ball->getLocation().x = 0;
ball->getLocation().y = 0;
player1->goal();
}
}
if(ball->getVelocity().x < 0)
{
if (collision2d(ball->getMinimum(), ball->getMaximum(), player1->paddle->getMinimum(), player1->paddle->getMaximum()))
{
reverse_x_velocity(ball->getVelocity());
}
else if(planeCollision2d(left, ball->getLocation()))
{
reverse_x_velocity(ball->getVelocity());
ball->getLocation().x = 0;
ball->getLocation().y = 0;
player2->goal();
}
}
if(ball->getVelocity().y > 0)
{
if (planeCollision2d(top, ball->getLocation()))
{
reverse_y_velocity(ball->getVelocity());
}
}
if(ball->getVelocity().y < 0)
{
if (planeCollision2d(bottom, ball->getLocation()))
{
reverse_y_velocity(ball->getVelocity());
}
}
}
void check_paddle_collision()
{
if (planeCollision2d(top, player1->paddle->getLocation()))
{
player1->paddle->getLocation().y = 70;
}
else if(planeCollision2d(bottom, player1->paddle->getLocation()))
{
player1->paddle->getLocation().y = -70;
}
if (planeCollision2d(top, player2->paddle->getLocation()))
{
player2->paddle->getLocation().y = 70;
}
else if(planeCollision2d(bottom, player2->paddle->getLocation()))
{
player2->paddle->getLocation().y = -70;
}
}