R
Ralliare
Guest
I've read a few blog posts and watched a few different YouTubers go on about the benefits of using tile collisions in GM2. From having a play around with it it does seem like a great way to quickly draw collisions, especially on more complex leves. I don't think anyone like dumping loads of objects onto the old room editor.
There is one thing I have noticed when trying it out. The maths (and by extension everyone) says it only works with powers of 2. The only examples I can find at for 64x64 tile sets. So I thought I'd try out Heartbeasts implementation but go with 8x8 grid.
And then my headache started. I thought I must have coded something wrong as my player object sumk down and right one pixel. So I grabbed his project files, reworked the one code reference to tile sizes, updated the objects, sprites and tile sets to 8x8, and the same issue again. I tried the approach GMwolf used. And got the same issue.
I only understand the basic concept of the binary used, but from my understanding any power of 2 should work for tile size, and yet when I tried, 8, then 16 neither of these worked.
Anyone have any insight on this? I'd love to try this out in a game idea I've been tossing around that would require a large number of objects on screen so the performance boost would be useful.
tile_collide_at_points script
move_and_contact_tiles script called from player step event
There is one thing I have noticed when trying it out. The maths (and by extension everyone) says it only works with powers of 2. The only examples I can find at for 64x64 tile sets. So I thought I'd try out Heartbeasts implementation but go with 8x8 grid.
And then my headache started. I thought I must have coded something wrong as my player object sumk down and right one pixel. So I grabbed his project files, reworked the one code reference to tile sizes, updated the objects, sprites and tile sets to 8x8, and the same issue again. I tried the approach GMwolf used. And got the same issue.
I only understand the basic concept of the binary used, but from my understanding any power of 2 should work for tile size, and yet when I tried, 8, then 16 neither of these worked.
Anyone have any insight on this? I'd love to try this out in a game idea I've been tossing around that would require a large number of objects on screen so the performance boost would be useful.
tile_collide_at_points script
Code:
///@param tile_map_id
///@param point_arrays...
var tile_map_id = argument[0];
// Found variable
var found = false;
// for the point arrays
var vector2_x = 0;
var vector2_y = 1;
// Loop through the points and check for a tile
for (var i=1; i<argument_count; i++) {
var point = argument[i];
found = found || tilemap_get_at_pixel(tile_map_id, point[vector2_x], point[vector2_y]);
}
// return found
return found;
Code:
///@param tile_map_id
///@param tile_size
///@param velocity_array
var tile_map_id = argument0;
var tile_size = argument1;
var velocity = argument2;
// For the velocity array
var vector2_x = 0;
var vector2_y = 1;
// Move horizontally
x += velocity[vector2_x];
// Right collisions
if velocity[vector2_x] > 0 {
var tile_right = tile_collide_at_points(tile_map_id, [bbox_right-1, bbox_top], [bbox_right-1, bbox_bottom-1]);
if tile_right {
x = bbox_right & ~(tile_size-1);
x -= bbox_right-x;
velocity[@ vector2_x] = 0;
}
} else {
var tile_left = tile_collide_at_points(tile_map_id, [bbox_left, bbox_top], [bbox_left, bbox_bottom-1]);
if tile_left {
x = bbox_left & ~(tile_size-1);
x += tile_size+x-bbox_left;
velocity[@ vector2_x] = 0;
}
}
// Move vertically
y += velocity[vector2_y];
// Vertical collisions
if velocity[vector2_y] > 0 {
var tile_bottom = tile_collide_at_points(tile_map_id, [bbox_left, bbox_bottom-1], [bbox_right-1, bbox_bottom-1]);
if tile_bottom {
y = bbox_bottom & ~(tile_size-1);
y -= bbox_bottom-y;
velocity[@ vector2_y] = 0;
}
} else {
var tile_top = tile_collide_at_points(tile_map_id, [bbox_left, bbox_top], [bbox_right-1, bbox_top]);
if tile_top {
y = bbox_top & ~(tile_size-1);
y += tile_size+y-bbox_top;
velocity[@ vector2_y] = 0;
}
}