3
\$\begingroup\$

There are many objects named obj_Cell and obj_Core in the room, each Core has a number (0 to 10) of Cells that belong to it. Each Cell record its corresponding Core with the variable ID_MyCore, which is the id of its Core.

Collisions/overlapping between Cells belonging to different Cores are to be detected. The purpose of this is to know which Cells are overlapping with a "foreign" Cell.

The following code is used, it is run in the step event of every Cell

with(obj_Cell)
{
    if place_meeting(x,y,other)=true
    {
        if other.ID_MyCore!=ID_MyCore
        {
            //do action
        }
    }
}

The annotated version of the above:

with(obj_Cell)  //get every cell in the room to do the following:
{
    if place_meeting(x,y,other)=true  //if you are touching the cell that is runing the script...
    {
        if other.ID_MyCore!=ID_MyCore  //...and your core is not the other cell's core
        {
            //do the thing that should be done when two foreign cells touch
        }
    }
}

My question is, is there a way of doing the same but faster? As it is, the number of times these scripts need to be run is the square value of total number of cells in the room, I intend to have hundreds of cells simultaneously.

\$\endgroup\$
2
  • \$\begingroup\$ Do the cores have fixed size bounds? Do the cells stay within the cores' bounds? If you can identify some coherence about the location of these objects, you should be able to optimise collision detection using a bounding volume hierarchy type approach. Please include any rules the cells obey aside from the fact they are grouped by core ID. \$\endgroup\$ – zcabjro Jun 30 '16 at 7:44
  • 1
    \$\begingroup\$ You can halve the check count by only checking cells with higher ID since collisions are mutual. \$\endgroup\$ – wondra Jun 30 '16 at 10:22
1
\$\begingroup\$

The only thing you need to do is removing the with statement. Putting a with statement in the Step Event of an object whose object_index is the same as the statement's will slow down your game very, very much. That's why.

Let's say, there are n instances of obj_Cell. Your current code will make every instance of obj_Cell to execute a snippet for any current instance of obj_Cell, to check for collisions. That means, you are performing n collision checkings per instance, for a total of n2 total operations. That's because Step Event defined for the object is executed for all existing instances til that moment.

The idea behind object-oriented programming is defining behaviors for an object which interacts with the world. So, if you tell any cell what to do only for itself, you will have n cells performing collision checking, in other words there will be n checks instead of n2:

Step Event of obj_Cell:

// Step Event
if place_meeting(x,y,other)=true
{
    if other.ID_MyCore!=ID_MyCore
    {
        //do the thing that should be done when two foreign cells touch
    }
}

The with statement would best fit if it was executed by an object different from obj_Cell, maybe by using the function instance_place() to keep track of which instance collides with another, instead of place_meeting().

About collisions, there are a lot of things that can be said. If your obj_Cells are circle-shaped, for example, you may even check for the closest one's distance, and check if it is smaller that their radius (which is a positive collision) instead of using pixel-perfect mask-based collision, that may be slower for a high number of instances.

To end up, I suggest you to test this script for your Step Event collision script:

// Step Event
var obj_other = instance_place(x,y,obj_Cell);

if (obj_other!=noone)
{
    if (obj_other.ID_MyCore!=ID_MyCore)
    {
        // Perform action for collision with different cell
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.