Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (794)
Games in Android Showcase (234)
games submitted by our members
Games in WIP (864)
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 after resizing screen  (Read 408 times)
0 Members and 1 Guest are viewing this topic.
Offline FabulousFellini
« Posted 2019-05-15 17:56:13 »

What's the best way to do this?  I've read it's done by converting world coordinates to screen coordinates.  I've also read you can use camera.unproject().    (I'll be honest and haven't tried anything yet, I just wanted to have something to go off of when I get off work).  I want to try the camera.unproject method.  Does this method go into the resize() method in LibGDX?  Or do I call unproject when the mouse actually clicks if the game is full screen?   

Basically, the problem is my screen size is set for Android-like, and I have an inventory screen where you can click on different weapons to select which weapon you want to use.  But if the screen is resized, this all breaks and you can't click on anything to select inventory (not even where the original coordinates started...so basically if you resize the screen to full screen and click on where the inventory should be before resizing, that doesn't work either)  Is there something I'm missing?  Thanks and have a good day Smiley

-FabulousFellini
www.fabulousfellini.com
Offline VaTTeRGeR
« Reply #1 - Posted 2019-05-15 20:39:21 »

This stuff is taken directly from my game project and it should work for you and also explain some concepts.

First you need to setup your Camera and Viewport, the Scaling mode is important, read the LibGDX-Wiki and test some Scaling modes yourself to see the difference:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
private void setupCamera() {
   
   camera = new OrthographicCamera();
   
   // ww and hw are your world width and height in world units (meters for example!)
   // Read the LibGDX Wiki on viewports and try different types
   viewport = new ScalingViewport(Scaling.fit, Metrics.ww , Metrics.hw, camera);
   
   // [...]
   
}



Then you need to calculate your viewport size, do this in your games resize-method, it is called when needed by LibGDX, read the Wiki if you need to know more:
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  
@Override
public void resize(int width, int height) {
   
   // width and height of the window in pixels
   Metrics.wv = width;
   Metrics.hv = height;
   
   // I want 1:1 texel to pixel mapping on screen for zoom=1.
   // You can choose how much of your world you show here, you could also choose a constant width OR height in meters instead.
   // world width and height in meters
   // window_width_in_pixels * zoom / pixel_per_meter
   Metrics.ww = Metrics.wv * camController.getZoom() / Metrics.ppm;
   // window_height_in_pixels * zoom / pixel_per_meter
   Metrics.hw = Metrics.hv * camController.getZoom() / Metrics.ppm;
   
   // World viewport uses world coordinates (meters) so we pass ww (WidthWorld) and hw (HeightWorld).
   viewport.setWorldSize(Metrics.ww , Metrics.hw);
   // Always input your screen width and height
   viewport.update(Metrics.wv, Metrics.hv, true);
   
   // My stage on the other hand uses pixel coordinates so i pass wv (WidthView) and hv (HeightView).
   stage.getViewport().setWorldSize(Metrics.wv, Metrics.hv);
   // Always input your screen width and height
   stage.getViewport().update(Metrics.wv, Metrics.hv, true);
}


Now the fun part, getting the position in world coordinates where the mouse pointer is:
1  
2  
3  
4  
5  
6  
7  
8  
// v get's overwritten and returned
public static Vector3 mousePositionInWorld(Vector3 v, Camera camera) {

   v.set(Gdx.input.getX(), Gdx.input.getY(), 0f);
   camera.unproject(v);

   return v;
}


Getting the screen-position in pixels from a point in your game world:
1  
2  
3  
4  
5  
6  
7  
// v should contain the world position you want to convert
public static Vector3 worldToScreenPosition(Vector3 v, Camera camera) {

   camera.project(v);

   return v;
}


The most important step is to always update your viewport! This works irrespective of screen size, fullscreen or not.
Offline FabulousFellini
« Reply #2 - Posted 2019-05-15 20:57:06 »

Ok thanks!  I'll try some of this out and see what's up.  Thanks man!  I thought it would be easier than this lol.  I.E. just resize the window and it works.   Definitely not the case here.

-FabulousFellini
www.fabulousfellini.com
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline VaTTeRGeR
« Reply #3 - Posted 2019-05-15 21:09:15 »

Quote
just resize the window and it works. Definitely not the case here.

Errmm ... it actually is that simple, you just resize and update your viewport in that resize method that gets called automagically and you're set Cheesy

This is the super condensed version:

1  
2  
3  
4  
5  
6  
7  
@Override
public void resize(int width, int height) {
    // Viewport => How much of your world you want to display in the window.
    // You basically use this to adjust after a resize and for zooming in/out. This has no effect on the camera position/rotation.
    viewport.setWorldSize(viewportWidthInWorldUnits , viewportHeightInWorldUnits);
    viewport.update(width, height, true);
}


That's literally it. Don't forget to set your sprite sizes in world units though, that's another easy mistake.
Pages: [1]
  ignore  |  Print  
 
 

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

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

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

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

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

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

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

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

nelsongames (3549 views)
2018-04-24 18:15:36

nelsongames (4599 views)
2018-04-24 18:14:32
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

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