Collision detection with clones (ID recognition)

Update: resolved! (for now...we'll see about individual velocities...)
I made a little demo of the collision detection:
Snap! Build Your Own Blocks


I am attempting to make a little game, and a feature of that game is that there are circular balls that are constantly moving and colliding with each other. I want to make it so that instead of having individual sprites for each ball, there is just one sprite that clones itself.

When the balls collide, they need to recognize which clone they are colliding with, and then get the data (x, y, radius, x&y velocities) of the specific ball they're colliding with. So far what I've figured out is I need to give each clone an ID when it's made, but I can't figure out how to make each one get the ID of the specific clone it's in contact with. I've tried messing around with a global data list, and using the map function to ask every sprite for it's ID, but I'm not sure where to go from there.

Does anybody know how to accomplish this in Snap, or if there is a better way to go about doing this?

Here is a test project with a very basic framework (the balls don't move, but are draggable):

Also, for reference, I'm following this guide (hence why the balls are only draggable for now):

Hi @viberfoner and wellcome to this forum :slight_smile:

I can't see your project (it says "This project does not exist"), but I want to comment some "clones possibilities":

  • You have the primitive block myclones And so, you can reference all your clones. That list is have all your clones and its order it's the creation order.

  • And if you want your "own control", as Sprites (and clones) are first class, you can store its references as you want, creating clones with the reporter block:
    oneClone
    or
    myClonesList
    and then, you can control those references as you want.

  • In all these cases, you can control a clone easily:
    tellClones

Joan

Thanks for the response @jguille2! Oh whoops, forgot to make it shareable. Now it should be visible.

So will that work even when the clones run the script? For example, if clone #2 contacts the original sprite, will the original sprite fall under "my clones"?

Also, when I use
1%20script%20pic
under "when I start as a clone," the id (local variable) doesn't get set to anything.

I am messing around with the ideas you sent, but I'm still not sure how I'll be able to get them to detect the specific clones they are touching.

Here's an updated project: https://snap.berkeley.edu/project?user=viberfoner&project=object%20oriented%20circle%20collision%20detection%201.1

I tried implementing the ideas you sent, and it helped to clean up the code. But now I'm having a new issue.

1%20script%20pic2

When the original sprite touches the other sprites, it only returns the "overlap" of the first clone (as shown by the "data" list), no matter which sprite it is actually touching. Note: the overlap should be a negative number when it is touching a sprite. The overlap will be used to make sure sprites don't get stuck to each other.

Keep in mind, every clone should also interact with each other, not just the parent. I'm just trying to go one step at a time, so I'm just focusing on the parent script right now.

Update: I think the issue was that I was nesting custom blocks inside the definitions of other custom blocks. I got it to work! I've made it so every clone can push the parent away, and vice versa. Now I just need to figure out how to get every clone push each other.

This is my attempt at getting the target ball to move in reaction to the ball executing the script. The problem is that the "object: myself" block isn't actually referring back to the clone that is carrying out the script. How can I make this work?
1b%20script%20pic

Updated project file:

Final update for today (probably):

I realized it wasn't really necessary to have the sprites tell each other how to move, since they each have their own script that makes them move accordingly! I made a little demo for it. Next step is to get the balls to move around with individual velocities! Now that the hard part is out of the way, I think I can figure it out.

Demo: https://snap.berkeley.edu/project?user=viberfoner&project=Ball%20Physics%201

I just realized that the original question still remains, because I still haven't figured out how to ask another sprite for a local variable (not x, y, size, etc, but a local variable made by the user, such as velocity).

1%20dimensional%20elastic%20collision%20script%20pic

Whenever I try this, the simulation gets stuck. Is there another way to do this, or just a workaround? I've messed around with a global data list but keep running into dead ends.

Hi @viberfoner,

Many questions over your past posts... but I see you "final" project and it's all fine. Only some "final" comments:

  • Yes, here "my other sprites" are fine because everybody (the original sprite and all its clones) are playing. Maybe "my neightbors" are better, because you only have to check the closer ones. Then, you don't need to check all "numBalls", only the "lenght of (my neigbors)".
  • In other projects, maybe you have more sprites, and you can't use "my other sprites". You can use "my other clones" and keep the "original sprite" out of the play.
  • Anyway, you can create your own list of sprites, and take more control about this (the list items and their order inside the list).
    creatingBalls

Joan

Thanks so much for your replies Joan, and sorry for asking so many questions haha.

That's great to know, but I'm still not sure how I would do something like what I said in my last post:

1%20dimensional%20elastic%20collision%20script%20pic

Hi @viberfoner ,

I wrote my last post before reading your last one.

About local vars, you can use the block "_ of _"
velOfSprite

Note: first, you must choose a "normal sprite" to get the properties (and local vars) of it. After this, you can drop your "item i of my other sprites".

Joan

Wow, I can't believe I missed that. I'm excited to get to work on the next step of the project! Thanks again!

Nick