# GMS 2Collision Problems

J

#### Jaxon

##### Guest
v2.1.4.285
I am using a basic collision system(place_meeting) for my 2d platformer. But due to my tileset having to be snapped to the grid(middle centre) for edges to of tile to show. and I use a !visible object that acts as walls and floor. but I have to use 4 different objects to cover the tile's(because of the middle centre alignment). but when I put all objects into my code the game lags(and quits) as soon as a touch the floor or ground(instantly).
my code
[//Get Input
if (hascontrol)
{
key_left = keyboard_check(vk_left) or keyboard_check(ord("A"));
key_right = keyboard_check(vk_right) or keyboard_check(ord("D"));
key_jump = keyboard_check_pressed(vk_space);
}
//Calculate Movement
var move = key_right - key_left;
hsp = move * walksp;

vsp = vsp + grv;

if (place_meeting(x,y + 1, obj_wall)) xor (place_meeting(x,y + 1, obj_wall2)) xor (place_meeting(x,y + 1, obj_wall3)) xor (place_meeting(x,y + 1, obj_wall4)) and (key_jump)
{
vsp = -5;
}
else
{
key_left = 0;
key_right = 0;
key_jump = 0;
}

//Horizontal Collision
if (place_meeting(x + hsp,y,obj_wall)) xor (place_meeting(x + hsp,y,obj_wall2)) xor (place_meeting(x + hsp,y,obj_wall3)) xor (place_meeting(x + hsp,y,obj_wall4))
{
while (!place_meeting(x + sign(hsp),y,obj_wall)) xor (!place_meeting(x + sign(hsp),y,obj_wall2)) xor (!place_meeting(x + sign(hsp),y,obj_wall3)) xor (!place_meeting(x + sign(hsp),y,obj_wall4))
{
x = x + sign(hsp)
}
hsp = 0;
}
x = x + hsp;

//Vertical Collision
if (place_meeting(x,y + vsp,obj_wall)) xor (place_meeting(x,y + vsp,obj_wall2)) xor (place_meeting(x,y + vsp,obj_wall3)) xor (place_meeting(x,y + vsp,obj_wall4))
{
while (!place_meeting(x,y + sign(vsp),obj_wall)) xor (!place_meeting(x,y + sign(vsp),obj_wall2)) xor (!place_meeting(x,y + sign(vsp),obj_wall3)) xor (!place_meeting(x,y + sign(vsp),obj_wall4))
{
y = y + sign(vsp)
}
vsp = 0;
}
y = y + vsp;

//Animation
if (!place_meeting(x,y + 1,obj_wall)) xor (!place_meeting(x,y + 1,obj_wall2)) xor (!place_meeting(x,y + 1,obj_wall3)) xor (!place_meeting(x,y + 1,obj_wall4))
{
sprite_index = spr_player_jump;
image_speed = 0;
if (sign(vsp) > 0)
{
image_index = 1;
}
else
{
image_index = 0;
}
}
else
{
image_speed = 1;
if (hsp == 0)
{
sprite_index = spr_player_still;
}
else
{
sprite_index = spr_player_run;
}
}

if (hsp != 0)
{
image_xscale = sign(hsp);
}]
Sorry about putting 78 lines of code in but I thought (just in case) my movement could be changed as well in the need of a different system.
Thx

R

#### RandumbPurson

##### Guest
Whenever you have that many objects it's gonna be really slow, especially if it's doing several collision calculations at once. The first thing you can do to alleviate this is only use one object per tile. You can change the grid size by clicking the pull down menu next to the "show grid" option in the room editor. Even better than that, you can uncheck "snap to grid" and it will make it so that you can move an object anywhere you want. If I understand you correctly there might be an even better option as well. You said that you have a tileset so you have the option, but are in no way obligated, to do tile collisions instead. Tile collisions will drastically reduce the impact on your machine but they are slightly more difficult. I would start off by just reducing the number of objects by changing the grid snap but if that doesn't help very much you can try tile collisions, there are a lot of very helpful videos that explain how to do them.

J

#### Jaxon

##### Guest
Whenever you have that many objects it's gonna be really slow, especially if it's doing several collision calculations at once. The first thing you can do to alleviate this is only use one object per tile. You can change the grid size by clicking the pull down menu next to the "show grid" option in the room editor. Even better than that, you can uncheck "snap to grid" and it will make it so that you can move an object anywhere you want. If I understand you correctly there might be an even better option as well. You said that you have a tileset so you have the option, but are in no way obligated, to do tile collisions instead. Tile collisions will drastically reduce the impact on your machine but they are slightly more difficult. I would start off by just reducing the number of objects by changing the grid snap but if that doesn't help very much you can try tile collisions, there are a lot of very helpful videos that explain how to do them.
Thx I will try turning off snap to grid. and by the way, I did look into tile collisions but instead of using a background image I have a tile as my background and then a 16 autotile set over the top to create my rooms(look/style).
Thx anyway will return back if it doesn't work.

J

#### Jaxon

##### Guest
Thx I will try turning off snap to grid. and by the way, I did look into tile collisions but instead of using a background image I have a tile as my background and then a 16 autotile set over the top to create my rooms(look/style).
Thx anyway will return back if it doesn't work.
It worked massive help thx