Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (491)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (556)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [Libgdx] Screen coordinates -> Isometric world coordinates  (Read 562 times)
0 Members and 1 Guest are viewing this topic.
Offline 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 Cheesy)


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..
Offline 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 Cheesy
Online BurntPizza
« 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.

More info here: http://webglfactory.blogspot.com/2011/05/how-to-convert-world-to-screen.html
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 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
Offline 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;
}

Offline 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 »

This thread may also help...

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

Online matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


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

...

FTFY Smiley

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  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Nickropheliac (15 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (32 views)
2014-08-22 19:31:30

atombrot (41 views)
2014-08-19 09:29:53

Tekkerue (40 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (25 views)
2014-08-16 06:20:21

Tekkerue (37 views)
2014-08-16 06:12:11

Rayexar (72 views)
2014-08-11 02:49:23

BurntPizza (49 views)
2014-08-09 21:09:32
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!