How to make instances of the same object stick to each other?

Hello!

I’m trying to get multiple instance of the same object to stick to each other when they collide.

Currently I’m using Physics2 and creating a joint when they collide. It works fine with two different objects, but I can’t figure out how to do it when it’s the same object, but different instances.


On the top (disabled) is my failed attempt with the single object. I understand that Gdevelop would struggle to figure out which ‘Atom’ is which. Is there a way to refer to a specific instance?

Below is working pretty fine, but I want it to work without needing a separate object.

Please let me know if you need any more clarification!

It sounds interesting. Can you describe your project a little more? Are they bouncing around and sticking together? Is there some form of player interaction? After they link what happens? Do they stay linked forever? Is it one “clump” or can there be multiple?

The more details the better. An image, short gif or a video link to say YouTube would really help you get a quicker and applicable suggestion. Anything would just be a guess.

Hi Keith,
Thanks for your reply!

I was trying to make a game where you control a little black blob. There are other blobs slowly moving around the screen. When you touch one, it sticks to you, and you are now a 2-blob blob clump. Then when you bump into another blob, you become a 3-blob blob clump. And on and on.

Eventually I was going to add more gameplay where you ‘add’ smaller blob-clumps to your blob-clump, but bigger blob-clumps kill you.

But for now I was just trying to create a blob clump that grows as more blobs stick to it.

They stay linked forever.

Sounds nice. I tested the concept using a mouse joint, it worked pretty good. My concept would work with any joint type.

The blobs had 2 variable. One to tell if it was linked to the player and the other for its jointID. A scene JointID variable is used to hold the joint ID and then set the object Joint ID to it. I gave all of the objects the physics behavior and set the gravity to 0.

Try me, I added a mouse joint on the player just to test it.
https://gd.games/keith_13579/needy-test

The same technique could be used on the blobs. They could use a linked object. When blobs that aren’t stuck to the player collide then they could get a mouse jointed to a link object. If one didn’t exist, it could be added. And once it’s created any blobs that collide could be added to it. You could use another Boolean to track the blob link status, link them to the link object. So, other blobs would know which link object to use. There could be multiple clumps.

Having the clumps merge with either the player or another clump would just require a change to the booleans and change the object linking. I didn’t use the object linking in my example or link blobs but the concept would be similar. There would be more state variables to check.

I did it on my phone. The text might be small. I’ll add the mobile version screenshot as well. It gets formatted differently in mobile view.

mobile view

The joint forces can be increased or you could use a different joint style.

The scene variable Link is used to make sure there is at least 1 object linked to the player and 1 linked to 1 not linked.

I used the blob centerX and centerY for the mouse joint original target for the blob . And the updated it using the player centerX and centerY.

Edit: IDKW but not all of the blobs always linked, I have missed something. The concept still works. It just needs tweaking.

I see what’s wrong, the collision between the player and blob also needs a for each object for the blob object.

I think this works. The joint might have been added but because you can’t use an object variable in the add joint action, I used a scene variable. Without the for each object they probably all got the same ID. I don’t know if you can add joints to multiple objects without the for each object.

Thank you Keith for this in-depth answer!

1 Like