Collisions between instances of the same object?

Hi guys!

I have a lot of Asteroids in my space shooter game that are heading towards the player. After filling the screen with them I suddenly realized that it would be cooler if they bounce from each other based on their size, force etc.

So for starters
I want to make it so that when an Asteroid collides with another one, each one goes to the opposite direction.

image

Any easy solutions? It seems that Gdevelop’s collision system can’t recognize two instances as different entities if they belong to the same object

Asteroids force

What is the result now?

Try to use the.

Repeat for each instance

Condition

1 Like

Nah still nothing even with repeat for each instance

I think you need to play a bid with the sub events and with the trigger once.

so what you could do is.

-For each object of asteroids.
-when its in collocision
trigger once.

then as action stop the asteroiid
and as subaction make it move again

One way is to have a second hidden object, say called “collider”, with the same collision mask as the asteroid. Stick a collider object to each asteroid object. Then check for collision between collider and asteroid objects (with the additional condition that the collider is not stuck to the asteroid) and separate the two objects.

1 Like

with some testing with my own stacking game i indeed see that it indeed ignores instances of itself to collide with

reading the page we mitgh want to play with the layers and masks aswell 2D Physics Engine - GDevelop documentation

If you use physics then you don’t need to worry about collision between object of the same type - the physics engine will work that out for you.

However, don’t mix physics behaviour and standard movement/collisions. It’ll lead to tears and frustration. Use one or the other, but not a combination to control the objects.

Can you provide an example/screenshot on how to do this?

I tried to make this, but I fail to make it work

How do you mean? If you use physics behaviour then you don’t need any events to move physics objects apart when they collide.

Hi, I replied to your first reply regarding the collider solution. I have no idea how to properly do it :sweat_smile:
Sadly I can’t use Psychics as they don’t fit my game.

Everything mrmen said is correct…

if you’re not with physics 2 engine…

imo i would approach the problem with the Id counter method.
Assigning an id for each meteor (so each meteor have unique id) and at collision do something with the bounce extension…

I’m telling this without even have tested it…so consider it just a probable input.

Doh, half asleep :confused:

This method works, but the separate method is a bit jarring visually. You may want to explore the bounce extension.

Just using the “is stuck to” condition isn’t enough. Instead give an unique id for each asteroid and it’s collider object using a scene variable :

  • create an asteroid collider object by duplicating the asteroid object, and add the instance variable “id” to each:

  • add sticker behaviour to the collider object:

  • when you create an asteroid also create the collider, stick it to the asteroid, hide it and give both objects the same id value:

  • check for collision between an asteroid and collider that isn’t stuck to it. Counter to what I normally preach about using for each object, this is the only way I could make it work:
    image

1 Like

Thank you for the well written reply.

For now I have tried the Bounce extension. Literally just added the extension on the object and added an action that states that the asteroid should bounce away from each asteroid. I don’t know what kind of black sorcery the bounce extension is using but it works. Not perfect but it does the basic thing that I want.

Now because I want to calculate the force of the asteroid, cause I want some of them to get pushed back or get crushed by bigger ones I will try the collider option. Just wish there was a way to do it with the bounce extension.

One option for bouncing off each other is possibly adding an object var ‘force’ and object var ‘force x’ and ‘force y’. Give them unique id. Repeat for each. When colliding set the ‘force’ to a number based on size etc and the ‘force x’ ‘force y’ to the point of collission. In the repeat for each events. if ‘force’ greater than zero apply force ‘force’ in direction away from point force x, force y. and decrease ‘force’ by something like 100 to slow them down again