1. Hey! Guest! The 35th GMC Jam will take place between November 28th, 12:00 UTC - December 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

[SOLVED] Collision / checking for free space.

Discussion in 'Programming' started by Fredrik, Oct 14, 2019.

  1. Fredrik

    Fredrik Member

    Joined:
    Jun 21, 2016
    Posts:
    183
    I have a question. It might be a "noobish" question, but I have a generation system that generates something in a grid-form, and each instance in each grid goes under the parent "room_parent" and i want each instance under room_parent to check in certain directions if there is any further rooms generated. With this, I thought of using

    if position_meeting(x, y,room_parent) {what to happend...}

    to check for any further instances in a certain direction, but with this function, do the instances actually have to collide? or can I check if a certain instance is, let's say position_meeting(x - 16, y, obj) without the instances actually colliding? and if not, is there any other function for this?


    Edit:
    As of now I've been using place_empty to check if a space in a given direction is empty, but that causes bugs and some generations... so I wanna do the same. Check in one given direction, but to check if there is a instance under room_parent there.
     
  2. NoFontNL

    NoFontNL Member

    Joined:
    Feb 10, 2018
    Posts:
    303
    position_meeting
    With this function you can check a position for a collision with another instance or all instances of an object. When you use this you are checking a single point in the room for an instance or an object.

    You give a position and the game will determine if that point is colliding with obj.
     
  3. Fredrik

    Fredrik Member

    Joined:
    Jun 21, 2016
    Posts:
    183
    Thanks for replying.
    I've tried position_meeting
    I might be doing something wrong, but I cannot get it to work...
    I want instances under room_parent to check for other instances under the same parent in the directions up,down,left and right, so I wrote it like this:

    Code:
    // up.
    if position_meeting(x,y-1,room_parent)
        {instance_create(x+128,y,wall_blockade_up);}
    
    // down.
    if position_meeting(x,y+289,room_parent)
      {instance_create(x+128,y+256,wall_blockade_down);}
    
    // left.
    if position_meeting(x-1,y,room_parent)
        {instance_create(x,y+128,wall_blockade_left);}
    
    // right.
    if position_meeting(x+289,y,room_parent)
        {instance_create(x+256,y+128,wall_blockade_right);}
    
    (all object under room_parent are 288x288, so that's why I check 288 + 1)
    I also tried to check further out than just one pixel but that didn't work either :/
     
  4. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,007
    position_meeting() doesn't care who calls the code, so it can also return true if the calling instance is at that position (and is one of the objects being checked). If you want to exclude the calling instance, you need to either use place_meeting(), collision_point(), or any of the other collision_* functions. The collision_* functions all have the last argument notme, which tells the function to ignore the calling instance.

    So in summary:
    • position_meeting() checks if any instance of the specified object (or the actual instance specified) occupies the coordinates specified
    • place_meeting() hypothetically "places" the calling instance at the coordinates specified and then checks for a collision with at least one instance of the specified object, assuming both instances have a sprite assigned
    • collision_point() checks if any instance of the specified object (or the actual instance specified) occupies the coordinates specified, but can ignore the calling instance if opted
    • collision_rectangle() checks if any instance of the specified object (or the actual instance specified) overlaps the rectangular area as defined by the two specified coordinates, but can ignore the calling instance if opted.
    I don't notice anything in your code that would necessitate using the collision_* functions, though, assuming ALL your room objects have their origin set in the top-left corner and their bounding boxes do not exceed those dimensions you specified.

    What's results is your code currently generating?

    Are you trying to make it block off any connections to other rooms? Because that's what it looks like is going on. Your code seems to say, "if there is a room above me, put a wall between me and the room above me."

    Edit: You also don't need to use 289; 288 will suffice. A sprite that is 288 pixels wide spans {0,287}, so a coordinate at x+288 will be outside the sprite.
     
    Last edited: Oct 14, 2019
  5. Fredrik

    Fredrik Member

    Joined:
    Jun 21, 2016
    Posts:
    183
    Thanks alot for the reply. This helps alot!
    You are right about that I'm trying to do in the script. But I realised that the code I posted was wrong. How I originally had made it was that the objects as you can see in my code, thats spawned in to block off vertain ways
    (wall_blockade_up, wall_blockade_down, and so on...) would spawn in anyways, and then they would check if a place was empty with place_empty, and if it was empty it would stay, and if not, it would delete it self. This caused some bugs where certain paths that were supposed to be blocked off were not, and some paths that were not supposed to be blocked would get blocked, which would sometimes break the level, making it impossible to progress.
    This was a rare bug, but it happend, so now I wanted to improve it by making so it would only spawn the blockade objects if a path is supposed to be blocked! and the first example I wrote for this solution is the code I also shared here. What I can see not is that I forgot a exclamation mark. It's supposed to be if !position_meeting(x,y-1,room_parent), which seems to work now, but I gotta test it some more to be sure. By what you wrote it seems like collision_point might be something to try as well, if the current solution doesnt work.

    I'll update if it works!
    Thanks alot for replying.

    Update:
    It works perfectly.
     
    Last edited: Oct 16, 2019

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