Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have an XNA application (a force based visualisation of some circles) that I've implemented zooming and scrolling in, thanks to this guide.

Before I had the camera implemented, I had picking implemented (click and drag a circle to move it around). However, after the camera transformation I'm struggling to match up my mouse X,Y coordinates with the post-transformation coordinates of my circles.

I attempted to multiply my mouse X,Y vector with my camera transformation matrix. Given a mouse click at (356, 492), after translation I get (713,300), but my objects are around (-1000,900) or so.

So, given a mouse click in local X,Y coordinates and circle locations in post-transformation X,Y coordinates, how do I match them up?

(If it's needed, my VS2010 project is here.)

share|improve this question

2 Answers 2

up vote 5 down vote accepted

The view matrix transforms from world space to view space. Your mouse is in view space and you want to transform it into world space. That's the opposite of what the view matrix does. But the solution is simple - just multiply by the inverse view matrix instead.

Matrix inverseViewMatrix = Matrix.Invert(camera.ViewMatrix);
Vector2 worldMousePosition = Vector2.Transform(mousePosition, inverseViewMatrix);

You can then match that location against your world space circles.

share|improve this answer
    
Thanks, I now have a function to return the world coordinates of my mouse position, but it seems to be giving me unusual information. Dragging my mouse from left to right gives me extremely large values in world coordinates when I'm zoomed all the way in, but varies only slightly when I'm zoomed all the way out. –  Crummy Mar 17 '12 at 0:24
    
@Crummy Have you updated the file on your dropbox? I've downloaded it now and you're doing Matrix inverseWorldMatrix = camera.get_transformation(); which isn't actually inverting the matrix, so that would certainly be a problem. (Just nitpicking but it should also be an inverseViewMatrix, not an inverseWorldMatrix) –  David Gouveia Mar 17 '12 at 0:45
    
Wow, that was silly. I forgot that when I made a function of it. All working now; thanks very much! –  Crummy Mar 17 '12 at 2:38

You can also use method Viewport.Unproject(...).

share|improve this answer
    
I assume Crummy is working on a 2D spritebatch game because of the camera he linked. Therefore, although Viewport.Unproject is great for 3D games, I don't think it's a good choice for a 2D spritebatch game because, besides being a lot more expensive than what is really needed, you also need to pass it the projection matrix, and the spritebatch handles its own orthographic projection matrix internally. As far as I know you don't have access to this projection matrix, so you'd have to recreate it on your own. But for 3D games, it is indeed the best way to handle the problem. –  David Gouveia Mar 16 '12 at 20:41
    
My mistake. I haven't know he is making 2D game. ;) –  zacharmarz Mar 17 '12 at 8:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.