Java-Gaming.org Hi !
 Featured games (91) games approved by the League of Dukes Games in Showcase (757) Games in Android Showcase (229) games submitted by our members Games in WIP (844) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 [Libgdx] Screen coordinates -> Isometric world coordinates  (Read 4796 times) 0 Members and 1 Guest are viewing this topic.
trollwarrior1
 « Posted 2014-05-25 09:53:38 »

I made this little thingy, which display kinda isometric floor. Here is how it looks without rotating the matrices.

As expected, finding on which tile mouse is currently at is really easy. Just divide mouse coordinates by tile size..

Here is the image of how it looks when I do my 'isometric' rotations (not sure if this is called isometric )

Here is the code for making this 'isometric' ground.
 1  2  3  4  5  6  7  8  9  10  11  12 `Matrix4 projectionMatrix = new Matrix4();      projectionMatrix.setToOrtho(0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), 0, -1, 1);            Matrix4 modelviewMatrix = new Matrix4();      modelviewMatrix.rotate(1, 0, 0, 35);      modelviewMatrix.rotate(0, 0, 1, 45);      projectionMatrix.tra();      modelviewMatrix.tra();            isoShader.setUniformMatrix(isoShader.getUniformLocation("u_projectionMatrix"), projectionMatrix, false);      isoShader.setUniformMatrix(isoShader.getUniformLocation("u_modelViewMatrix"), modelviewMatrix, false);`

How do I transform screen mouse coordinates to these world coordinates?

 1  2  3  4  5  6  7  8  9  10 `      float x = Gdx.input.getX();      float y = Gdx.input.getY();      float[] vec = { x, y, 0 };      Matrix4 transformationMatrix = projectionMatrix.cpy();      transformationMatrix.mul(modelviewMatrix);      Matrix4.mulVec(transformationMatrix.val, vec);      System.out.println(vec[0] + "  " + vec[1]);`

I tried this code, and in my head, it should work. But it gives not correct values.. As I point along the horizontal axis of the map, the Y is increasing..
trollwarrior1
 « Reply #1 - Posted 2014-05-31 19:19:15 »

Can somebody give me a medal? I managed to come up with a question that no one on this forum can answer
BurntPizza

« JGO Bitwise Duke »

Medals: 485
Exp: 7 years

 « Reply #2 - Posted 2014-05-31 19:22:43 »

Since the projection matrix takes screen coordinates to world coordinates, you need the inverse of the projection matrix to invert the process.

Spacebeans
 « Reply #3 - Posted 2014-05-31 19:24:21 »

I'm just a noobie to matrices, but this is the only information I found a long time ago on isometric mouse coordinates;

http://stackoverflow.com/questions/6915555/how-to-transform-mouse-location-in-isometric-tiling-map
UprightPath
 « Reply #4 - Posted 2014-05-31 19:36:08 »

Also, if you just need to know which of the tiles, and perhaps which of the objects, has been touched you can do something like give each tile a bounding box and check which the screen ray passes through first. You can also give your entities their own bounding boxes for this. There are many optimizations, such as if you know which tiles are being shown (Such as if you track which of your tiles are being culled during projection).

What you get? If you rotate your screen you use the same method of checking. If you make it a 2.5d game where there is a Z component for objects (Such as hills) you will get the 'higher' tiles if you click on them rather than the tiles behind them.

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16 `public Tile getTileTouched(float screenX, float screenY) {   Ray pickRay = camera.getPickRay(screenX, screenY);      Vector3 intersection = new Vector3();   Tile closestTile = null;   boolean distance = Float.MAX_NUMBER;   for(int i = 0; i < tiles.size; i++) {      if(Intersector.intersectRayBounds(pickRay, tile.get(i).boundingBox, intersection)) {         if(intersection.dst2(ray.origin) < distance) {            distance = intersection.dst2(ray.origin);            closestTile = tile.get(i);         }      }   }   return closestTile;}`

ctomni231

JGO Wizard

Medals: 99
Projects: 1
Exp: 7 years

Not a glitch. Just have a lil' pixelexia...

 « Reply #5 - Posted 2014-05-31 22:31:00 »

Edit: thanks matheus23, silly tablet auto correct...

matheus23

JGO Kernel

Medals: 138
Projects: 3

You think about my Avatar right now!

 « Reply #6 - Posted 2014-06-01 20:24:37 »

See my:
My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Pages: [1]
 ignore  |  Print

 EgonOlsen (45 views) 2018-06-10 19:43:48 EgonOlsen (27 views) 2018-06-10 19:43:44 EgonOlsen (47 views) 2018-06-10 19:43:20 DesertCoockie (211 views) 2018-05-13 18:23:11 nelsongames (128 views) 2018-04-24 18:15:36 nelsongames (127 views) 2018-04-24 18:14:32 ivj94 (868 views) 2018-03-24 14:47:39 ivj94 (129 views) 2018-03-24 14:46:31 ivj94 (780 views) 2018-03-24 14:43:53 Solater (144 views) 2018-03-17 05:04:08
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 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