So I now have a rectangle with a bunch of balls bouncing around inside of it. When the rectangle begins to rotate, collisions work decently...there are times when they seem to reverse velocities a couple pixels before a side of the rectangle, but that shouldn't be hard to fix. Sometimes, however, I have some serious sticking. I haven't quite figured that one out yet.
As the rectangle rotates farther and farther from its original point, the collisions become less and less accurate. The balls begin to change their velocity close to 50 pixels away from the actual sides of the rectangle.
Does anyone know what might be causing this anomaly?
Here is my code:
[source lang = c++] void PlayGameState::FindWallCollision (vector <Ball>:: iterator iter) { Vector2d vertex1, vertex2, vertex3, vertex4, testPoint; double u, distance; //Starting from the upper left vertex and going clockwise, ending at the lower left vertex. //This finds the coordinates of each vertex based on the given rotation. // I considered this formula to calculate each vertex below. cx and cy are the anchor point of the rectangle. // x and y are the original vertices (the corners) of the rectangle // newx = cx + (x - cx) * cos(angle) + (y - cy) * sin(angle) // newy = cy + (y - cy) * cos(angle) - (x - cx) * sin(angle) vertex1.x = 200 + (55 - 200) * cos(-rotationPos) + (55 - 220) * sin(-rotationPos);//200 and 240 are the coorinates of the center of the box. vertex1.y = 220 + (55 - 220) * cos(-rotationPos) - (55 - 200) * sin(-rotationPos); vertex2.x = 200 + (455 - 200) * cos(-rotationPos) + (55 - 220) * sin(-rotationPos); vertex2.y = 220 + (55 - 220) * cos(-rotationPos) - (455 - 200) * sin(-rotationPos); vertex3.x = 200 + (455 - 200) * cos(-rotationPos) + (455 - 220) * sin(-rotationPos); vertex3.y = 220 + (455 - 220) * cos(-rotationPos) - (455 - 200) * sin(-rotationPos); vertex4.x = 200 + (55 - 200) * cos(-rotationPos) + (455 - 220) * sin(-rotationPos); vertex4.y = 220 + (455 - 220) * cos(-rotationPos) - (55 - 200) * sin(-rotationPos); // Find the closest point on every line to the ball (iter). // Take dot product between position vector and relative velocity vector // // which velocities reverse depends on the orientation of the wall. // we will determine which wall is which by simply considering which two vertices were are examining. //1st and 2nd vertex means top wall. u = ( ( ( (*iter).Pos().x- vertex1.x ) * ( vertex2.x - vertex1.x ) ) + ( ( (*iter).Pos().y - vertex1.y ) * ( vertex2.y - vertex1.y ) ) ) / ( Distance (vertex1, vertex2) * Distance (vertex1, vertex2) ); if (u > 0.0f && u < 1.0f) { //The point on the side of the rectangle that we are testing that is closest to the ball. //A line that connects these two points would intersect perpendicularly the line between the two vertices we are testing. testPoint.x = vertex1.x + u * (vertex2.x - vertex1.x); testPoint.y = vertex1.y + u * (vertex2.y - vertex1.y); //find the distance between the ball and the wall distance = Distance((*iter).Pos(), testPoint); //if the distance is greater than the radius of the ball, a collision occurs if ( distance <= 29 * (*iter).GetMass() + epsilon) { //The rectangle is rotated based on the momentum of the ball. CalculateRotation(iter); //occasionally there is sticking. Under these circumstances, //we want to teleport (a very very small distance) the ball back into the rectangle to avoid the detection of a collision over and over. (*iter).Pos(( (-(*iter).Vel()) * epsilon * 100000) + (*iter).OldPos()); //the velocitiy of the ball is changed. (*iter).Vel(Vector2d((*iter).Vel().x, -(*iter).Vel().y)); game.hge->Effect_Play(WallCollisionEffect); } }//note that this operation is repeated 4 times, once for each line of the rectangle.