Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (799)
Games in Android Showcase (236)
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 display text on screen with camera movement.  (Read 209 times)
0 Members and 1 Guest are viewing this topic.
Offline FabulousFellini
« Posted 2019-12-03 14:26:11 »

I implemented a loot/money system using text with LibGDX.  It displays how much money you have in the HUD.

The problem: The rendering of the money value does not move smoothly with the camera.  Basically, it retains it's int position, and not it's float position.  This is bad because it follows the camera and player and supposed to be rendered according to where the player is in the world.  This makes the money display kind of just shoot over constantly when the player hits a new tile.   

The player moves on the x, y axis with float values, which is how the "money display" is set up (so the x and y positions are floats, not ints).  However, it does not move smoothly...the money display wont move until the player gets to a certain int (not float).   So if the player is moving from tile one to tile two, and it takes him 10 frames to do this smoothly, the money display will retain it's original position at tile one, and when the player hits tile two, the money display will shoot over to the correct position at tile two, but disregard the movement between tile one and two.

I can upload a video of this after work if it doesn't make sense.

But my main question is, should I even be using the draw text method in LibGDX for a game that moves around a lot?   Or should I have images (which do move correctly with the camera and player).  How do you guys display text, and if you use the drawing text method in LibGDX, how do you make it have smooth movement?  (I already googled and looked on here but didn't really find a nice solution).

Thanks.

-FabulousFellini
www.fabulousfellini.com
Offline FabulousFellini
« Reply #1 - Posted 2019-12-03 14:29:10 »

Here is the code where the money display is rendered:

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  
package ui;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.mygdx.mygame.MyGame;

import gameobjects.GameObject;
import loaders.ImageLoader;

/**
 * Extend GameObject to use animation variables.
 *
 * @author Fabulous Fellini
 *
 */

public class TextBasedUiParent extends GameObject {

   protected BitmapFont font;

   /**
    * Constructor.
    */

   public TextBasedUiParent() {
      // The true paramater signifies font is flipped.
      font = new BitmapFont(true);
      // Uncomment this to make text seem smoother.
      //font.getRegion().getTexture().setFilter(TextureFilter.Linear, TextureFilter.Linear);
      float scale = 0.1f;
      font.getData().setScale(scale, scale);
      font.setColor(Color.BLACK);
   }

   /**
    *
    * @param SpriteBatch batch
    * @param ImageLoader imageLoader
    * @param MyGame      myGame
    * @param String      text
    * @param GameOjbect  player
    * @param float       xOffset
    * @param float       yOffset
    */

   public void renderUi(
         SpriteBatch batch,
         ImageLoader imageLoader,
         MyGame myGame,
         String text,
         GameObject player,
         float xOffset,
         float yOffset
         ) {
      font.draw(
            batch,
            text,
            player.getX() - xOffset,
            player.getY() + yOffset
            );
   }
}

-FabulousFellini
www.fabulousfellini.com
Offline FabulousFellini
« Reply #2 - Posted 2019-12-03 15:47:49 »

And here is where the UI is rendered:   maybe this is the problem, or has to be:

1  
2  
3  
4  
5  
6  
7  
8  
9  
public void renderUserInterface(SpriteBatch batch, ImageLoader imageLoader, MyGame myGame) {
      if (!CutScene.anyCutSceneIsInProgress) {
         healthUi.renderHealthUi(batch, imageLoader, myGame);
         GameObject player = PlayerController.getCurrentPlayer(myGame);
         lootUi.renderUi(batch, imageLoader, myGame, player.convertPlayerLootToString(), player, 11.0f, -6.0f);
         playerNameUi.renderUi(batch, imageLoader, myGame, player, 10.5f, 6.0f);
         selectedInventoryUi.renderSelectedInventoryUi(batch, imageLoader, myGame, player);
      }
   }

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

lootUi.renderUi(batch, imageLoader, myGame, player.convertPlayerLootToString(), player, 11.0f, -6.0f);

That line has to be the problem...11 and -6 should probably reflect the player.getX() - 11.0, and same with the y pos.   Thanks guys, writing this out really helped.  If anyone sees a different problem let me know, otherwise I think my solution may work.

Also, I tried the game on my girls' computer as well as work computer and it runs flawlessly, so all my slow downs developing must be from my shitty old computer.   But if I can get it to run on that it should run on anything, so that's good.

-FabulousFellini
www.fabulousfellini.com
Offline abcdef
« Reply #4 - Posted 2019-12-03 16:23:22 »

I'm not familiar with libgdx but you should be using a different projection matrix for hud's, one that is orthographic rather than a perspective. The perspective matrix gets used with a view matrix (camera) but your orthographic one doesn't. It's stationary and never moves.
Offline FabulousFellini
« Reply #5 - Posted 2019-12-03 16:30:54 »

I'm not familiar with libgdx but you should be using a different projection matrix for hud's, one that is orthographic rather than a perspective. The perspective matrix gets used with a view matrix (camera) but your orthographic one doesn't. It's stationary and never moves.

Ok, it looks like I have some research to do.   When I started this project, I attempted to make 2 screens on top of one another.   So the game screen was below the UI screen.   But I couldn't get the screens to work very well, so I decided to just render everything on the main game screen (with the UI getting rendered over everything).

I will try your method after work and see if I can get it to work.   Thanks@abcdef

-FabulousFellini
www.fabulousfellini.com
Pages: [1]
  ignore  |  Print  
 
 

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

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

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

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

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

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

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

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

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

nelsongames (4293 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!