Design [solved]Tile collision code not working but, its close. I can feel it.

Discussion in 'Game Design, Development And Publishing' started by flyinian, Jan 23, 2019.

  1. flyinian

    flyinian Member

    Joined:
    Feb 5, 2018
    Posts:
    60
    Hey all. I am currently attempting to get this tile collision to work but, haven't yet. However, i believe I managed to narrow what might be breaking the code for me. When I disable the "else" statements within the "if" statements the player collides with the tile and i am able to move left and right. However, when i enable the "else" statements, the player instantly goes down vertically and off to the right...I also go vertically down and off to the right when the entire code is active. I have been stuck on this code for many hours into attempting to get this code to work and I decided to try my hand here. Ill be going at it once again today and see if i can narrow it down further.

    My player is 32 x 32 and the tiles are 64 x 64. So it can't be the player size being to big and bypassing the collision checking.

    Here is the collision code:

    Code:
    var dx = move_speed * (keyboard_check(vk_right) - (keyboard_check(vk_left)));
    var dy = v_speed;
    v_speed += grav;
    
    
    
    
    
    //vertical
    
    y += dy;
    if ( dy > 0)
    {
    //down
        var t1 = tilemap_get_at_pixel(tilemap, bbox_left, bbox_bottom) & tile_index_mask;
        var t2 = tilemap_get_at_pixel(tilemap, bbox_right, bbox_bottom) & tile_index_mask;
    
    if (t1 != 0 || t2 != 0)
    
    {
            y = ((bbox_bottom & ~ 63) -1) - sprite_bbox_bottom; 
            v_speed = 0;
    }
    }
    
    else
    
    {
        ///up
        var t1 = tilemap_get_at_pixel(tilemap, bbox_left, bbox_top) & tile_index_mask;
        var t2 = tilemap_get_at_pixel(tilemap, bbox_right, bbox_top) & tile_index_mask;
     
        if(t1 != 0 || t2 = 0)
        {
            y = ((bbox_top + 64) & ~63) - sprite_bbox_top;
            v_speed = 0;
     
        }
    
    }
    
    ///___________________
    x += dx;
    
    ///right
    if ( dx > 0)
    {
    
        var t1 = tilemap_get_at_pixel(tilemap, bbox_right, bbox_top) & tile_index_mask;
        var t2 = tilemap_get_at_pixel(tilemap, bbox_right, bbox_bottom) & tile_index_mask;
    
    if (t1 != 0 || t2 != 0)
    
    {
            x = ((bbox_right & ~ 63) -1) - sprite_bbox_right; 
    
    }
    
    }
    
    else
    //left
    {
        var t1 = tilemap_get_at_pixel(tilemap, bbox_left, bbox_top) & tile_index_mask;
        var t2 = tilemap_get_at_pixel(tilemap, bbox_left, bbox_bottom) & tile_index_mask;
     
        if(t1 != 0 || t2 = 0)
        {
            x = ((bbox_left + 64) & ~63) - sprite_bbox_left;
     
        }
    
    }
    

    Here is the variable code for the player:
    Code:
    move_speed = 8;
    jump_impulse = 21;
    grav = 0.75;
    v_speed = 0;
    
    //tilemap information
    var l = layer_get_id("collision_map");
    tilemap = layer_tilemap_get_id(l);
    
    //sprite info
    sprite_bbox_left = sprite_get_bbox_left(sprite_index) - sprite_get_xoffset(sprite_index);
    sprite_bbox_right = sprite_get_bbox_right(sprite_index) - sprite_get_xoffset(sprite_index);
    sprite_bbox_bottom = sprite_get_bbox_bottom(sprite_index) - sprite_get_yoffset(sprite_index);
    sprite_bbox_top = sprite_get_bbox_top(sprite_index) - sprite_get_yoffset(sprite_index);

    Thank you.
     
  2. flyinian

    flyinian Member

    Joined:
    Feb 5, 2018
    Posts:
    60
    I found my problem. I was missing the "!" in the code....I can't...believe it. Atleast I fixed it and can now move on. :)

    P.s. how do I mark "solved" or delete the post?
     

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