I have a sprite object in XNA.
It has a size, position and rotation.
How to translate a point from the screen coordinates to the sprite coordinates ?
Thanks,
SW
views:
870answers:
4
A:
One solution is to hit test against the sprite's original, unrotated bounding box. So given the 2D screen vector (x,y):
- translate the 2D vector into local sprite space: (x,y) - (spritex,spritey)
- apply inverse sprite rotation
- perform hit testing against bounding box
The hit test can of course be made more accurate by taking into account the sprite shape.
Peter Lillevold
2009-12-17 09:26:11
A:
I think it may be as simple as using the Contains
method on Rectangle
, the rectangle being the bounding box of your sprite. I've implemented drag-and-drop this way in XNA; I believe Contains
tests based on x and y being screen coordinates.
Kyralessa
2009-12-17 18:46:35
But does the bounding box take into account that the sprite is rotated?
Peter Lillevold
2009-12-18 07:38:04
+1
A:
You need to calculate the transform matrix for your sprite, invert that (so the transform now goes from world space -> local space) and transform the mouse position by the inverted matrix.
Matrix transform = Matrix.CreateScale(scale) * Matrix.CreateRotationZ(rotation) * Matrix.CreateTranslation(translation);
Matrix inverseTransform = Matrix.Invert(transform);
Vector3 transformedMousePosition = Vector3.Transform(mousePosition, inverseTransform);
Aphid
2010-01-05 20:25:59