1. Hey! Guest! The 34th GMC Jam will take place between August 22nd, 12:00 UTC (Thursday noon) and August 26th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

GMS 2 Having trouble with Tile Collision.

Discussion in 'Programming' started by Harmony, Mar 31, 2018.

  1. Harmony

    Harmony Member

    Joined:
    Mar 31, 2018
    Posts:
    2
    So recently I have been trying to work with Tile collision instead of object collision. So what happens is the up and down are perfectly normal, they don't allow the player through. The main problem is that when you walk to the left or right of a certain block it would move the character up or down one depending on where it was. Even more, when you hold up/down it would launch you at light speed. Just wondering what I am doing wrong. I have tried to flip some numbers and where the collision boxes meet, but nothing seems to change how it works.
    Code:
    //Create
    //Variables
    
    //tile maps
    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);
    
    Code:
    //Step
    //Collision
    var dy = 15;
    var dx = 15;
    if ( dy > 0) {
        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
        }
    }
    
    if ( dy > 0) {
        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
        }
    }
    
    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;
        }
    } if (dx > 0) {
        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;
        }
    }
     
  2. Miradur

    Miradur Member

    Joined:
    Jan 16, 2018
    Posts:
    129
    Hi, looks very much like code from GMWolf, here is the original from his video:



    And here the working code:

    Code:
    /// @description Movement
    
    // jump routine
    
    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)) {
            v_speed = -jump_impulse
        }
    }
    
    var dx = move_speed * (keyboard_check(vk_right) - keyboard_check(vk_left))
    var dy = v_speed
    v_speed += grav
    
    // 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 & ~63) - 1) - sprite_bbox_bottom
            v_speed = 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 + 64) & ~63) - sprite_bbox_top
            v_speed = 0
        }
    }
    
    // do horizontal move
    
    x += dx
    
    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
            v_speed = 0
        }
    } else {
        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
            v_speed = 0
        }
    }
    
    Miradur
     
  3. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,661
    Not to sound snarky, do you even know what the difference between "greater than" and "less than" is in math? Because Mirauder helped you out with the code, but what you originally wrote suggests you don't even understand the difference between > and < yet.
     
  4. Harmony

    Harmony Member

    Joined:
    Mar 31, 2018
    Posts:
    2
    So I am sorry that I may not have been clear with what I meant, it was late. To clarify what I meant was I am working on a top-down game, while he was working on a platformer. In his game, he has gravity and was basing it off that. I tried to attempt it with my game, though it didn't work. My main problem is how to turn it into a top-down experience instead of it being based on Platformer experience. I do understand how the Less than and greater than mean, I was just trying to stick to the original code. Sorry for the confusion.
     
  5. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,661
    Well all four of your conditionals were using > when two of them should be using <.

    Also why are you setting dx and dy everytime?
     
  6. Joe Ellis

    Joe Ellis Member

    Joined:
    Aug 30, 2016
    Posts:
    893
    Have you wrote this code yourself? or have you found bits online, serious question cus you really need to know what your doing with stuff like this and pasting peoples code isnt gonna help, you need to spend a good 10 hours to wrap your head around it, I'm just saying from experience lol
     
  7. Miradur

    Miradur Member

    Joined:
    Jan 16, 2018
    Posts:
    129
    If you take code from other people's guides, refer to them. here is another very good guide from GMWolf,
    which might help you more:



    You can also help better if you indicate where you got the code from.


    Miradur
     
    GMWolf likes this.
  8. NatashaD

    NatashaD Member

    Joined:
    Aug 13, 2019
    Posts:
    4
    Im having the exact issue, followed GM Wolfs tile collision system to the T, with some minor adjustments with the tile sizing to fit 32x32 tiles and sprite. The only thing that I didn’t do was implement his jump system. Instead I went with simplicity with,

    If key_space // its a variable
    {
    y -= jump;
    }

    Could this possibly be interfering with the tile collision system and abducting my guy into nothingness?
     
  9. NatashaD

    NatashaD Member

    Joined:
    Aug 13, 2019
    Posts:
    4

    Never mind, found out my gravity was maybe a bit too light which caused my guy to launch, tweaked the settings a bit and my guy seems to just chill on the ground.
     

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