How to rotate vertices?

Hi,

Does anyone have an example of rotating vertices (as used when creating a b2PolyDef shape) ?

For example I have this triangle:

Code:
shapeDef.vertices[0].Set( -1, 0);
shapeDef.vertices[1].Set( 1, 0);
shapeDef.vertices[2].Set( 0, -2 );


and here is a "house" shape:

Code:
shapeDef.vertices[0].Set( 1, 0 );
shapeDef.vertices[1].Set( -1, 0 );
shapeDef.vertices[2].Set( -1, -2 );
shapeDef.vertices[3].Set( 0, -3 );
shapeDef.vertices[4].Set( 1, -2 );


How could I rotate each vertice in the examples above to any angle?

I thought it was something to do with sin() and cos() on the x/y values, but I'll be blown if I can get it working right!
is there a reason for rotating this way? is creating the shape and then just setting the angle of the body out of the question?
That will work in some very basic instances, for example when the overall body is symmetrical. But if the body consists of multiple shapes then you can't use rotation (as it'll effect all shapes within the body, as it should).
Well if you were to just rotate the vertices around the origin you could just use a rotation matrix multiplied by a matrix consisting of the vertices

For example on the triangle.
Code:
X   Y  1       A is the angle
(-1, 0, 1 )   (cos(a),sin(a),   0)
( 1, 0, 1 ) X (-sin(a),cos(a), 0)
( 0,-2, 1 )   (0       , 0       , 1)


You will now have a matrix that consists of the rotated points. (the first two columns of each row is where the new rotated points are held)

But to rotate about an arbitrary point(which will be most likely the center of your shape) you need to translate the points from the arbitrary point to the origin, rotate,
and then translate them back to the original arbitrary point.

so for the triangle again
Code:
(Translation Matrix)     (Rotation Matrix)            (Translation Matrix back)      The Matrix of Vertices
(x y arbitrary points)    A is the angle                (x y arbitrary points)                X   Y  1
( 1, 0, 0)                  (cos(a),sin(a),   0)            ( 1, 0, 0)                        (-1, 0, 1 )
( 0, 1, 0)         *       (-sin(a),cos(a),  0)  *          ( 0, 1, 0)            *           ( 1, 0, 1 )
(-x,-y,1)                   (0       , 0       , 1)          (-x,-y,1)                         ( 0,-2, 1 )

Although you can concatenate the first three matrices into:
Code:
(cos(a),                             sin(a),                          0)
(-sin(a),                            cos(a),                          0)
((-x*cos(a) + y*sin(a) + x), (-x*sin(a)-y*cos(a) + y),               1)

So you can use that matrix to multiply it by the matrix of vertices.
Code:
(cos(a),                             sin(a),                          0)          (-1, 0, 1 )
(-sin(a),                            cos(a),                          0)  *      ( 1, 0, 1 )
((-x*cos(a) + y*sin(a) + x), (-x*sin(a)-y*cos(a) + y),                1)          ( 0,-2, 1 )


WARNING!

I only remember this from geometry class last year. I'm positive this is how to do it on paper, but I have never used it in an application.

Google rotation matrices and rotation about an arbitrary point (Homogeneous space) for more help.
Perhaps you ought to make two b2Bodies, one containing one b2PolygonShape and the other body containing the other poly, and then rotate them relative to each other using a b2RevoluteJoint? That way, Box2D handles the matrix transformations and/or trigonometry for you, and if there is an obstruction preventing the rotation, it will stop rotating just as it would in real life.

I don't know precisely what the effect you're going for is, but it's pretty rare that a Box2D user wants to rotate polygons independently on the same body instead of in separate bodies with a revolute joint.
It's a reasonable thing to want to do IMHO. A good example would be placing multiple polygons inside the ground body, which you are clearly not going to rotate.
Creating two bodies and using a revolute joint is a bad idea as this puts unneccessary strain on the engine, and is not numerically as accurate (though you sometimes want this, as observed). The trigonometry is probably correct, but is more work thank you need. Instead, we can make Box2D work for us:
Code:
var angle:Number = 45 * (Math.PI / 180);
var mat:b2Mat22 = new b2Mat22(angle);
for(var i:Number=0;i<myVecs.length;i++){
  myVecs[i]=b2Math.b2MulMV(mat,myVecs[i]);
}

You can do basically the same thing with XForms if you want some translation as well.
That is excellent Boris, thank you 🙂

Will try it out now!

Original thread: https://box2d.org/forum/viewtopic.php?t=897