Discuss Scratch

VisioTech
Scratcher
10 posts

Collision code for two moving sprites.

I have 2 sprites that I want to not collide with each other. One (and its clones) is set on a randomized path, the other moves with the mouse. How can I code this so they do not collide? I am okay with rethinking how the project works too.

Here is the project:
https://scratch.mit.edu/projects/413660544/

Thanks for any help!

Last edited by VisioTech (July 25, 2020 14:48:36)

Vibrato
Scratcher
1000+ posts

Collision code for two moving sprites.

What do you mean by collide? Do you mean that you don't want them to touch at all? Or just not to be affected by physics?
VisioTech
Scratcher
10 posts

Collision code for two moving sprites.

Vibrato wrote:

What do you mean by collide? Do you mean that you don't want them to touch at all? Or just not to be affected by physics?
I don't want them to be able to pass through each other. So I guess, I don't want them to touch each other. Thanks for helping me clarify
Vibrato
Scratcher
1000+ posts

Collision code for two moving sprites.

In the firefly sprite, when it moves, add something like this:
when move
if <touching [bottle v] ?> then
repeat until <not <touching [bottle v] ?>>
change x by (pick random (-5) to (5))
change y by (pick random (-5) to (5))
end
end
Cutie_Pooge
Scratcher
500+ posts

Collision code for two moving sprites.

I did create a remix however it's still not perfect. Personally, I would have used hitboxes and some formulas to try to make it more reliable but I went for simplicity.
VisioTech
Scratcher
10 posts

Collision code for two moving sprites.

Cutie_Pooge wrote:

I did create a remix however it's still not perfect. Personally, I would have used hitboxes and some formulas to try to make it more reliable but I went for simplicity.
Thanks for the remix. Good for new ideas, but like you said it doesn't quite do the trick. Tell me about these hitboxes/formulas. I'd love to learn.

Also, thanks for cleaning up the “reverse” costume change. I've been trying to figure that out for a while, but since my workaround “worked” I just kept doing that. This is much better!

Last edited by VisioTech (July 25, 2020 20:16:40)

VisioTech
Scratcher
10 posts

Collision code for two moving sprites.

Vibrato wrote:

In the firefly sprite, when it moves, add something like this:
when move
if <touching [bottle v] ?> then
repeat until <not <touching [bottle v] ?>>
change x by (pick random (-5) to (5))
change y by (pick random (-5) to (5))
end
end


What type of block would the “when move” be? Still very new to this. I try to search these things for myself, but realize I don't even know what words I'm looking for to search correctly.
Vibrato
Scratcher
1000+ posts

Collision code for two moving sprites.

VisioTech wrote:

Vibrato wrote:

In the firefly sprite, when it moves, add something like this:
when move
if <touching [bottle v] ?> then
repeat until <not <touching [bottle v] ?>>
change x by (pick random (-5) to (5))
change y by (pick random (-5) to (5))
end
end


What type of block would the “when move” be? Still very new to this. I try to search these things for myself, but realize I don't even know what words I'm looking for to search correctly.
It would be whatever your movement code is, that's just to tell you to where in the script to put my code.
Cutie_Pooge
Scratcher
500+ posts

Collision code for two moving sprites.

Vibrato wrote:

It would be whatever your movement code is, that's just to tell you to where in the script to put my code.
But that will be much more random when the bottle touches the firefly, they may be pushed out or pushed in so I don't suggest that solution.
I will try to show you how I will do it later, sorry but I have a lot of work to do after an hour and my method needs a lot of time.
VisioTech
Scratcher
10 posts

Collision code for two moving sprites.

Vibrato wrote:

VisioTech wrote:

Vibrato wrote:

In the firefly sprite, when it moves, add something like this:
when move
if <touching [bottle v] ?> then
repeat until <not <touching [bottle v] ?>>
change x by (pick random (-5) to (5))
change y by (pick random (-5) to (5))
end
end


What type of block would the “when move” be? Still very new to this. I try to search these things for myself, but realize I don't even know what words I'm looking for to search correctly.
It would be whatever your movement code is, that's just to tell you to where in the script to put my code.
The movement code is randomized. Will this still work with that? I am just trying to understand the math behind this, sorry if that is a dense question.
VisioTech
Scratcher
10 posts

Collision code for two moving sprites.

Cutie_Pooge wrote:

Vibrato wrote:

It would be whatever your movement code is, that's just to tell you to where in the script to put my code.
But that will be much more random when the bottle touches the firefly, they may be pushed out or pushed in so I don't suggest that solution.
I will try to show you how I will do it later, sorry but I have a lot of work to do after an hour and my method needs a lot of time.


Sure! Take your time. This is just for fun after all and you are helping me. I would never rush anyone helping me out.
Cutie_Pooge
Scratcher
500+ posts

Collision code for two moving sprites.

Turns out you don't need maths to create good looking collision, I updated my remix.
VisioTech
Scratcher
10 posts

Collision code for two moving sprites.

Cutie_Pooge wrote:

Turns out you don't need maths to create good looking collision, I updated my remix.

That, my fine sir, is definitely math. Just not necessarily what you may think of as math. I'd love some help some time helping me understand what you did there though. I am new to variables and “my blocks” so haven't a clue what you did even when looking right at it. You can even point me to a tutorial too if you don't have the time to show me.

Thanks for the help, works splendidly on your remix.

Now my only trouble is making it work in my project. I have put all the code into my project using the backpack, but for some reason it is working differently on my project (fireflies seem to teleport and move funny after being touched with the jar) even though they are basically identical now. What am I missing? I'm feeling rather needy right now, sorry! Please take your time, I'll just fiddle around until you get to it.



Last edited by VisioTech (July 26, 2020 11:32:04)

Cutie_Pooge
Scratcher
500+ posts

Collision code for two moving sprites.

VisioTech wrote:

-snip

Yes, you may want to read my comment again. I was changing the project according to some math but then I had the idea to just make another sprite, turns out it looks very smooth is very simple and understandable.
VisioTech
Scratcher
10 posts

Collision code for two moving sprites.

Cutie_Pooge wrote:

VisioTech wrote:

-snip

Yes, you may want to read my comment again. I was changing the project according to some math but then I had the idea to just make another sprite, turns out it looks very smooth is very simple and understandable.

Do you mean Sprite 3? Or did you redo Sprite 2?

I have pretty much duplicated what you had, but for some reason it isn't working in my project. Do some aspects of variables and “my blocks” get lost when put into a backpack?

Powered by DjangoBB