# Question about pick [sprite] vs tilemap

Get help using Construct 2

### » Sat Feb 21, 2015 8:04 pm

Its not the only way, theres like dozens.
You need to say what you want to do, not postulate how you think you should go about it.
We have no idea why you need to pick the object, so we can't give you a better way to go about it.
Associating a object by its position is a bad way to go unless you are dealing in absolute positions. As in not using sub-pixel coordinates, or decimals.

You have to say something like "how do I pick this object when its close to this other object?", or something like that.
B
178
S
50
G
205
Posts: 8,684
Reputation: 127,211

### » Sat Feb 21, 2015 9:29 pm

My question was more in a general way about how to efficiently pick some objects among a lot of instances and the array thing seems to suit me well.

That said, this is what I want to achieve :

I have a level made by from lot of violet squares as sprites.

When we click somewhere, it destroys some blocks in a given radius (a slope is just a different frame from the same sprite) :
hole1.png

I want my hole to have slopes on each "corners" :
hole2.png

And this for any given radius or shape. A bigger hole would make this (I did it manually) :
bighole.png

Plus, when a player creates a hole, the hole regenerates hitself somewhat square by square :
You can try it here : http://canapin.com/construct/tolilo/06/ (arrow keys, left click)
For each square regenerated, it needs to scan surrounding blocks to know which frame must be set at the current square (frame 0 : full square, frame 1 : top left slope, frame 2 : top right slope etc…).
You do not have the required permissions to view the files attached to this post.
B
12
S
7
G
7
Posts: 450
Reputation: 4,276

### » Sat Feb 21, 2015 9:45 pm

Yeah, theres a bunch of ways to do that.
If it were with the tilemap object checking xy's would have been ok.
Here your quickest dirtiest method would to use a dummy object, and check if the invisible dummy object was overlapping the tiles.
B
178
S
50
G
205
Posts: 8,684
Reputation: 127,211

### » Mon Feb 23, 2015 12:37 pm

I managed to achieve what I wanted : http://canapin.com/construct/tolilo/07/
I noticed the game slows down when there are many projectiles on the screen.

My guess is that the event "projectile on collision with block" (where block is a sprite) iterates over all the block sprites, which are still about 4000. So, if there are 10 projectiles on screen, it iterates over 10*4000 tiles on a tick. Is that right ?

So I guess I'll going back to tiles and find a way to manage stuff like timer regeneration for reach tile.
B
12
S
7
G
7
Posts: 450
Reputation: 4,276

### » Mon Feb 23, 2015 3:35 pm

Coin-coin le Canapin wrote:I managed to achieve what I wanted : http://canapin.com/construct/tolilo/07/
I noticed the game slows down when there are many projectiles on the screen.

My guess is that the event "projectile on collision with block" (where block is a sprite) iterates over all the block sprites, which are still about 4000. So, if there are 10 projectiles on screen, it iterates over 10*4000 tiles on a tick. Is that right ?

So I guess I'll going back to tiles and find a way to manage stuff like timer regeneration for reach tile.

You will eventually kill your program when using so many objects, because the moment you have to check vs any of them, it will add a lot of tests, which will rapidly build up.

Also the way you do collision check is of great important when it comes to performance, which you wouldn't think. I did some testing of this some time ago and here is two examples. They are exactly the same, only the way collision is handled is different.

The first one uses "is overlapping" including some optimization that I added. The second one uses "on collision".

