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

Author Topic: Drawing everything? Checking collision with all solid tiles?  (Read 4414 times)

0 Members and 1 Guest are viewing this topic.

santiaboy

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Drawing everything? Checking collision with all solid tiles?
« on: February 27, 2013, 11:48:49 pm »
Hi, i've been developing a simple 2d platformer in c++, using SFML, and have some general questions

Do I draw everything or just the things on screen? Drawing everything is easier as it requires less work for me, but it seems that it is a badpractice as it utilizes more resources than what it is actually needed. ( I have a "tiles" list and I go throught it with an iterator + for loop)

Also, Do I check collision with solid tiles? Same as above. It's easier but consumes more resources.

What I mean is this: does checking if something will be on screen or not(so I draw it or not), or if something is near my character or not (collision) waste far less resources than just doing it for everything?

If you want me to clarify some things tell me, and I'll do my best.

The Terminator

  • Full Member
  • ***
  • Posts: 224
  • Windows and Mac C++ Developer
    • View Profile
Re: Drawing everything? Checking collision with all solid tiles?
« Reply #1 on: February 27, 2013, 11:57:38 pm »
I'm sorry, but I don't really understand your question. What do you mean "drawing everything"?
Current Projects:
Technoport

santiaboy

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Re: Drawing everything? Checking collision with all solid tiles?
« Reply #2 on: February 28, 2013, 12:00:42 am »
pseudo-code

for(listOfTiles){
window.draw(tile);
}

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
AW: Drawing everything? Checking collision with all solid tiles?
« Reply #3 on: February 28, 2013, 12:45:43 am »
Usually you go with the most straight forward approach and start optimizing when you hit performance issues.

For static (or also dynamic) tiles (map) I suggest to use sf::VertexArray.

If you then want to only draw the relevant parts you could use multiple vertex arrays and do a simple 'intersection check' and only draw the tile batch that 'intersects' with the screen.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

mateandmetal

  • Full Member
  • ***
  • Posts: 171
  • The bird is the word
    • View Profile
    • my blog
Re: AW: Drawing everything? Checking collision with all solid tiles?
« Reply #4 on: February 28, 2013, 12:53:55 am »
For static (or also dynamic) tiles (map) I suggest to use sf::VertexArray.

Exactly, use VA's for the tilemap to get the best performance
For the collision detection, one of the recommended data structures to use is a QuadTree.
- Mate (beverage) addict
- Heavy metal addict _lml
- SFML 2 addict
- My first (and free) game: BichingISH!

krzat

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: Drawing everything? Checking collision with all solid tiles?
« Reply #5 on: February 28, 2013, 11:55:49 am »
From my map renderer: source

///take world position and return tile indexes
sf::Vector2i MapRenderer::getTile(sf::Vector2f pos) const
{
        int x = static_cast<int>(pos.x / tileSize);
        int y = static_cast<int>(pos.y / tileSize);
        if(pos.x < 0) x--;
        if(pos.y < 0) y--;
        return sf::Vector2i(x,y);
}


//how to draw only visible tiles
//may not be valid C++ code
auto view = window.getView();
auto b = view.getCenter() - view.getSize() / 2; // left top corner
auto e = view.getCenter() + view.getSize() / 2; // right bottom corner

for(int x=b.X; x <= e.X; x++)
        for(int y=b.Y; y <= e.Y; y++)
        {
                //draw [x y] tile
        }
 

For collisions, you can replace view with AABB.
SFML.Utils - useful extensions for SFML.Net

Ricky

  • Jr. Member
  • **
  • Posts: 99
    • View Profile
    • Tejada
    • Email
Re: Drawing everything? Checking collision with all solid tiles?
« Reply #6 on: March 01, 2013, 06:35:55 pm »
What I did was made a template function and a vector of my drawable objects.
The function will take any type vector and try to draw it.
Wilt thou yet say before him that slayeth thee, I am God? but thou shalt be a man, and no God, in the hand of him that slayeth thee.

santiaboy

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Re: Drawing everything? Checking collision with all solid tiles?
« Reply #7 on: March 02, 2013, 09:47:17 pm »
Thanks for the replies!

The game is capped at 60 FPS, and it seems to work fine. However, when in fullscreen, the tiles look jagged for a split second. I tried with a small map and it keeps happening, so I guess it wasn't an optimization problem after all.
Strangely it only happens when (in fullscreen) the screen moves (I have a sf::View camera), and if I stay still the game runs smoothly.

EDIT:
Drivers are updated, so that's not an issue

I haven't used Vertex Arrays as I'm having some trouble understanding them haha
« Last Edit: March 02, 2013, 10:06:02 pm by santiaboy »

 

anything