So I am making a little physics engine, and I am working on getting the colliders to rotate with their parent objects, so I need to know where the vertices lie after a rotation. Unfortunately, I can't think of an easy way to test whether it is actually working. I could just do a general test of the collisions, but if it fails, I won't know that this rotation stuff is the actual cause. So I am asking here as a sanity-check.
So here is the code:
void PolygonCollider::SetRotation(float newRot)
{
float rotDiff = newRot - Collider::GetRotation();
sf::Vector2f transPos = Collider::GetPosition();
std::vector<sf::Vector2f>::iterator vert = _verts.begin();
while(vert != _verts.end())
{
sf::Vector2f vertDisp = sf::Vector2f(vert->x - transPos.x, vert->y - transPos.y); //the displacement vector from the centre of the shape to the vertex
vertDisp = Rotate(vertDisp, rotDiff); //rotate the displacement vector.
vert->x = vertDisp.x + transPos.x; //x-position of the vertex after rotation
vert->y = vertDisp.y + transPos.y; //y-position of the vertex after rotation
}
}
In words, here is what I am doing, for each vertex:
- Find the displacement of the vertex relative to the centre of the object.
- Rotate the displacement vector.
- Add the new displacement vector to the transform position.
So, I personally don't see any reason why this shouldn't work, as long as my Rotate() function is solid.
N.B. As it says in the title, this is in 2D, in a case where rotations are assumed to be about the depth-axis only, hence the no-quaternions/n-matrices deal.
Thank you.