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

Author Topic: Problem with efficiency and collision when drawing map  (Read 3215 times)

0 Members and 5 Guests are viewing this topic.

Skullek

  • Newbie
  • *
  • Posts: 5
    • View Profile
Problem with efficiency and collision when drawing map
« on: June 04, 2016, 11:57:25 am »
Hello, First of all sorry for my english.


So I've got gaming project to do for my university. I'm studying Computer Science and i chose SFML library.
Here's my topic. I have to write something like Worms (Simple ofc, it don't have to be AAA game ^^) but in real time with simple map editor. It's hot-seat game, map has got size of screen resolution, each of players has 30 seconds to draw his half of map and then they're shooting to each other.  Map is destructible.
I've got some problems. I've got 2 versions of drawing map but both sucks.

First version and problem: efficiency

void Level::Render(sf::RenderWindow &gamewindow)
{
        for (int i = 0; i < mapelements.size(); i++)
        {
                mapElement a = mapelements[i];
                gamewindow.draw(a.circle);
        }
        p1.Render(gamewindow);
}

void Level::handleInput(sf::RenderWindow& gamewindow) {

        if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
        {
                mapElement circle;
                circle.setPosition(mouse.getPosition(gamewindow).x, mouse.getPosition(gamewindow).y);
                mapelements.push_back(circle);
        }
...
 
mapElement is a circle with some methods. This version really sucks in efficiency. If there are 500+ circles there's huge fps drop. If collision is off i can draw about 700+ circles

Second version
I've got transparent RenderTexture (Screen Resolution size, e.g 1366x768) on which i draw circles and then i remove circles from vector so there's no efficiency problems but i can't handle with collision cause it's one big texture...



I don't have other ideas, i'm new in SFML but i tried. Maybe you can help me with ideas of mine or you've got other ideas.

Thanks ;)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Problem with efficiency and collision when drawing map
« Reply #1 on: June 04, 2016, 12:01:37 pm »
Quote
mapElement a = mapelements[i];
gamewindow.draw(a.circle);
You're doing un unecessary copy/destruction of each map element every time you draw them. And you're probably doing it also in other parts of your code (collision detection, ...). Remove all these copies and see if things get better.

Quote
I've got transparent RenderTexture (Screen Resolution size, e.g 1366x768) on which i draw circles and then i remove circles from vector so there's no efficiency problems but i can't handle with collision cause it's one big texture...
You can reconstruct collision information from transparent parts of the texture. Or keep a separate representation of your map for collision detection (ie. every time you draw or erase something to/from the render-texture, add/remove a corresponding entry in the physical description of your map); should be easy if it's just a set of circles.
« Last Edit: June 04, 2016, 12:04:14 pm by Laurent »
Laurent Gomila - SFML developer

Skullek

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Problem with efficiency and collision when drawing map
« Reply #2 on: June 04, 2016, 12:16:06 pm »
And which solution is better on your opinion ? Because i think second is way better.

Basically i have to draw this map with my mouse it doesn't matter which shape will it be i read about VertexArrays but i don't know if it's good idea. I have to destroy map with bullets so i choose circles

I've got only 4 days so Good Luck to me :D

I change this part of code
gamewindow.draw(a.circle);
and i can now draw about 1000 circles so it's better ;) but it's still not enough.


I can send all of Source Code, or even project in a VS.
« Last Edit: June 04, 2016, 12:17:41 pm by Skullek »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Problem with efficiency and collision when drawing map
« Reply #3 on: June 04, 2016, 03:46:02 pm »
If you're drawing over a thousand circles and need more, you may want to consider using a vertex array. This can reduce the number of draw calls (from one per circle shape to one per vertex array i.e. one for all circles).

It's relatively simple to batch up sprites and rectangles into a single vertex array but for multiple circles, you'd need the vertex array to be able to create circular shapes inside itself. It's not a massive undertaking but it's more complex than just quads.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Skullek

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Problem with efficiency and collision when drawing map
« Reply #4 on: June 04, 2016, 04:16:52 pm »
You can reconstruct collision information from transparent parts of the texture.

How can I do it? It's quite interesting, how can i check if my player sprite intersects with transparent part of texture?

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Problem with efficiency and collision when drawing map
« Reply #5 on: June 04, 2016, 05:07:52 pm »
Compare the pixels.

Skullek

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Problem with efficiency and collision when drawing map
« Reply #6 on: June 04, 2016, 06:11:25 pm »
Sorry for stupid questions, can you give me some examples, some links cause i'm really new and idk 80% of things. I'm trying to do sth with RenderTexture, Alpha and even VertexArrays alone but my implementation is awful.