I have a System.Drawing.Drawing开发者_JAVA百科2D.Matrix with rotation and translation transforms which I need to apply to a sprite. Unfortunately rotation origin is in the upper left corner of the sprite.
How to change a rotation origin to the center of the sprite?
At the moment your transformation matrix consists of a rotation (origin top-left), multiplied by a translation (from original top-left to new top-left).
To change the origin of the rotation, you need to do another translation first:
Translate (-half width, -half height) to centre the object
Then the rotation (now centred on the centre of the sprite)
Then the final translation (plus the half with and half height you took off in stage 1).
There's also a RotateAt()
method which you can use to specify the origin point you want.
just pass a PointF
structure in the argument where you want the origin to be.
http://msdn.microsoft.com/en-us/library/awacs0xh.aspx
Unfortunately, none of the proposed solutions are working for me. They seems to be a good solutions and probably will work on another scenarios but not in my case.
However, I found a solution which works:
I've changed values for OffsetX and OffsetY fields of the matrix to following:
OffsetX = OffsetX - M11 * hx - M21 * hy + hx;
OffsetY = OffsetY - M12 * hx - M11 * hy + hy;
Where hx is half width, hy is half height of the sprite and M11, M12 and M21 - corresponding matrix fields.
精彩评论