2
votes

I code racing game using libgdx library in java. It is 2D game where camera moves with a car (car is always in the center of the screen). I use orthographic camera. I want to display some text always on the left top corner of the screen. The problem is a width between left edge of the screen and the text is changing when car moves faster. I suppose that camera moves to a new position with some delay.

Here is my code:

Create camera:

    _camera = new OrthographicCamera();     
    float widthUnit = _cameraWidth;
    float heightUnit = widthUnit * (float)_screenHeight / (float)_screenWidth;      
    _camera.setToOrtho(false, widthUnit, heightUnit);   

Every frame set camera position:

    _batch.setProjectionMatrix(_camera.combined);
        _camera.position.set(_visibleRider.getPosition().x, _visibleRider.getPosition().y, 0);          
        _camera.update();   

Every frame draw text:

float leftPos = _camera.position.x - _camera.viewportWidth / 2f + 5f;

When my rider drives slowly, distance between left edge and text is 5 unit. Width is bigger when my rider drives fast to the right side. Width is smaller when driver drives fast to the left side. I want to show text always 5 units from the left side of the screen no matter how fast camera move. How can i do this? What am I doing wrong?

1

1 Answers

0
votes

You can always use separate stage for HUD (text or other UI elements). Those are supposed to be relative to the screen irrespective of the camera position.

It would simplify stuff as you won't need to keep up with the camera position and calculate text position every time.

Hope this helps.