Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (582) Games in Android Showcase (163) games submitted by our members Games in WIP (633) 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 1242 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: 390
Exp: 6 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: 121
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

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

 Gibbo3771 (11 views) 2015-05-29 06:54:17 Ralphanese (20 views) 2015-05-28 16:52:52 theagentd (31 views) 2015-05-27 22:21:17 theagentd (35 views) 2015-05-27 22:21:08 CopyableCougar4 (25 views) 2015-05-27 19:24:50 MrMapcom (24 views) 2015-05-23 20:26:16 MrMapcom (32 views) 2015-05-23 20:23:34 Waterwolf (37 views) 2015-05-20 15:01:45 chrislo27 (44 views) 2015-05-20 03:42:21 BurntPizza (79 views) 2015-05-10 15:53:18
 Rayvolution 24x theagentd 21x Spasi 18x Riven 18x BurntPizza 17x orangepascal 15x Drenius 15x ra4king 13x opiop65 12x EgonOlsen 12x princec 12x DavidBVal 11x Husk 11x KevinWorkman 10x kevglass 9x scanevaro 8x
 Intersection 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:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02
 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