Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (595) Games in Android Showcase (169) games submitted by our members Games in WIP (647) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Getting screen-to-world coordinates  (Read 1697 times) 0 Members and 1 Guest are viewing this topic.
Dotson

Junior Newbie

 « Posted 2012-08-23 21:55:15 »

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.

I created a method in my Matrices class for this task:
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18 `public Vector2f getScreentoWorldCoordinates(Vector2f screenCoordinates){    //Create the transformation matrix //    viewProjectionMatrix = new Matrix4f();    Matrix4f.mul(viewMatrix, projectionMatrix, viewProjectionMatrix);    viewProjectionMatrix.invert();    //Set input vectors to values -1 to 1 //    float xIn = (screenCoordinates.x / screenWidth) * 2.0f - 1.0f;    float yIn = (screenCoordinates.y / screenHeight) * 2.0f - 1.0f;    float zIn = 0.5f; //This is probably not right, but it shouldn't matter for 2D //    float wIn = 1.0f;    //Create the vector to be transformed, and the destination vector //    Vector4f in = new Vector4f(xIn, yIn, zIn, wIn);    Vector4f out = new Vector4f();    //Transform the vector //    Matrix4f.transform(viewProjectionMatrix, in, out);    //Return the mouse position in world coordinates //    return new Vector2f(out.x, out.y);}`

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.
Dotson

Junior Newbie

 « Reply #1 - Posted 2012-08-24 16:49:51 »

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:
 1  2  3  4  5  6  7  8  9  10  11  12  13  14 `public Vector2f getScreentoWorldCoordinates(Vector2f screenCoordinates){      transformationMatrix = new Matrix4f();      Matrix4f.mul(modelMatrix, viewMatrix, transformationMatrix);      Matrix4f.mul(projectionMatrix, transformationMatrix, transformationMatrix);      transformationMatrix.invert();      float xIn = (screenCoordinates.x / screenWidth) * 2.0f - 1.0f;      float yIn = (screenCoordinates.y / screenHeight) * 2.0f - 1.0f;      float zIn = -0.2002002002002002f; //-(2*zNear*zFar) / (zFar-zNear)      float wIn = 1f;      Vector4f in = new Vector4f(xIn, yIn, zIn, wIn);      Vector4f out = new Vector4f();      Matrix4f.transform(transformationMatrix, in, out);      return new Vector2f(out.x, out.y);   }`

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?

Works perfectly!
Ultroman

JGO Knight

Medals: 25
Projects: 1

Snappin' at snizzes since '83

 « Reply #2 - Posted 2012-08-24 19:13:03 »

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.

- Jonas
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 KaiHH (15 views) 2015-07-07 11:39:58 deepthought (54 views) 2015-06-30 15:39:44 deepthought (57 views) 2015-06-30 15:39:09 deepthought (66 views) 2015-06-30 15:36:52 Za\'Anzabar (44 views) 2015-06-29 05:44:54 TritonDreyja (50 views) 2015-06-24 17:10:40 CopyableCougar4 (52 views) 2015-06-23 00:34:45 BurntPizza (58 views) 2015-06-21 20:36:46 cookiecompiler (100 views) 2015-06-11 15:42:53 cookiecompiler (58 views) 2015-06-11 15:41:14
 princec 31x orangepascal 22x wessles 21x CopyableCougar4 19x EgonOlsen 18x BurntPizza 17x opiop65 17x theagentd 17x ags1 15x nsigma 15x Riven 14x KaiHH 13x SauronWatchesYou 11x KevinWorkman 11x sunburn 11x Jesse 11x
 How Do I Expand My Game?by bashfrog2015-06-14 11:34:43List of Learning Resources2015-05-31 05:37:30Intersection Methodsby Roquen2015-05-29 08:19:33List of Learning Resources2015-05-05 10:20:32How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14
 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