Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (808)
Games in Android Showcase (239)
games submitted by our members
Games in WIP (872)
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] Mouse coordinates all funky.  (Read 7149 times)
0 Members and 1 Guest are viewing this topic.
Offline MrPork
« Posted 2015-02-11 01:54:01 »

Alright, so whenever I call Gdx.input.getMouseX(); and test it out, I get insanely high numbers even though my screen width is 480. Now, the beginning (left) is still 0, but the end (right of screen) goes almost to 30,000. I'm not using Cameras or anything for my game so could that be the problem?





I cut off unimportant parts of this 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  
public class Player {
   private int x = 480 / 2, y = 100, reviveTime = 0;
   private Rectangle rect;
   private Texture playerTexture;
   private SpriteBatch batch;
   public boolean timedOut = false;

   public Player() {
      rect = new Rectangle(x, y, 32, 32);
      Texture.setEnforcePotImages(false);
      playerTexture = new Texture(Gdx.files.internal("data/Graphics/Orb.png"));
      batch = new SpriteBatch();
     

   }

   public void render() {
         
      batch.begin();
      batch.draw(playerTexture, x, y);
      batch.end();

   }

   public void update() {
      x = Gdx.input.getX(0)-75 /2;
     
      rect.setPosition(x, y);

     
      if (x < 0) {
         x = 0;
      }
      ;
      if (x > 480) {
         x = 480;
      }
      ;
     
         if(timedOut == true){
            revive();
         }
   }

"f**k it, maybe it'll work." -Me
Offline Jervac
« Reply #1 - Posted 2015-02-11 02:23:18 »

I don't think the problem is in this code, so would you care to share some more? Are you changing the player position somewhere else? A problem may be that you are setting the rectangle's position before the if statements check if x <0 or x > 480.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
public void update() {
      x = Gdx.input.getX(0)-75 /2;
     
      rect.setPosition(x, y); //<-------------- try putting this under the if statements

     
      if (x < 0) {
         x = 0;
      }
      ;
      if (x > 480) {
         x = 480;
      }
      ;
     
         if(timedOut == true){
            revive();
         }
   }
Offline MrPork
« Reply #2 - Posted 2015-02-11 02:29:27 »

Changing the position of that piece of code does nothing, and I do not modify the location of the player anywhere else. I would give you the rest of the code, but they're just getter methods. Thank you for trying to help though!  Huh Huh Huh

"f**k it, maybe it'll work." -Me
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline craftm

JGO Coder


Medals: 15
Projects: 1


_Keep Trying


« Reply #3 - Posted 2015-02-11 02:48:51 »

I suggest you to use a camera and do this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
if (Gdx.input.justTouched())
{
   vector3 newPos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);

   camera.unproject(newPos);
   
   System.out.println(newPos.x); //New X
   System.out.println(newPos.y); //New Y
}


You can override InputAdapter>MouseEvent with the above code.

_ Wink
Offline MrPork
« Reply #4 - Posted 2015-02-11 03:06:42 »

Alright, so i've added the unproject code you guys told me to in my update loop and nothing. What I have found out though is that nothing is being printed to the console which means that Gdx.input.justTouched() is never getting called. Whats going on???

1  
2  
3  
4  
5  
6  
if(Gdx.input.justTouched()){
         newPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
         camera.unproject(newPos);
         System.out.println(newPos.x); //New X
         System.out.println(newPos.y); //New Y
      }

"f**k it, maybe it'll work." -Me
Offline craftm

JGO Coder


Medals: 15
Projects: 1


_Keep Trying


« Reply #5 - Posted 2015-02-11 03:31:29 »

Alright, so i've added the unproject code you guys told me to in my update loop and nothing. What I have found out though is that nothing is being printed to the console which means that Gdx.input.justTouched() is never getting called. Whats going on???

1  
2  
3  
4  
5  
6  
if(Gdx.input.justTouched()){
         newPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
         camera.unproject(newPos);
         System.out.println(newPos.x); //New X
         System.out.println(newPos.y); //New Y
      }


justTouched() is a method called when you touch the screen (Android) or Mouse Click Event (Desktop), I don't know if you tried "click", but if you want a constant test:


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
Vector3 newPos = new Vector3();

update()
{
   newPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
   camera.unproject(newPos);

   System.out.println("Normal getX(): " + Gdx.input.getX() + " and Unproject X: " + newPos.x); // Print: Normal getX(): "SomeNumber" and Unproject X: "SomeNumber"
   System.out.println("Normal getY(): " + Gdx.input.getY() + " and Unproject Y: " + newPos.y); // Print: Normal getY(): "SomeNumber" and Unproject Y: "SomeNumber"
}

_ Wink
Offline MrPork
« Reply #6 - Posted 2015-02-11 10:42:24 »

Thanks mate! It done did worked!  Grin

"f**k it, maybe it'll work." -Me
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (847 views)
2019-09-04 15:33:17

hadezbladez (5795 views)
2018-11-16 13:46:03

hadezbladez (2603 views)
2018-11-16 13:41:33

hadezbladez (6211 views)
2018-11-16 13:35:35

hadezbladez (1499 views)
2018-11-16 13:32:03

EgonOlsen (4734 views)
2018-06-10 19:43:48

EgonOlsen (5793 views)
2018-06-10 19:43:44

EgonOlsen (3276 views)
2018-06-10 19:43:20

DesertCoockie (4175 views)
2018-05-13 18:23:11

nelsongames (5501 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04: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!