Discuss Scratch

4lex4ndre
Scratcher
62 posts

Sprite touch detection

Hi there.

I'm working on a project and I need some help.

To make it easier to understand my problem, I made a “sightly” simplified version <– please check

My problem is that I want for sprite clones to detect touching themself but :
- There is a background, and sprites can
go to front layer
or
go to back layer
- the sprites must “touch” each other BEHIND the red square
- the sprites must “touch” each other IN FRONT OF the red square
- the sprites must NOT “touch” each other THROUGH the red square

Notes :
- My sprite clones are always black
- They are numerous (250)
- I need a simple way to detect that the clones are touching each other (the project is already tricky)

Actually failled methods (watch the test project to see why) :
<touching color [#000000] ?>
<touching [sprite v] ?>

To make it easier to understand my problem, I made a “sightly” simplified version <– please check

Thanks for your help
Cool_Dude2022
Scratcher
500+ posts

Sprite touch detection

4lex4ndre wrote:

Hi there.

I'm working on a project and I need some help.

To make it easier to understand my problem, I made a “sightly” simplified version <– please check

My problem is that I want for sprite clones to detect touching themself but :
- There is a background, and sprites can
go to front layer
or
go to back layer
- the sprites must “touch” each other BEHIND the red square
- the sprites must “touch” each other IN FRONT OF the red square
- the sprites must NOT “touch” each other THROUGH the red square

Notes :
- My sprite clones are always black
- They are numerous (250)
- I need a simple way to detect that the clones are touching each other (the project is already tricky)

Actually failled methods (watch the test project to see why) :
<touching color [#000000] ?>
<touching [sprite v] ?>

To make it easier to understand my problem, I made a “sightly” simplified version <– please check

Thanks for your help

There are a couple ways that you can detect whether a clone is touching another clone. First, you can use different costumes for each clone. Second, you could add each clone to a list with constantly updating X and Y coordinates for each one. You can then detect which clone number one is by looking up its coordinates in the list.
Vaibhs11
Scratcher
1000+ posts

Sprite touch detection

lf you're not using cIones, you can try setting up a ‘z-index’ variabIe for each object, and if the z-index of both sprites match then the if statement turns true. Yes, l did see that you do use cIones in your originaI project.

AIso, in the test project, aII three tests work for me.

Last edited by Vaibhs11 (Feb. 8, 2024 12:49:02)

4lex4ndre
Scratcher
62 posts

Sprite touch detection

First, you can use different costumes for each clone
I'm interesting with this solution but how ? can you mix https://scratch.mit.edu/projects/963084408/editor/ or show example ?

Second, you could add each clone to a list with constantly updating X and Y coordinates for each one.
Although it seems to work, it seems very complicated to me with more than 250 clones…

lf you're not using cIones, you can try setting up a ‘z-index’ variabIe for each object, and if the z-index of both sprites match then the if statement turns true.

It is clones, i can't do it in another way…

Last edited by 4lex4ndre (Feb. 8, 2024 12:48:12)

Cool_Dude2022
Scratcher
500+ posts

Sprite touch detection

4lex4ndre wrote:

It is clones, i can't do it in another way…

He might have been referring to you using pen.

4lex4ndre wrote:

Cool_Dude2022 wrote:

Second, you could add each clone to a list with constantly updating X and Y coordinates for each one.
Although it seems to work, it seems very complicated to me with more than 250 clones…

Lists have a limit of 200,000 (according to the scratch wiki.) If I understand correctly, then this will be a pretty simple solution to your problem.

4lex4ndre wrote:

Cool_Dude2022 wrote:

First, you can use different costumes for each clone
I'm interesting with this solution but how ? can you mix https://scratch.mit.edu/projects/963084408/editor/ or show example ?

I misspoke, using many different costumes is so that you can use a true/false (boolean) to check if the costume matches your specific check. This will not actually work for your game and should just try to use the lists.

Last edited by Cool_Dude2022 (Feb. 8, 2024 13:00:02)

4lex4ndre
Scratcher
62 posts

Sprite touch detection

AIso, in the test project, aII three tests work for me.

The 3rd test must be not “touching”
Cool_Dude2022
Scratcher
500+ posts

Sprite touch detection

4lex4ndre wrote:

AIso, in the test project, aII three tests work for me.

The 3rd test must be not “touching”

I'm so dumb

Just use this:

wait until <<touching [entity v] ?> or <touching color [#000000] ?>>

I've mixed it here: https://scratch.mit.edu/projects/963267430/editor

Last edited by Cool_Dude2022 (Feb. 8, 2024 13:19:25)

4lex4ndre
Scratcher
62 posts

Sprite touch detection

Cool_Dude2022 wrote:

Just use this:

wait until <<touching [entity v] ?> or <touching color [#000000] ?>>

I've mixed it here: https://scratch.mit.edu/projects/963267430/editor

Sorry but the 3rd test is'nt working. They must NOT touch each other through the red square
4lex4ndre
Scratcher
62 posts

Sprite touch detection

I just saw a new behavior, it's different depending on who (clone or sprite) is in the front or back

So there is 4 TESTS to do

back/back
top/top
back/top
top/back

Last edited by 4lex4ndre (Feb. 8, 2024 13:38:07)

Cool_Dude2022
Scratcher
500+ posts

Sprite touch detection

4lex4ndre wrote:

I just saw a new behavior, it's different depending on who (clone or sprite) is in the front or back

So there is 4 TESTS to do

back/back
top/top
back/top
top/back

I also noticed that, how would you like them to appear? Like this?

back/back = true
top/top = true
back/top = false
top back = false

4lex4ndre wrote:

Cool_Dude2022 wrote:

Just use this:

wait until <<touching [entity v] ?> or <touching color [#000000] ?>>

I've mixed it here: https://scratch.mit.edu/projects/963267430/editor

Sorry but the 3rd test is'nt working. They must NOT touch each other through the red square
I misheard you, I'm working on finding a new solution
4lex4ndre
Scratcher
62 posts

Sprite touch detection

I also noticed that, how would you like them to appear? Like this?

back/back = true
top/top = true
back/top = false
top back = false

Exactly

I misheard you, I'm working on finding a new solution

Thank you
4lex4ndre
Scratcher
62 posts

Sprite touch detection

In the final project, the “original” sprite is the queen, and there is some outside and inside workers clones

- Queen must detect when touching a INSIDE worker
- OUTSIDE workers must detect when touching each other and share food coord

(they already have a “Job” var to know if they are outside or inside)


Last edited by 4lex4ndre (Feb. 8, 2024 13:53:59)

Cool_Dude2022
Scratcher
500+ posts

Sprite touch detection

4lex4ndre wrote:

In the final project, the “original” sprite is the queen, and there is some outside and inside workers clones

- Queen must detect when touching a INSIDE worker
- OUTSIDE workers must detect when touching each other and share food coord

(they already have a “Job” var to know if they are outside or inside)


4lex4ndre wrote:

In the final project, the “original” sprite is the queen, and there is some outside and inside workers clones

- Queen must detect when touching a INSIDE worker
- OUTSIDE workers must detect when touching each other and share food coord

(they already have a “Job” var to know if they are outside or inside)



Can you change the color of the inside worker ants by 1?

Then, you can use something like this:

if <<(costume [name v]) = [queen]> and <touching color [#000001] ?>> then

end

Since the outside workers are going to be a different color, then you can use similar code to detect whether the outside workers are touching.

The collision tests that you are conducting are near impossible with what you're trying to use. The <touching color> and <touching (entity)?> can be true and false at the same time. We need another boolean true/false detector to make this work.

<touching (entity)?>:
back/back = true (passed)
front/front = true (passed)
back/front = true (failed)
front/back = true (failed)

<touching color (black)?]>:
back/back = false (failed)
front/front = true (passed)
back/front = true (failed)
front/back = false (passed)

We need a boolean that passes the back/front and then we should be good. We might run into an issue with the ones that overlap (front/front & back/front)

Last edited by Cool_Dude2022 (Feb. 8, 2024 15:25:30)

4lex4ndre
Scratcher
62 posts

Sprite touch detection

The actual “best” test :
if <<<is front?> and <touching color [#000000] ?>> or <<not <is front?>> and <touching [ clone v] ?>>> then

end

back/back = true (passed)
front/front = true (passed)
back/front = true (failed)
front/back = false (passed)

3/4…

Here are some thoughts on how to achieve this (all considered invalid but perhaps with improvement?):
- Make “front” and “back” clone as 2 separate sprite : But i will lost a lot of shared functions/var, and they often switch from front to back
- In case of contact, declare which collides with 2 different lists : But posing an overwriting problem on these lists
- In case of contact, hide those of the other layer for the time of a new test : but causing a very annoying flashing
Cool_Dude2022
Scratcher
500+ posts

Sprite touch detection

4lex4ndre wrote:

The actual “best” test :
if <<<is front?> and <touching color [#000000] ?>> or <<not <is front?>> and <touching [ clone v] ?>>> then

end

back/back = true (passed)
front/front = true (passed)
back/front = true (failed)
front/back = false (passed)

3/4…

Here are some thoughts on how to achieve this (all considered invalid but perhaps with improvement?):
- Make “front” and “back” clone as 2 separate sprite : But i will lost a lot of shared functions/var, and they often switch from front to back
- In case of contact, declare which collides with 2 different lists : But posing an overwriting problem on these lists
- In case of contact, hide those of the other layer for the time of a new test : but causing a very annoying flashing

1. It's hard to use variables with many clones, especially since you need to check it every so often. You're better off just switching to a list, but even if you do, this still won't work.
2. I might open a new post as this one has too many chats and is a little cluttered for other people to read. So, I'm thinking of simplifying this entire chat and creating a new forum post for you.

4lex4ndre
Scratcher
62 posts

Sprite touch detection

My actual thoughts are to use 2 differents sprite and use
<touching [clone v] ?>

It seems to be the proper way to do it without complexify to much the code
Cool_Dude2022
Scratcher
500+ posts

Sprite touch detection

4lex4ndre wrote:

My actual thoughts are to use 2 differents sprite and use
<touching [clone v] ?>

It seems to be the proper way to do it without complexify to much the code
Alright, you can try that.
4lex4ndre
Scratcher
62 posts

Sprite touch detection

OMG soooo much work
Cool_Dude2022
Scratcher
500+ posts

Sprite touch detection

4lex4ndre wrote:

OMG soooo much work
Alright, well I've made a new topic: https://scratch.mit.edu/discuss/topic/740094/ and I'm waiting for a response.

Powered by DjangoBB