[solved] If a sprite is hidden by another sprite ?

Get help using Construct 2

Post » Tue Feb 17, 2015 5:46 pm

Hi.

I would like to know when my character isn't visible anymore when it is hidden behind a tree or a wall (sometimes more than one if the player is bigger than a single wall). When we actually can't see any pixel of it.

Any idea how to do that ?
Last edited by Coin-coin le Canapin on Fri Mar 20, 2015 10:57 pm, edited 1 time in total.
B
12
S
7
G
7
Posts: 450
Reputation: 4,276

Post » Tue Feb 17, 2015 7:10 pm

One idea is to use a canvas. 1st paste the character and then paste with "destination-out" anything in front of the player. Then loop over all the pixels of the canvas and see if any pixels aren't transparent. The main issue with that is it's very slow.

If you can treat the player as a square it can be done much quicker.

For instance to check if a player's bounding box is inside another box you can do this:
box.bbleft<=sprite.bbleft
box.bbright>=sprite.bbright
box.bbtop<=sprite.bbtop
box.bbbottom>=sprite.bbbottom

It would become much more complicated if you wanted to know if multiple boxes covered the sprite completely. It would probably require splitting the bboxes up.

Another idea would be to loop over the positions of the sprite and 1st check if the point overlaps the sprite then see of it's overlapping the object's above the sprite. It could be slow too but you also could do a more coarse check by not checking every point.
B
91
S
31
G
99
Posts: 5,197
Reputation: 65,680

Post » Tue Feb 17, 2015 8:08 pm

Thank you for your reply :)
Actually my character will be overlapped by some little squares and it takes about 10 squares to cover it entirely.

I like your canvas suggestion. My character (and its squares) won't be that big so it could fit. I could also iterate every 10 pixels, I think it would work too because of the square shapes.
B
12
S
7
G
7
Posts: 450
Reputation: 4,276

Post » Fri Mar 20, 2015 12:29 pm

Hi !
I've set up this. It works very well with sprites.

However, there is an error message when I try to paste a tilemap into the canvas.

Javascript error!
Uncaught TypeError: Cannot read property '0' of undefined
http://localhost:50000/Tilemap_plugin.js , line 204 (col 20)

Any idea how to fix that ?

edit : okay I've got this : plugin-canvas_p845147?#p845147

So, is there any way to know how much a tilemap is overlapping a sprite with webGL on ?
Last edited by Coin-coin le Canapin on Fri Mar 20, 2015 10:56 pm, edited 1 time in total.
B
12
S
7
G
7
Posts: 450
Reputation: 4,276

Post » Fri Mar 20, 2015 7:33 pm

A solution that's kind of hacky is to use a paster object as well.
1. Paste the tilemap to the Paster object
2. Load the Paster.imageUrl to a sprite
3. paste that sprite to the canvas.
B
91
S
31
G
99
Posts: 5,197
Reputation: 65,680

Post » Fri Mar 20, 2015 9:43 pm

I'm going to try that, thank you so much :)
B
12
S
7
G
7
Posts: 450
Reputation: 4,276

Post » Fri Mar 20, 2015 10:39 pm

Last question !

It seems that I can't paste my freshly loaded (from paster.imageUrl) sprite into a canvas object on the same tick.
For example, it works if I add
system -> wait 0.1s
between
sprite -> load from paster.imageUrl
and
canvas -> paste sprite

If I don't add a system wait, it won't paste the sprite until I trigger it later.

How to avoid this ?

edit : it seems that I should use "sprite -> on url loaded" but I can't find it. Weird.

edit2 : nevermind, found it. :roll:

I'll leave this here, in case someone needs to do the same thing one day : https://www.dropbox.com/s/dgjryr0qujw2x ... .capx?dl=1 (needs the canvas and paster plugins)
(drag'n drop the character and it will show how many pixels from the sprites and the tilemaps are overlapping it)
B
12
S
7
G
7
Posts: 450
Reputation: 4,276


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 7 guests