World Co-Ordinates for Mouse in Screen Space for 2D
Monday, April 6th, 2009 at
2:09 pm
Well I managed to fix the problem I was having in my previous post. This works for both translations in X and Y, and also for camera scale (zoom level). I haven’t figured out how to include rotation yet but as that isn’t particularly important for what I’m trying to do I’ll let that slide for now.
Assuming you have a 2D camera class that implements a transformation matrix like so:
transform = Matrix.CreateScale( new Vector3((scale * scale * scale), (scale * scale * scale), 0)) * Matrix.CreateRotationZ(rotation) * Matrix.CreateTranslation(new Vector3( position.X, position.Y, 0));
The world space mouse co-ordinates can be obtained like so:
float mouseWorldX = (mouse.X - camera.Position.X) / (float)Math.Pow(camera.Scale, 3); float mouseWorldY = (mouse.Y - camera.Position.Y) / (float)Math.Pow(camera.Scale, 3);
Filed under: Game Design
Like this post? Subscribe to my RSS feed and get loads more!

Leave a Reply