# Thread: collision detection problem solved!

1. ## 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)
{
{
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)
{

{
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());
}
}
}
{
{
}
{
}
{
}
{
}
}```

2. Still using numeric constants like 70, I see.

Also, try to avoid calling variables things that look like numbers, such as l2.

Code:
`if (l2<-0)`
is that supposed to be l2 <= 0?

Edit:
Code:
`reverse_x_velocity(ball->getVelocity());`
Do I take it that getVelocity returns a reference to Ball::velocity? If so, perhaps you should do something like this instead:
Code:
`ball->setVelocity(-ball->getVelocity().x, ball->getVelocity().y);`
That maintains strict encapsulation.
Or you could let the velocity itself have a public member function "reversex()" and "reversey()".

--
Mats

--
Mats