I have been working on a simple 2D side-scroller in Java/LWJGL, and decided early on to avoid using the deprecated legacy functions; instead I have set up my own model, view, and projection matrices in a "Matrices" class where they can be initialized and manipulated independently of one another. In creating the side-scroller engine I quickly realized that I require a method which takes in mouse "screen" coordinates and converts them to "world"(forgive me if this terminology is incorrect) coordinates.

It returns a Vector2f that does this perfectly when the view matrix is at (0,0), but when moved away from zero the position upon which the returns are based becomes distorted. For example if I leave the pointer in the center of the screen and track view matrix coordinates, when the camera is above (10,10) my world coordinates say they are at (10.26, 5.77) The calculation still returns good results relative to the distorted position(meaning that an object that is .5 width will measure exactly so if points are taken with the mouse cursor's world coordinates according to the above method), so I know I didn't get it entirely wrong. I have tried to figure out where I went wrong for a few hours now and I'm stumped.

Could any of you inform me as to my folly?

I realize that I could just calculate the difference between the distorted point and the actual view location and apply offsets to the out(x,y) for good world coordinates, but I'd much rather figure out why my method didn't work and learn from the error.

Edit:

I just "solved" the problem by multiplying the out.x with the xScale value in position 00 of the projection matrix. Same with out.y and yScale. I still feel like I am doing this wrong and shouldn't have needed to take this additional step though.

Edit 2:

Nevermind, this fixed the distorted point but screwed up the x,y values when you move the cursor away from the center. It inverted my problem.

I solved my problem after sleeping on it and calculating some things by hand. I had forgotten to include the model matrix in my initial(failed) attempts. It was also necessary to set the in.z value to that which I plugged into my projection matrix at position 3,2. I added a note in the code specifying exactly how that was derived.

Code below for others who may come to this thread looking for a similar solution:

That's it. It takes a Vector2f parameter(which for my game are my mouse's screen coordinates), and returns a Vector2f which represents the mouse's position in the game world. Object space or whatever it is called?

You could use a reference-point. Whenever you move the camera, you move this point the exact same amount. Then it could keep track of the position of the topleft, and then you add in the mouse-position on the screen after that.

I have a render-offset like this, to make sure the player is in the middle of the screen in all resolutions using g.translate. I use it for almost everything I render, actually. It's so easy to do rendering-code with screen-restrictions (render only tiles on the screen) when you always have a knowledge about where the screen is. I've tied mine to playerPosition right now, but it'll be easy to add "movement-cushion" later, so it doesn't move statically with the player, but kinda follows him instead.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org