• Hey! Guest! The 40th (!!!) GMC Jam will take place between February 25th, 12:00 UTC to March 1st 12:00 UTC. Why not join in this very special anniversary jam! Click here to find out more!

Game Mechanics [Discussion]Collision codes

zendraw

Member
so recently ive wrote a collision code that i find most functional and light then anything ive seen. Collision code that deals with instances and is not grid based, thus giving you maximum freedom of usage.
this is the code
Code:
var ins=instance_place(x+x_speed, y, o_block);

if (instance_exists(ins))
{
    if (ins.block)
    {
        if (x<ins.x)
        {
            x=ins.bbox_left-half_mask_width;
        } else
        {
            x=ins.bbox_right+half_mask_width;
        }
        x_speed=0;
    }
}

ins=instance_place(x, y+y_speed, o_block);

if (instance_exists(ins))
{
    if (ins.block)
    {
        if (y<ins.y)
        {
            y=ins.bbox_top-half_mask_width;
        } else
        {
            y=ins.bbox_bottom+half_mask_width;
        }
        y_speed=0;
    }
}

x+=x_speed;
y+=y_speed;
-one thing i need to note, since the distance betwean bbox_left-x and x-bbox_right is never the same, i use (half the calculated width)+1; otherwise the instance gets stuck either on the left or right, top or down.

now the code isnt yet polished and needs work on, but it is basically that, instead of shooting in the dark with a While statement and doing things the hard way, you just calculate the final resault.
the functionality of the code comes mainly from storing the instance that you collide with in a variable. once you have that variable you can do alot.
a more flexible code wuld be this

Code:
var ins=instance_place(x+x_speed, y, o_block);

if (instance_exists(ins))
{
    script_execute(scr_collision+ins.type, ins.id, id);
}

ins=instance_place(x, y+y_speed, o_block);

if (instance_exists(ins))
{
    script_execute(scr_collision+ins.type, ins.id, id);
}

x+=x_speed;
y+=y_speed;
now you can go crazy with scripts and this can cover all of your collision codes, like interacting with instances. for that you can have a script like this:

Code:
switch (argument1.object_index)
{
case o_door:
     //interact with door code
     break;
case o_portal:
     //interact with portal code
     break;
}
and so on. main thing about this is that it doesnt use a grid, it has high funcitonality, and is very light.
 

Niels

Member
What's the advantage of using this code over the "usual" place_meeting-while loop version that most people use?
 

zendraw

Member
functionality, and performance. once you get the ID of the instance you collide with you can do whatever you want and interact it however you want, see my example. also not doing loops but a simple calculation saves alot of performance when you have alot of instances runing the code.
 

zendraw

Member
you are not missing anything, true its only for rectangular masks. altho im sure theres a way to make it for non rectangular masks also, its very yung in my coding practices.

also i need to point out that it shuld be used per instance type. what i mean by that is you shuldnt use it for instance that is impassable and passable becouse you take the ID of only one instance that you collide with, and if you take the ID of the passable instance, it wont trigger the code for the impassable one. for interactive objects i typically use this

Code:
if (place_meeting(x, y, o_interactive))
{
  var int=instance_nearest(x, y, o_interactive);
  switch (int.object_index)
{
case o_door:
case o_portal:
etc.
}
}
this way you interact with the most nearest instance of that object even if you collide with multiple of it. i usually use this code in a keyboard check if statement so the code is run once. also you dont need to be checking the object_index of the instance, but the sprite or some special variable that defines it.
 
Top