Reaching Critical Mass (3 Objects Touching?)

B

Broken_squirrel

Guest
Hello all, this is my first post about anything related to Game Maker. I started using Game Maker back when it was called Game Maker 6. After making a statement like that, most people would expect me to say something about how pro I am at code. But that would not be true, in fact, quite the opposite. Despite having used Game Maker for this long, I still am quite awful at coding. I hate to be the one who asks for help but I've come to realize that the point of these forums is to help others who face similar issues. The problem I am facing is at least an interesting puzzle to solve from a coding perspective (for me at least). Here it is:

I have most a game completed but like everyone else, features are being added over time. It is a platformer that has some physics puzzles. I wanted to add an object to the game that would act like plutonium reaching critical mass. Once a certain amount of the stuff all comes into contact, a reaction happens. It would be a little physics object that could drop, and be pushed around. And the explosion could be triggered even if the 3rd pellet didn't touch the first.

The idea is: If three instances of that object were to come into contact and stay in contact, after a short delay, the objects would explode. To make things worse, I added different colors of the object and that an explosion would only occur if 3 of the same color were touching.

The short version: How can I make an object that can detect that a chain of 3 are in contact? Then, explode all of them?

Like this with red and green pellets...

Boom Graph.jpg
Boom Graph2.jpg

My attempt at solving this...

GML:
//Create Event
Pelletcolor = 1; // I have 1 representing green. and 2 representing red.
Colorpair = false;
tripleTimer = 0;
GML:
//Step Event

if place_meeting(x,y,par_Collider) //check for a collision. All physics objects are children of this object.
{
    if other != obj_Wall //Check that the object is not a wall.
        {
        collidedColor = other.Pelletcolor; //set up a variable that gets the color code of the object we collided with.
        
    
        if collidedColor = Pelletcolor // If these colors match then a pair of colors are in contact.
            {
                Colorpair = true;
                firstOther = other.id; // get the id of the that object so we can destroy it later.
            
                if place_meeting(x,y,par_Collider) // Check for another object touching.
                    {
                        if other != obj_Wall || firstOther && collidedColor = other.Pelletcolor //That object can't be a wall or that first color matching object, it also needs to match colors.
                        {
                            secondOther = other.id; // Now there's another matching color in contact (I think?) This variable will get that object's ID.
                            tripleTimer = tripleTimer + 1; // Start the timer for the explosion.
                        }
                        else
                        {
                            tripleTimer = 0;    // We lost contact before the timer we off.
                        }
    
                        if tripleTimer > 90 // This is the explosion trigger. Once this variable counts to 90 it goes off. Actual explosion to come, lets just get this working first.
                            {
                                instance_destroy(); //destroy this object
                                instance_destroy(firstOther); //destroy that first object we touched.
                                instance_destroy(secondOther); // destroy that second object we touched.
                            }
                    }
                        else
                        {
                            Colorpair = false;   
                        }
    
            }
        }
}
This is all very sloppy and definitely not the best way to go about solving this problem. But had to start somewhere. ¯\_(ツ)_/¯

With this code, hitting run will bring the objects in with no errors, once they bounce around a bit they will start exploding. However they can explode without being in direct contact. So I'm thinking I need a check for when the objects come out of contact.

BUT is this the way? I am starting to feel that this particular method is barking up the wrong tree. I feel like there must be an easier way that exists but I have never attempted writing a code like this. I rarely use "other" I barely understand how it works.

What would you do? Is there a better method?
 

Nidoking

Member
If you're looking for three circles to be touching, then one of those circles must be touching both of the other two. If a circle is touching two other, distinct circles of the same color, then you have a chain of three or more of that color. If not, then you don't. Make it four or more and you have a difficult problem, but this is as simple as that.
 
Top