Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (481)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (548)
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 projecting coordinates into world space  (Read 2597 times)
0 Members and 1 Guest are viewing this topic.
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Posted 2013-09-26 01:54:53 »

I know this is quite the newbie question, but I'm having trouble setting up mouse input in my game. The game world can scroll, and I'm trying to get the position of the mouse when it clicks. My game is a tiled game, so I take every mouse click and divide it by the tile size to try to get world coordinates. However, when I get the coordinates, they are crazy. The y is opposite of what I'm clicking (click at 0, 1 and I get 0, -1). I realize this is probably because my camera is set up with the origin to the bottom left, but when I try subtracting the window height, the y coordinate is still crazy. Its generally the same for the x coordinate, although I can't really tell if its doing the same as y because the x seems so sporadic... I thought about it and I decided that since my world can scroll, maybe that was what was messing it up. So I used unproject, but that did nothing. Here's my code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
if (Gdx.input.isTouched()) {
         Vector3 touchPos = new Vector3();
         touchPos.set(Gdx.input.getX() / 16, Gdx.input.getY() / 16, 0);
         cam.getCamera().unproject(touchPos);
                     
         if (touchPos.x >= 0 && touchPos.y >= 0 && touchPos.x <= tileWidth && touchPos.y <= tileHeight) {
            tiles[(int) touchPos.x][(int) touchPos.y] = Tile.base;
         }
      }

And my camera code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
package com.nishu.balancedEnergy.utilities;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.OrthographicCamera;

public class Camera {
   
   private OrthographicCamera camera;
   
   private float x, y;
   private float moveSpeed = 1f;
   private float zoom = 0.5f;
   
   public Camera(){
      camera = new OrthographicCamera();
      camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
      camera.zoom = zoom;
   }
   
   public void update(){
      camera.update();
      if(zoom <= 0){
         zoom = 0.1f;
      }
      if(zoom > 1){
         zoom = 1;
      }
      camera.zoom = zoom;
   }
   
   public void move(){
      if(Gdx.input.isKeyPressed(Keys.W)){
         y += moveSpeed;
      }
      if(Gdx.input.isKeyPressed(Keys.S)){
         y -= moveSpeed;
      }
      if(Gdx.input.isKeyPressed(Keys.A)){
         x -= moveSpeed;
      }
      if(Gdx.input.isKeyPressed(Keys.D)){
         x += moveSpeed;
      }
      if(Gdx.input.isKeyPressed(Keys.NUM_1)){
         zoom += 0.0625f;
      }
      if(Gdx.input.isKeyPressed(Keys.NUM_2)){
         zoom -= 0.0625f;
      }
      translate(x, y);
      x = 0;
      y = 0;
   }
   
   public void translate(float x, float y){
      camera.translate(x, y);
   }

   public OrthographicCamera getCamera() {
      return camera;
   }

   public void setCamera(OrthographicCamera camera) {
      this.camera = camera;
   }

   public float getX() {
      return x;
   }

   public void setX(float x) {
      this.x = x;
   }

   public float getY() {
      return y;
   }

   public void setY(float y) {
      this.y = y;
   }

}


So what am I doing wrong here?

Offline BurntPizza
« Reply #1 - Posted 2013-09-26 02:33:40 »

I had issues with unprojecting mouse coordinates a while back as well, which I documented here.
I still haven't found a good solution though, so I'm afraid my 'fix' is all I can help you with.
Note however that I am using a PerspectiveCamera and an isometric (ish) view of the tiles.
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #2 - Posted 2013-09-26 03:24:34 »

Well, thanks for the help but I don't know if any of that really applies to me :/

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jeremy
« Reply #3 - Posted 2013-09-26 04:11:41 »

I'm not sure what perspective you're talking about, top-down tile based, or isometric tile based?

If it is top-down perspective, you can just subtract your maps render offset and divide by your tile width\height.

If it's isometric, the best methods are to (aside from those provided by LibGdx as I am unfamiliar with the library myself)

(1) reverse the projection matrix (limited to tile-based picking),

(2) (inefficient in Java2D since you have to cache a processed version of that image just to render it into the colour map)  use a colour map, or

(3) (not efficiently doable in Java2D) use a depth buffer of some sort and pack the depth in to the pixel data (possible, but slow because you'll be using software shaders in Java2D.)

If your using JWJGL/OpenGL (3) is your best bet and it is by far the easiest of the three to pull off (assuming the library doesn't provide it's own way of doing that)

That said, I know a way of doing per-pixel picking in Java2D as well using bit sets etc, I haven't tested the idea out but I'm pretty sure it'd perform decently enough. It is a bit of an odd work around though, if anyone is interested.

JevaEngine, Latest Playthrough (This demo is networked with a centralized server model)

http://www.youtube.com/watch?v=rWA8bajpVXg
Offline Grunnt

JGO Wizard


Medals: 66
Projects: 8
Exp: 5 years


Complex != complicated


« Reply #4 - Posted 2013-09-29 19:25:03 »

Shouldn't you divide by 16 *after* the unproject? I mean the grid is in the world coordinates, not in screen coordinates, right? Also, you can indeed adjust for the negative values by subtracting the mouse y from the height:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
      if (Gdx.input.isTouched()) {
         Vector3 touchPos = new Vector3();
         touchPos.set(Gdx.input.getX() , Gdx.graphics.getHeight() - Gdx.input.getY(), 0);
         cam.getCamera().unproject(touchPos);
         touchPos.scl(1f/16f);
         
         if (touchPos.x >= 0 && touchPos.y >= 0 && touchPos.x <= tileWidth && touchPos.y <= tileHeight) {
            tiles[(int) touchPos.x][(int) touchPos.y] = Tile.base;
         }
      }


Does this help?

Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #5 - Posted 2013-10-06 02:23:05 »

Sorry I'm so late responding, I still am having the issue after trying to fix it for weeks.

I forgot to mention that I zoom my camera in and out, and at zoom level 1(no zoom pretty much), it works fine, as it should. My issue is that it doesn't work after I zoom in at all. I'm using an orthological camera.

Offline Grunnt

JGO Wizard


Medals: 66
Projects: 8
Exp: 5 years


Complex != complicated


« Reply #6 - Posted 2013-10-14 09:30:52 »

Your response is no response. You have received several responses and suggestions, maybe try starting there?

Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #7 - Posted 2013-10-14 10:06:06 »

Sorry, I did. None of them worked, scaling the vector probably came the closest to helping, but I still can only get true screen to world coordinates when the camera isn't zoomed at all.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

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

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

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

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

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

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

BurntPizza (38 views)
2014-08-09 21:09:32

BurntPizza (30 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38

BurntPizza (67 views)
2014-08-03 02:57:17
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!