Problem in Polygon::FindAxisLeastPenetration :
double Polygon::FindAxisLeastPenetration(unsigned int *faceIndex, const Polygon &polygonA, const Polygon &polygonB) const { double bestDistance = -std::numeric_limits<double>::infinity(); unsigned int bestIndex; for (unsigned int i = 0; i < polygonA.points.size(); i++) { Vector2D n = polygonA.normals[i]; Vector2D nw = polygonA.rotationMatrix * n; //ROTATION Matrix22 buT = polygonB.rotationMatrix.Transposed(); n = buT * nw; //ROTATION Vector2D support = polygonB.points[polygonB.GetSupport(-n)]; Vector2D vertex = polygonA.points[i]; vertex = polygonA.rotationMatrix * vertex; //ROTATION vertex.Add(polygonA.body->GetPosition()); vertex.Subtract(polygonB.body->GetPosition()); vertex = buT * vertex; // ROTATION double distance = n.DotProduct(support - vertex); if (distance > bestDistance) { bestDistance = distance; bestIndex = i; } } *faceIndex = bestIndex; return bestDistance; } unsigned int Polygon::GetSupport(const Vector2D &dir) const { double bestProjection = -std::numeric_limits<double>::infinity(); unsigned int bestIndex = 0; for (unsigned int i = 0; i < points.size(); i++) { Vector2D vertex = points[i]; double projection = vertex.DotProduct(dir); if (projection > bestProjection) { bestProjection = projection; bestIndex = i; } } return bestIndex; } Manifold Polygon::CheckCollision(const Polygon &polygonA, const Polygon &polygonB) const { Manifold result; result.objectA = polygonA.body; result.objectB = polygonB.body; unsigned int indexA; double penetrationA = Polygon::FindAxisLeastPenetration(&indexA, polygonA, polygonB); if (penetrationA >= 0.0) { result.intersects = false; return result; } unsigned int indexB; double penetrationB = Polygon::FindAxisLeastPenetration(&indexB, polygonB, polygonA); if (penetrationB >= 0.0) { result.intersects = false; return result; } result.intersects = true; //... return result; Rectangle::Rectangle(double width, double height) : Polygon() { double hw = width / 2.0; double hh = height / 2.0; points.push_back(Vector2D(-hw, -hh)); points.push_back(Vector2D(hw, -hh)); points.push_back(Vector2D(hw, hh)); points.push_back(Vector2D(-hw, hh)); // points.push_back(Vector2D(0, 0)); // points.push_back(Vector2D(width, 0)); // points.push_back(Vector2D(width, height)); // points.push_back(Vector2D(0, height)); normals.push_back(Vector2D(0.0, -1.0)); normals.push_back(Vector2D(1.0, 0.0)); normals.push_back(Vector2D(0.0, 1.0)); normals.push_back(Vector2D(-1.0, 0.0)); center.x = 0; center.y = 0;
}
polygon.rotationMatrix is an object of type Matrix22 , which is a 2x2 matrix.
polygon.points std::vector<Vector2D> filled with vectors.
polygon.body is a pointer to an instance of Object . In this case, it was used only to obtain a position.
polygon.body->position is an instance of Vector2D containing the X and Y coordinates.
Vector2D polygon.body->GetPosition() returns the position vector of the body.
It works fine, except that it rotates around the point [0, 0] but it should rotate around the center of mass.
I know that rotation around a point can be done as follows:
rotationMatrix * (vertex - point) + point
And it works great when rendering polygons. But not in collision detection.
How to rotate vectors around a certain point in this case?
EDIT: That's what I still have
double Polygon::FindAxisLeastPenetration(unsigned int *faceIndex, const Polygon &polygonA, const Polygon &polygonB) const { double bestDistance = -std::numeric_limits<double>::infinity(); unsigned int bestIndex; for (unsigned int i = 0; i < polygonA.points.size(); i++) {
Now I donβt care about optimization. There is the same problem. When rotating around the center of the collisions are not detected properly. However, if the center is [0, 0] or it is not used, then the collision detection works correctly, but again the rotation is not performed correctly.
Edit: even when turning before collision detection, I get the same problem. The best approach so far has been to translate the polygon so that its center is at [0, 0] , but no collisions were detected at some angles. I donβt know what to do now.
Edit: screenshots (polygons are translated so that their centers of mass are always at [0, 0] , in this case polygons are rectangles) Collision detection does not work here 
Collision detection also works poorly 
Collision detection worked well here 
Edit: I added a Rectangle class.