Collision detection on object groups

—Background—
I’m creating a tower defense game.
Enemies spawn, then follow the same path.
Enemies belong to the same group 'Enemy_All".
Enemies have a variable called ‘Speed’ that dictates their speed.
Enemies have an ID generated on spawn. Lower the ID the younger the enemy.

—What I would like to happen—
Some enemies are quicker than others. When they catch up to a slower enemy. They would take on the speed of the enemy in front + a small added decrease to give space. This would stop them running over the top of each other.

–What is happening—
The code seems to work if the enemies are different types. If they are of the same type. For example enemy1 collides with another enemy1 that has been previously slowed. It would esentially take the speed of the second enemy1 and cause them both to speed up.At that point it chaos ensues!

—What i have tried—
1 -I have tried using the for each object but it was more glitchy than my current solution.
2 - Also tried adding and removing the ‘separate object’ function.
3 -Also tried resetting the variables just in case.

I’m sure, i’m doing something obviously wrong. But its got to the point where my head is now colliding with my keyboard!

thanks :slight_smile:

Same object collisions can be troublesome. Here’s a solution that works for a single collision. If multiple collisions occur at the same time then it may not work as expected and will require extra events.

I’ve used linear movement, not the curved path movement as you have, but hopefully you get the idea:


It looks like this:

Bump

Dude that’s amazing. Thank you :slight_smile: I’m curious had you built this before or did you just build it? I would have never thought about doing it this way. What was your process?

The only thing I needed to add was ‘trigger once’ at the start. Otherwise the code would trigger until the negative Min Speed was hit and the enemies started to run backwards.

Which comical in appearance is not what i wanted :slight_smile:

Thank you :slight_smile:

I built it after reading your post. I don’t have a process - it’s not something I consciously think about and to me it’s just a logical way of doing it.

Ok and thank you again :smile: really appreciate it

1 Like

This is an interesting solution. I want to understand it. What I read:

  • If two instances of the same object collide, make sure they don’t overlap. (Move away)

  • Then set two temporal variables minSpeed and maxSpeed as the speed of the first created instance of the two objects. Check if the second instance has a higher or lower speed than the previous and change minSpeed/maxSpeed accordingly.

  • Now pick the instance of the two, which has the higher speed of the two (speed of instance = maxSpeed) and set its speed to the lower speed - 2 of the two.

Is that about right? Its not really how it looks like.

Yep, that about sums it up.

Not sure if anyone is interested :slight_smile: And thank you again @MrMen

1 -If the enemy is in the process of dying (animation) they are ignored.
2 - I added an ID check to cover for simultaneous collisions.
3 - I also added a speed up action on the enemy in-front.

As much like real traffic. If one enemy is slowed, that slow down is passed down the line until the guys at the end are hardly moving and it turns into a blood bath! :slight_smile: