# [XNA] 2D camera problem

## Recommended Posts

DpakoH    1204
hi there,
in my current XNA project i am using a [url="http://webcache.googleusercontent.com/search?q=cache:N5Wn9mguUPAJ:www.david-amador.com/2009/10/xna-camera-2d-with-zoom-and-rotation/+xna+2d+camera+david&cd=1&hl=en&ct=clnk&gl=bg&source=www.google.bg"]simple 2d camera made by david amador[/url] ( i am giving you google cache as his site is down at the moment). the camera is working fine. what is puzzling me is how i can transform my mouse coordinates on the screen to world coordinates depending on the camera's transformation matrix. my math knowledge is obviously not enough, so i will be grateful for any information on the topic.

the way i am trying to solve the problem is not working, but i will post it here so you can give me guidance:

[code]

public Vector2 ConvertScreenToWorldCoordinates(Vector2 location)
{
var t = Vector3.Transform(new Vector3(location, 0), get_transformation());
return new Vector2(t.X, t.Y);
}

// transformation matrix from the camera class
public Matrix get_transformation()
{
_transform = Matrix.CreateTranslation(new Vector3(-_position.X, -_position.Y, 0)) *
Matrix.CreateRotationZ(Rotation) *
Matrix.CreateScale(new Vector3(Zoom, Zoom, 1)) *
Matrix.CreateTranslation(new Vector3(_graphics.Viewport.Width * 0.5f, _graphics.Viewport.Height * 0.5f, 0));

return _transform;
}[/code]

##### Share on other sites
DpakoH    1204
ok, (i think) i solved this.

the problem was that [b]get_transformation()[/b] gives us a matrix that we apply on [i]world coordinates to get screen coordinates[/i]! and to do the process in the opposite direction ([i]from screen to world coordinates[/i]) i have to take the inverse of the matrix and apply it on the coordinates.