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);
Sunday, April 5th, 2009 at
7:30 pm
I’ve actually started working on something. The last couple of days I have started work on a Map Editor for an undefined game, or game type, hence going against all principles of game design and development. All that is defined at this stage is that it will be a 2D environment.
The editor allows complete camera control (rotation, translation and zoom) based partly on the Tiled Sprites Sample over at Creators.XNA.Com website. That all works properly. I have a collsion grid in place too, and it’s there that I have the problem.
I cannot manage to deduce the co-ordinates of the mouse in world space when positioned over the grid (or indeed any object in the scene). Viewport.Unproject doesn’t seem to work, and probably isn’t meant to as there are no ‘real’ 3D projection/world matrices in play with this scenario.
Taking into account only translation in X and Y, I can retrieve the co-ordinates just fine, but any change in scale (camera zoom) causes problems.
So work on the editor is now on hold until I can find some help to get this issue resolved.