Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Using multiple views for level and entities  (Read 4832 times)

0 Members and 1 Guest are viewing this topic.

MrOnlineCoder

  • Jr. Member
  • **
  • Posts: 87
    • View Profile
Using multiple views for level and entities
« on: February 01, 2018, 03:24:44 pm »
Hello. I need small tip on using views for my game.

Currently, I load and render Tiled (.tmx) level as a game level using one view called levelView, which represents "camera", so it is moved when the character moves. Also, that view is a little bit scaled, because my level tileset has tiles of size 16x16, so without lowering view size level appears really tiny on the screen.

Then, I have another view - playerView, which is the same as window default view, it is used to draw player UI and the player character sprite in the middle of the screen (the character is not moving, only camera (levelView) is moved).

Now, I need to draw entities: items, enemies. But how can I draw their positions correctly (moving along with the camera)? Which view should I use?

If I draw them with playerView - they appear statically on the screen, like an HUD element. And if I draw them with levelView - they appear really scaled (the quality of texture decreases).

Thanks in advance.

Paul

  • Jr. Member
  • **
  • Posts: 79
    • View Profile
Re: Using multiple views for level and entities
« Reply #1 on: February 01, 2018, 05:52:02 pm »
Hello,

I think it should be more like this:
levelView = level/map and all objects (player + enemies, items)
"UIView" = GUI, HUD, text infromations etc.

Sprite scaling is problematic in many ways - zooming, screen resolution, DPI etc.
- if you have retro style game, you can set setSmooth(0) for textures and it's really simple and effective solution
- If you have detailed graphics like e.g. Angry Birds, game graphics must be in higher resolution (PC) or in various resolutions (which is common for mobile phones because there are problems with DPI)
« Last Edit: February 01, 2018, 05:56:08 pm by Paul »

MrOnlineCoder

  • Jr. Member
  • **
  • Posts: 87
    • View Profile
Re: Using multiple views for level and entities
« Reply #2 on: February 01, 2018, 05:55:08 pm »
Thanks for reply! I don't have retro-style game, my graphics are in high/medium resoultion.

The problem that I need my player to be in screen center.

Paul

  • Jr. Member
  • **
  • Posts: 79
    • View Profile
Re: Using multiple views for level and entities
« Reply #3 on: February 01, 2018, 05:58:06 pm »
Sorry, I read it badly :)

You can change player position instead of levelView position and set levelView center to player position. So it will be virtually the same.
« Last Edit: February 01, 2018, 06:07:37 pm by Paul »

MrOnlineCoder

  • Jr. Member
  • **
  • Posts: 87
    • View Profile
Re: Using multiple views for level and entities
« Reply #4 on: February 01, 2018, 06:10:31 pm »
I am doing the same  :)

But issue with drawing entities/enemies/items remains active.

Paul

  • Jr. Member
  • **
  • Posts: 79
    • View Profile
Re: Using multiple views for level and entities
« Reply #5 on: February 01, 2018, 06:27:01 pm »
Simple way:
- 1 system of coorinates for all your world elements - tiles, characters, items and also for player position
- change player.x, player.y when is player moving

All this should be 1 camera (levelView), now just use levelView.setCenter( player.x, player.y );

Then you do not have to worry about the positions of entities or about mixing views somehow.



MrOnlineCoder

  • Jr. Member
  • **
  • Posts: 87
    • View Profile
Re: Using multiple views for level and entities
« Reply #6 on: February 01, 2018, 06:39:59 pm »
Well, generally, I  have the same situation:

  • Everything has 1 coordinate system (player sprite is drawn in the center, generally it's only a cosmetic feature, all game logic is built around real player position)
  • Everything except UI is rendered using levelView

But the main problem is that my level tileset is small and if I do not reduce (scale) levelView size -  my tiles become very small and character becomes very big.

Probably, the only solution is to increase tileset resolution and quality?

Paul

  • Jr. Member
  • **
  • Posts: 79
    • View Profile
Re: Using multiple views for level and entities
« Reply #7 on: February 01, 2018, 07:12:19 pm »
But now it's more functional I think. You can use normal math for game logic (e.g. collisions) instead of some conversion from one view to another.

You should definitely have a basic ratio for graphics, if you want same graphics quality for everything just have objects (sprites, textures) in their real/natural sizes. Then scale your object to needed dimensions but not through View, use setScale for sprites. For example if you need small rock and big rock.

Mixing different ratios and values does not work. You can't have tile with 16x16 pixels and player with 200 x 200 pixels (in world math). Tiles must be in right ratio mathematically. You can map 16 x 16 texture on them or use texture with better resolution, it does not matter anymore.

Shortly:
Dimensions of objects in the game world must have right proportion. Player vs house, vs wall, vs dog. And graphics (textures, sprites) can be eventually scaled to these dimensions.
« Last Edit: February 01, 2018, 07:43:48 pm by Paul »

Hapax

  • Hero Member
  • *****
  • Posts: 3346
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Using multiple views for level and entities
« Reply #8 on: February 03, 2018, 05:04:08 pm »
You can automatically convert co-ordinates amongst different views by mapping from one view to pixel and then mapping from pixel to the other view. Note that this rounds to the nearest pixel.

sf::Window window;
sf::View view1;
sf::View view2;
sf::Vector2f pointInView1;
sf::Vector2f pointInView2FromView1;
sf::Vector2i pointAsPixel;

// ... (set up views and pointInView1)

pointAsPixel = window.mapCoordsToPixel(pointInView1, view1); // convert from view1
pointInView2FromView1 = window.mapPixelToCoords(pointAsPixel, view2); // convert to view2


It might just be easier (and more accurate) to calculate the conversion manually, of course ;)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything