0

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:

  1. Find the displacement of the vertex relative to the centre of the object.
  2. Rotate the displacement vector.
  3. 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.

Makcheese
  • 397
  • 1
  • 14
  • Why not simply store your vertices as polar coordinates with the center of mass as your origin. This would make rotations infinitely easier as it would just be a matter of changing the angle component of the vector?? Just an idea :) – silvergasp Dec 17 '15 at 05:36
  • That...is an idea, definitely. The rendering system I use takes Cartesian, so basically I'll just have to make a conversion between the two. I'll have a think about whether the pay-off will be worth it. Thanks for the idea! – Makcheese Dec 17 '15 at 05:50
  • you will undoubtedly be using some kind of trigonometric function at some point in you code in order to do a rotation. To speed up this process you can create a lookup table see here for more information http://stackoverflow.com/questions/3688649/create-sine-lookup-table-in-c. If your rendering system takes only Cartesian then I would perhaps recommend staying away from polar coordinates unless there are large number of rotating objects that are not likely to be rendered :) Also It is difficult to answer your question without knowing how the rest of your code is implemented. – silvergasp Dec 17 '15 at 06:11
  • Thanks for the help, man. I will definitely look (ayyyy) at making a look-up table, as I am starting to do more trig stuff. And yeah, I understand that it is difficult without the rest of the code; unfortunately, the mathsy/physicsy stuff is cattered through a few files and such, so it tough to provide it all in one hit. But you have already helped a lot! – Makcheese Dec 17 '15 at 06:39
  • I would store your vertices in the original (not translated, not rotated) position + the translation/rotation info. As your translation/rotation changes, recalculate the new vertex position from scratch. That will prevent error accumulation - if you keep your code running long enough, you will notice your vertices "drifting". – Roman Zenka Dec 17 '15 at 19:43

0 Answers0