Sign in to follow this  
DpakoH

[XNA] 2D camera problem

Recommended Posts

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]



thank you in advance!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this