How can I detect a collision between two identical objects?

I have an object called unit1, and if it collides with unit1, both of them need to be deleted, and unit2 needs to be created. But how do I know that unit1 is colliding with unit1, and how do I delete them later?

What I usually do goes like this:

For every unit1 object,

Assign a unique id variable, then create a ā€œcolliderā€ object as well and link them together, and give the collider the same id variable as the unit1 object it’s linked to.

Now you’ll have to detect collision between unit1 objects and collider objects that don’t have the same id variable.

Thank you for the advice, but I’ve already managed to implement this using JavaScript without any additional collider objects. Later, I’ll explain how I did it.

Ah yes, please do. That would be quite useful. The collider method does seem a bit crude, if I do say so myself.

But this method is only suitable for games of this type:
GIF 05.09.2026 22-17-48
I used the cursor position and the UID of the objects, which is not suitable in most cases.(but it’s a bit glitchy)
But I’ll try to make it possible to detect when an object collides with itself.

You can definitely do the above, but this works OOTB:

{F3347E04-E044-4FF2-A4C1-2550987C834A}

I tried it, but it doesn’t work.

I think you could try using Custom Objects, maybe by creating a single mask that works as a sprite proxy. I’m suggesting this because with Custom Objects you could use ā€œOn Destroyā€ to create the unit2.

As for the collision check, I don’t think you should have problems, as long as you add an ID or something similar when creating them, so you can distinguish between the different instances.

disclaimer: i’ve never used any ā€œon destroyedā€ event yet..so i dunno how they behave.

I see… if I understand it correctly, you technically used the cursor as the collider. That’s a great idea. You’re right though, that it can’t be used in most cases.

As for same type object collision, would it be possible perhaps to assign a ā€˜unit type’ variable and reference them along with their uid? It seems kind of redundant considering they’re already the same type object, but just throwing it out there.

Hmm. I literally tested this in a new project and it worked fine. Do you have other events that may be picking other instances too?

No, I don’t have any other events related to this object.

If anyone is interested, here’s the code for my extension.


IDofCursorOnObject:
Arguments:
obj: Any object

var obj = eventsFunctionContext.getObjects("obj");
var ids = [];
var curX = gdjs.evtTools.input.getCursorX(runtimeScene);
var curY = gdjs.evtTools.input.getCursorY(runtimeScene);

var objectName = obj.length > 0 ? obj[0].getName() : "";
var allObj = objectName ? runtimeScene.getObjects(objectName) : obj;

function curOnObj(gdobj, cx, cy) {
    var L = gdobj.getX();
    var R = gdobj.getX() + gdobj.getWidth();
    var T = gdobj.getY();
    var B = gdobj.getY() + gdobj.getHeight();
    return cx >= L && cx <= R && cy >= T && cy <= B;
}

for (var i = 0; i < allObj.length; i++) {
    if (curOnObj(allObj[i], curX, curY)) {
        ids.push(allObj[i].getUniqueId());
    }
    if(ids.length > 1){
        break;
    }
}

eventsFunctionContext.returnValue = JSON.stringify(ids);

DeleteById:
Arguments:
obj: Any object
ID: Number

var obj = eventsFunctionContext.getObjects("obj");
var id = Number(eventsFunctionContext.getArgument("ID"));
var objectName = obj.length > 0 ? obj[0].getName(): "";
var allObj = objectName ? runtimeScene.getObjects(objectName) : obj;

for(var i = 0; i < obj.length; i++){
    if(allObj[i].getUniqueId() == id){
        allObj[i].deleteFromScene();
    }
}

YbyID:
Arguments:
obj: Any object
id: Number

var id = eventsFunctionContext.getArgument("id");
var obj = eventsFunctionContext.getObjects("Object");
var objectName = obj.length > 0 ? obj[0].getName() : "";
var allObj = objectName ? runtimeScene.getObjects(objectName) : obj;
for(var i = 0; i < obj.length; i++){
    if(allObj[i].getUniqueId() == id){
        eventsFunctionContext.returnValue = allObj[i].y;
    }
}

XbyID:
Arguments:
obj: Any object
id: Number

var id = eventsFunctionContext.getArgument("id");
var obj = eventsFunctionContext.getObjects("Object");
var objectName = obj.length > 0 ? obj[0].getName() : "";
var allObj = objectName ? runtimeScene.getObjects(objectName) : obj;
for(var i = 0; i < obj.length; i++){
    if(allObj[i].getUniqueId() == id){
        eventsFunctionContext.returnValue = allObj[i].x;
    }
}

I have reconfirmed in another project that my method works, are you on the newest version?

This is the entire event sheet

I added a variable to one to control it via the mouse, but I’ve also tested with the top down behavior using the same variable and it works too.

Your method seems extremely complicated for the same effect, so I want to try to understand what may be going wrong on your side.

Start of scene:

Moving the box around:

After touching one of the boxes:

I even set up an object creation like you mentioned:

Edit: I also confirmed Trigger once isn’t needed, either.

I created a more universal extension and used this event sheet.


IDActions

It uses a bounding rectangle for collisions.

I have the latest version of the engine.

ok …this may appear over complicated but i enjoy creating problems to solve!
I do this sort of thing (i do sometimes refer from another object too).
this looks complicated because there’s a distance check first to isolate and run the checks on the nearest objects. currently this checks if obj with id 1 is in collision with the others using point inside and then only deletes the other object. you could adapt it to work in other situations or view it as ridiculous and ignore it completely.



It saves the ids of the nearest objects and checks if the saved points are inside those objects. It seems to work now for multiple objects but ive probably made a mistake somewhere.
heres the points


it would be great to be able to reference the corners of the collision mask in events (for the benefit of this highly specific (and probably never repeated) situation) just spotted that the delete needs moving below create bomb.
I sensed that many of you were desperate to see it in action…
donket coll

Interesting solution (though a bit complicated, and I don’t like dealing with points), but I’ll probably release an extension that accesses objects by UID and will probably be able to detect collisions even between three or more objects using an array variable.