Skip to content

Instantly share code, notes, and snippets.

@pixelage
Last active May 17, 2023 11:06
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pixelage/1b943d1bbb2d5db65021a4a386d2b3ce to your computer and use it in GitHub Desktop.
Save pixelage/1b943d1bbb2d5db65021a4a386d2b3ce to your computer and use it in GitHub Desktop.
[Tile Collision Script] Game Maker 2 using tileset for collisions (i take no creadit) #gml #gms2
// movement variables
velocity = [0, 0];
new_gravity = 1.5;
jumpspeed = 28;
max_velocity = [8, 32];
acceleration = 2.1;
// get the tile map id
var _layer_id = layer_get_id("CollisionTiles");
collision_tile_map_id = layer_tilemap_get_id(_layer_id);
///@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 horizontal
x += _velocity[_vector2_x];
// right and left collision
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 vertical
y += _velocity[_vector2_y];
// down and up collision
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;
}
}
// get the input
var _x_input = (keyboard_check(vk_right) - keyboard_check(vk_left)) * acceleration;
// vector variables
var _vector2_x = 0;
var _vector2_y = 1;
// horizantal movement
velocity[_vector2_x] = clamp(velocity[_vector2_x] + _x_input, -max_velocity[_vector2_x], max_velocity[_vector2_x]);
// friction
if _x_input == 0
{
velocity[_vector2_x] = lerp(velocity[_vector2_x], 0, 0.2);
}
// gravity
velocity[_vector2_y] += new_gravity;
// move and collide
move_and_contact_tiles(collision_tile_map_id, 64, velocity);
// jumping
var _on_ground = tile_collide_at_points(collision_tile_map_id, [bbox_left, bbox_bottom], [bbox_right - 1, bbox_bottom]);
if _on_ground
{
// jumping
if keyboard_check_pressed(vk_up)
{
velocity[_vector2_y] = -jumpspeed;
}
} else {
// control jump height
if keyboard_check_released(vk_up) && velocity[_vector2_y] <= (jumpspeed/3)
{
velocity[_vector2_y] = -(jumpspeed/3);
}
}
///@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 to check for 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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment