Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (799)
Games in Android Showcase (235)
games submitted by our members
Games in WIP (865)
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] UI above characters' heads  (Read 1563 times)
0 Members and 1 Guest are viewing this topic.
Offline pateman

Senior Newbie


Exp: 6 years



« Posted 2019-07-30 07:10:49 »

Hello everyone,

I have the following setup in my project: a FillViewport(400, 200) which is used to render the world and now I want to add some UI to the game, so I plugged in a Stage without any custom viewport setup. All is fine for simple HUD like health, etc. - it behaves correctly (i.e. when the player moves, the elements move together with the player). But now I want to have create speech bubbles which will be displayed above the heads (or next to) of the characters I have in my game (please note that I don't use a Stage for my in-game characters and world).

How could I approach it? I tried using a different Stage for the speech bubbles, but they just kept following the player no matter which viewport configuration I tried. By 'following the player' I mean that when the camera moves, the UI moves along. I can share more information if you need.

Thank you,
Patryk
Offline LiquidNitrogen
« Reply #1 - Posted 2019-07-30 12:27:18 »

You might want to make use of the Camera project and unproject methods. Take the characters head position in world space - project that to screen space with the world camera. then take that screen space position and unproject it with the ui camera to get a viewport coordinates to draw the widget at (or something like that). I do pretty much the same process in 3d to give characters name tags.
Offline pateman

Senior Newbie


Exp: 6 years



« Reply #2 - Posted 2019-07-30 13:33:40 »

Holy cow, believe me or not, but I've actually tried to do that, but I messed up the order. Smiley Thanks so much, it worked!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline pateman

Senior Newbie


Exp: 6 years



« Reply #3 - Posted 2019-08-01 13:44:14 »

I have actually noticed a small problem, which I tried to illustrate in this GIF (it might take a moment to load):
https://i.imgur.com/V9kIcCN.gifv

If you take a look, you'll notice that the dialog box is moving - sometimes it's on the left edge of the small island, sometimes it moves towards the center.

Here's the code I'm using:
1  
2  
3  
4  
5  
    static Vector2 worldPosToViewport(Vector2 pos, Viewport gameCameraViewport, Viewport screenViewport) {
        Vector2 worldPosInScreenSpace = gameCameraViewport.project(pos);
        worldPosInScreenSpace.y = gameCameraViewport.getScreenHeight() - worldPosInScreenSpace.y;
        return screenViewport.unproject(worldPosInScreenSpace);
    }


Obviously, this method is called each frame.

What could be the problem?
Offline pateman

Senior Newbie


Exp: 6 years



« Reply #4 - Posted 2019-08-02 10:12:07 »

Here's how I fixed the problem:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
    static Vector2 worldPosToViewport(Vector2 pos, Viewport gameCameraViewport, Viewport screenViewport) {
        TempVars tempVars = TempVars.get();

        Vector3 worldPos = tempVars.vect3d1.set(pos, 1.0f);

        Vector3 worldPosInScreenSpace = gameCameraViewport.getCamera().project(worldPos);
        worldPosInScreenSpace.y = gameCameraViewport.getScreenHeight() - worldPosInScreenSpace.y;
        Vector3 screenPosInViewportSpace = screenViewport.getCamera().unproject(worldPosInScreenSpace);
        pos.set(screenPosInViewportSpace.x, screenPosInViewportSpace.y);

        tempVars.release();
        return pos;
    }


Methods in the Viewport class perform some additional changes to the output, which seem to mess up things for me a bit. FYI, TempVars is taken from jMonkeyEngine, but you can freely replace the code with a simple Vector3 field in your class for temporary operations.
Pages: [1]
  ignore  |  Print  
 
 

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

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

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

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

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

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

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

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

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

nelsongames (4157 views)
2018-04-24 18:15:36
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!