I ended up getting the line segments to detect collisions properly. I have a list of all edges on the table, and then I do a quick test to see if the ball interesects that plane.
float CollisionManager::CheckDistanceFromPoint(D3DXVECTOR2 point, D3DXVECTOR2 direction, D3DXVECTOR2 centerPoint){ //12 would be the distance for a collision D3DXVECTOR2 tmp = point - centerPoint; float distance = D3DXVec2CCW(&tmp, &direction); distance = abs(distance); return distance;}
Then I create a system of equations and solve for the intersection point and the amount of overlap.
Anyway, I ran into a new problem, that seems a lot simplier than it is. I wanted to minimize the number of collision tests I run in a second to about 30. When I lowered it to 100, 50, and then 30 I started getting some strange results. Right now I am doing well over 900 collision tests a second and everything seems to work fairly decent. When I lower the number of tests a couple of the balls seem to stick together and then orbit around eachother. I have been looking for a decent way to move the balls away from eachother so that they don't get stuck.
Here is an image of some of the possible scenarios. I was originally thinking that I would calculate the new velocity and then just seperate them based on there new speed/totalspeed * overshoot, but this will only work in straight lines.
http://adamwlarson.com/pictures/poolballs.JPG
Should I move the balls away from eachother first and the correct there velocity, or should I calc the new velocity and move the ball along that path based on there new speed. Either way I'm not really sure because I calculate the exchange of momentum based on the angle of collision compared to the velocity vector.
if(ball1->getCurrentSpeed() > ball2->getCurrentSpeed()) { //Ball 1 is moving faster //Get the collison Vector between the two balls D3DXVECTOR2 collisionVector = ball2->getCenter()-ball1->getCenter(); D3DXVec2Normalize(&collisionVectorNormalized, &collisionVector); //Store ball1's Velocity temp = ball1->getVelocity(); D3DXVECTOR2 ball1VelocityNorm; //Normalize the velocity vector so it can be used to calc the DotProduct D3DXVec2Normalize(&ball1VelocityNorm, &temp); //Determine Dot product to figure out percentage of impact float dotValue = D3DXVec2Dot(&ball1VelocityNorm, &collisionVectorNormalized); collisionVectorNormalized *= (ball1->getCurrentSpeed() * dotValue); collisionVectorNormalized += ball2->getVelocity(); //Subtract the transferred momentum from the Ball1's current velocity temp.x -= collisionVectorNormalized.x; temp.y -= collisionVectorNormalized.y; //Set the new velocities ball1->setVelocity(temp.x, temp.y); ball2->setVelocity(collisionVectorNormalized.x, collisionVectorNormalized.y); }
I need to fine tune the algorithm some to account for the transfer of ball2's velocity, but you can see what I'm getting at.
Thanks in advance