c# - How to get coords inside a transformed sprite? -
i trying x , y coordinates inside transformed sprite. have simple 200x200 sprite rotates in middle of screen - origin of (0,0) keep things simple.
i have written piece of code can transform mouse coordinates specified x or y value.
int ox = (int)(mousepos.x - position.x); int oy = (int)(mousepos.y - position.y); relative.x = (float)((ox - (math.sin(rotation) * y /* problem here */)) / math.cos(rotation)); relative.y = (float)((oy + (math.sin(rotation) * x /* problem here */)) / math.cos(rotation));
how can achieve this? or how can fix equation?
the general way express transformation matrix. way, can add other transformation later, if find need it.
for given transformation, matrix is:
var mat = matrix.createrotationz(rotation) * matrix.createtranslation(position);
this matrix can interpreted system transformation sprite space world space. want inverse transformation - system transformation world space sprite space.
var inv = matrix.invert(mat);
you can transform mouse coordinates matrix:
var mouseinspritespace = vector2.transform(mousepos, inv);
and mouse position in sprite's local system.
you can check if have correct matrix mat
using overload of spritebatch.begin()
takes matrix. if pass matrix, draw sprite @ (0, 0) no rotation.
Comments
Post a Comment