GML [SOLVED] Collision_Rectangle not working properly

S

Sir_Rysik

Guest
Sprite-0001.png
Hey everyone!

I'm having a problem with the collision_rectangle function in game maker where it's not registering any collisions. The picture provided is an example of what I am using it for. The Orange square will represent the object calling the collision code. The white/yellow/red box is the collision check area. And the gray squares are the objects that it's checking against.

Basically I want the orange object to check a 2x3 grid area in front of and on top of it (In this case the collision area is slightly smaller for the sake of performance (Upping the size has no effect)). If the specified object is within the area then the orange block will destroy itself.

As you can see the collision area should be triggered (represented by the bright red areas). But for some reason the object doesn't destroy itself. As far as I can tell the object will only destroy itself if it is on top of the object it's checking for.

Here is my code:
Create
Code:
/// @description Initialize Variables
Direction = "";
randomize();
Number = irandom_range(3,6);
X = 0;
Y = 0;
alarm[0] = 120;
Alarm 0
Code:
/// @description Create Walls
if (Number == 0)
{
    exit;
}

//Check Collisions
switch(Direction)
{
    case "Right":
    if (collision_rectangle(x + 8, y - 8, x + 24, y + 24, Obj_Solid_Parent, false, true) != noone)
    {
        instance_destroy();
    }
    X = 1;
    break;
    
    case "Up":
    if (collision_rectangle(x - 8, y - 8, x + 24, y + 8, Obj_Solid_Parent, false, true) != noone)
    {
        instance_destroy();
    }
    Y = -1;
    break;
    
    case "Left":
    if (collision_rectangle(x - 8, y - 8, x + 8, y + 24, Obj_Solid_Parent, false, true) != noone)
    {
        instance_destroy();
    }
    X = -1;
    break;
    
    case "Down":
    if (collision_rectangle(x - 8, y + 8, x + 24, y + 24, Obj_Solid_Parent, false, true) != noone)
    {
        instance_destroy();
    }
    Y = 1;
    break;
}

//Create Wall
instance_create_depth(x, y, -100, Obj_Dungeon_Walls);
Number--;

//Move
x += X * 16;
y += Y * 16;

alarm[0] = 120;
I also know that the collision areas are all correct since I have them being drawn for debugging.

Any help would be greatly appreciated!
 

curato

Member
That code likely needs to be in the step event. checking every 120 steps is likely making you miss where the collision occurs.

Also if you want them to be a bit fatter than they appear you could put a smaller image in a larger sprite and just you it for larger collision mask and then you could just use the collision event and greatly simplify what you are doing here.
 
S

Sir_Rysik

Guest
That code likely needs to be in the step event. checking every 120 steps is likely making you miss where the collision occurs.

Also if you want them to be a bit fatter than they appear you could put a smaller image in a larger sprite and just you it for larger collision mask and then you could just use the collision event and greatly simplify what you are doing here.
Thanks for your response! I actually had this code in the Step Event originally but the problem was somehow worse (Objects didn't destroy at all) So I put it in the alarm to slow the process down (Plus I could see what happened a little more clearly). But after reading your post I tried putting the collision checks in the step event and the rest I kept in the alarm and now it works perfectly!

Thanks again for your reply!
 
Top