GML [SOLVED] Collision_Rectangle not working properly

Discussion in 'Programming' started by Sir_Rysik, Aug 14, 2019.

  1. Sir_Rysik

    Sir_Rysik Member

    Joined:
    Mar 30, 2019
    Posts:
    5
    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!
     
  2. curato

    curato Member

    Joined:
    Jun 30, 2016
    Posts:
    417
    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.
     
    Sir_Rysik likes this.
  3. Sir_Rysik

    Sir_Rysik Member

    Joined:
    Mar 30, 2019
    Posts:
    5
    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!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice