[solved] Vector2.Transform(Vector2, Matrix) returns {X:n. def. Y:n. def.}

I’m trying to convert a Vector2 screen position to a world position. I’ve found this nice method in a question on stackoverflow:

public Vector2 ScreenToWorld(Vector2 onScreen)
    var matrix = Matrix.Invert(World.Camera.CurrentTransformation);
    return Vector2.Transform(onScreen, matrix);

But when I use this with the Matrix of my camera it only returns this:

{X:n. def. Y:n. def.} // Output of Vector2.ToString()

My Matrix is build like this:

m_transform = Matrix.CreateTranslation(new Vector3(-m_position.X, -m_position.Y, 0f)) *
    Matrix.CreateScale(new Vector3(m_zoom, m_zoom, 0)) *
    Matrix.CreateTranslation(new Vector3(m_viewport.Width / 2f, m_viewport.Height / 2f, 0));

When I try to compare the result with other Vector2 positions it allways return true. For Example, I have a clickable object, and even if I click outside of the object it returns that it is being clicked.

Had a look into how Vector2.Transform() works.

return new Vector2((point.X * matrix.M11) + (point.Y * matrix.M21) + matrix.M41, (point.X * matrix.M12) + (point.Y * matrix.M22) + matrix.M42);

And I also had a look into my Matrix and checked the used values.

// M11: 0.325, M12: 0, M21: 0, M22: 0.325, M41: 110.5, M42 279.5

But theese are the values for the actual Matrix, right? So I do Matrix.Invert() and look them up again…

// M11: {n. def.}, M12: {n. def.}, M21: {n. def.}, M22: {n. def.}, M41: {n. def.}, M42 {n. def.}

It’s way past bed time here. I will take a look into how Matrix.Invert() works tomorrow. Just wanted to leave this as an update.
I don’t really think all this will lead me to a solution, so pleeease for god’s sake help me :smile:

Maybe scale should have been (m_zoom, m_zoom, 1)? I don’t know.

If you are just starting with this kind of math you’d better stick to the standard methods to create a View and projection matrix (Matrix.CreateLookAt(), Matrix.CreateOrthographic()),
and the stock methods to convert from and to screen space (Viewport.Project() and Viewport.Unproject())

Thing is, when you are just starting with this kinda stuff, you have no damn clue what those standard methods are. Or how they are used. :stuck_out_tongue: thx for your tip tho :smiley:

Maybe scale should have been (m_zoom, m_zoom, 1)? I don’t know.

oh you doo :smiley: because that is the solution to my problem. Thank you sir! :smiley:

1 Like

Method’s name say it all :wink:

Just a lucky guess. :slight_smile: