GameMaker Having trouble with Tile Collision.

H

Harmony

Guest
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;
    }
}
 

Miradur

Member
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
 

TheouAegis

Member
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.
 
H

Harmony

Guest
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.
 

TheouAegis

Member
Well all four of your conditionals were using > when two of them should be using <.

Also why are you setting dx and dy everytime?
 

Joe Ellis

Member
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
 

Miradur

Member
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
 
N

NatashaD

Guest
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?
 
N

NatashaD

Guest
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?

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.
 
Top