• Advertisement
Sign in to follow this  

[XNA] 2D camera problem

This topic is 2473 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi there,
in my current XNA project i am using a simple 2d camera made by david amador ( 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:




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;
}




thank you in advance!

Share this post


Link to post
Share on other sites
Advertisement
ok, (i think) i solved this.

the problem was that get_transformation() gives us a matrix that we apply on world coordinates to get screen coordinates! and to do the process in the opposite direction (from screen to world coordinates) 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
Sign in to follow this  

  • Advertisement