If you try them don't press the "1. Spawn objects" more than 2-3 times, the program will kill your browser, as its was created to test performance when doing collisions.
After you have done that just press "2. Remove overlaps" and then "3. Increase speed by 25" to increase the speed of the objects. (The program was made as a test so, don't spawn objects while its running, there are no error handling in the program so it might crash or bug )

Personally I test it in chrome, since my internet explorer is causing problems all the time, reducing performance in general.

"Is overlapping with optimization"
https://dl.dropboxusercontent.com/u/109921357/Performance%20Test%201/index.html

"On collision"
https://dl.dropboxusercontent.com/u/109921357/Performance%20Test%202/index.html

So as you can see there is a quite huge difference between how you implement collision in C2, a general rule is to never use "On collision" it will always give poor performance, compared to "Is overlapping" and with optimization you can improve the "Is overlapping" which is impossible with "On collision".
B
45
S
12
G
3
Posts: 1,210
Reputation: 7,549

### » Mon Feb 23, 2015 6:11 pm

That's quite interesting. Can you attach the two capx ?
About my project, I'm now trying to stick to tilemaps (and arrays to store additionnal informations for each tile) rather than spawning some blocks. I'm not sure yet which method I'll use.

If I use some sprites only for destroyed blocks (just to set a timer behavior and maybe one or two variables), I'll have I think 100; 200 or 300 sprites at the same time instead of 4000 so it could be fast enough.
Storing my data (including my timers for each destroyed tile) into an array would, I think, give better performances, thought it's more complicated to handle with events.

I'll try to do some tests with both methods.
B
12
S
7
G
7
Posts: 450
Reputation: 4,276

### » Mon Feb 23, 2015 6:46 pm

Coin-coin le Canapin wrote:That's quite interesting. Can you attach the two capx ?

Its just a single CAPX, you just have to enable/disable different parts. I added commentary to it, so you can see which one controls what. Not that its that hard to figure out. But just so you don't think both should be enable at the same time.

Performance test.capx

Another solution that might work if you need sprite objects, is to create them when something interact with them, and that way you replace the tiles with objects. That way you can probably reduce the amount of objects needed at one given time.
You do not have the required permissions to view the files attached to this post.
B
45
S
12
G
3
Posts: 1,210
Reputation: 7,549

### » Mon Feb 23, 2015 8:34 pm

That's cool.
Do you know why the overlap events is faster than the collision one ?

Aside from this, I made three tests about my stuff with tiles (6480).
When the left mouse button is down, it constantly destroys the tiles in a 6 tiles radius and set a timer for each tile, which value depending how far the tile is from the mouse coordinates.

1) Only with a small array : I fill an array when clicking and set my timers in it. When a tile respawns, its reference is removed from the array. Worst performances.
2) With sprites spawned only when a tile is destroyed. Sprites contains each destroyed tiles timer and they are picked up regarding their XY coordinates (the same as the current tile). Bad performances, not as much as the array solution.
3) With sprites as the previous ones but with a big array, as the sprites UID are stored in it and so they are picked up by UID. Excellent performances.
You do not have the required permissions to view the files attached to this post.
B
12
S
7
G
7
Posts: 450
Reputation: 4,276

### » Tue Feb 24, 2015 1:47 am

Coin-coin le Canapin wrote:That's cool.
Do you know why the overlap events is faster than the collision one ?

To be honest, I have no clue according to the manual, even though its not an in-depth explanation they should do the exact same thing.

But did a test of "On collision", "Overlapping" and "Overlapping with optimization" all screenshots are taken after its started so it changes a little bit up and down.

As you can see, the "on collision" and "Overlapping" is practically the same in the one named "Low object count", however the CPU util and FPS is highly different. Strangely enough if you look at the Collision checks /sec, its only 43513 for the "On collision" while its 80812 for the "Overlapping" even though the approx. collision checks per tick is higher for "On collision" than overlapping. Regarding the optimized one, there is not a lot to add, think the numbers speaks for it self, and it have no problems with such a low number of objects.

On the "High object count" however things goes really from for "On collision" the performance was so bad, that it couldn't even register the amount of collision checks. However the CPU again goes to max and the FPS drops massively. Where as the "Is overlapping" gives a lot better performance, however still uses a lot of CPU and also take a quite big hit to the FPS. And with the "optimized" version it is barely noticeable, except it also uses a lot of CPU, however it doesn't hurt performance FPS wise.

So what exactly is going on with the "On collision" I don't know, because I would assume that "Is overlapping" is doing the same amount of checks as it does, at least according to the manual. And the optimized version as you know, is based on that it only cares about which object it can collide with that are also the closest, so it logically reduces the amount of checks needed to be made each ticks by a lot.
But would probably need Ashley to explain it, because I don't think its possible to find the answer in the manual at least. So its just something to be aware of I think, if performance drops, you should make sure to at least not use "On collision"
B
45
S
12
G
3
Posts: 1,210
Reputation: 7,549

Previous