need help with tile collisions

Discussion in 'Programming' started by Guywithabag, Feb 11, 2019.

  1. Guywithabag

    Guywithabag Member

    Joined:
    Feb 11, 2019
    Posts:
    1
    I have a sprite with a collision box that is rectangular shaped but it seems to teleport or push itself downwards then snap to a grid and can sometimes go through walls. Please i need someone to help me

    i am using the code in
    Tilemap Collisions in GMS 2 - GMWolf

    var dx = moveSpeed * (keyboard_check(vk_right) - keyboard_check((vk_left)));
    var dy = vsp;
    vsp += grav;

    //jump
    var t1 = tilemap_get_at_pixel(tilemap, bbox_left, bbox_bottom + 1) & tile_index_mask;
    var t2 = tilemap_get_at_pixel(tilemap, bbox_right, bbox_bottom + 1) & tile_index_mask;
    if (t1 != 0 || t2 != 0) {
    if (keyboard_check(vk_up)) {
    vsp = -jump_impulse;
    }
    }
    //do vertical move
    y += dy;
    if (dy > 0) { //downwards
    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 & ~31) - 1) - sprite_bbox_bottom;
    vsp = 0;
    }

    } else { //upwards
    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 +32) & ~31) - sprite_bbox_top;
    vsp = 0;
    }
    }

    //do horizontal move
    x += dx;
    if (dy > 0) { //right
    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 & ~ 31) - 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 + 32) & ~31) - sprite_bbox_left;

    }
    }
     

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