You need to define a center for rotation... in this case the box center..
then when you detect the mouse down you store vector (A) from the center to the mouse coords... this is your initial vector.... ans should store your shape rotation as initial rotation.
then while you move the mouse and it continues being down you calculate the second vector (B) from the center to the current mouse coords...
now you can calculate the angle between to vectors... as you should know it can be calculated normalizing the two vectors and calcultaing the dot product... so you get the cosine... now you get the angle with an arccosine function...
this angle should be added to the initial rotation of your shape... ans set the new rotation for your shape.
so you only have to build your transform matrix for your shape...
the easiest way is to store your vertex coords relative to the center of the shape, if you don't store that way you should get that points relative to it...
Your transform matrix should similar to this...
// Supposing your coords are absolute
Matrix transform = Matrix.CreateTranslation( -Center )
* Matrix.CreateRotationZ(rotation)
* Matrix.CreateTranslation (Center );
// Supposing your coords are relative, center is (0,0)
Matrix transform = Matrix.CreateRotationZ(rotation)
You should realize that usually the right way is the easy way... you should use matrix transforms... I'd do something similar to this:
class Bone2D {
Vector2 Translation;
float Rotation;
float Scale;
Bone Parent;
Matrix Local { get { return Matrix.CreateScale(scale)
* Matrix.CreateRotationZ(rotation)
* Matrix.CreateTranslation(translation); } }
Matrix Absolute { get{ return (Parent == null)
? Local
: Parent.Absolute * Local; }}
}
class Shape : Bone2D {
Vector2[] RelativeCoords; // Relative to your shape center
Vector2[] AbsoluteCoords {
get{
return RelativeCoords.Select(r => Vector2.Transform(r, Absolute)
.ToArray();
} }
}
This way the shape you show in your example, would have two bones...
1) the parent bone that translates the shape,
2) the shape that is a bone itself and can be rotated easily... or scaled or translated again relative to the parent bone of course... ;)
This editor is quite old... I have a new version... but the video shows what you can achieve doing it right... http://www.youtube.com/watch?feature=player_detailpage&v=K9R5v5Va4CI#t=28s
I hope this to be useful... ;)