# Top down collision

Z

#### zilo

##### Guest
Hey all,

I'm a GM noob and have a couple of questions regarding collision.

I've been following the various YT tutorial for GM:S online and have been getting mixed results.

On one end some of them use built in physics for their collision and others programmaticly applying their collisions. I've found it easier to just use physics and move things with phy_position_x and phy_position_y ,but I know this isn't really a thing you should do (according to the GM manual).

I was wondering if I could get some advice on if I should be using GM's physics or writing my own collision stuff.

Here's my current top down movement code, I'm not really sure how to approach wall collisions with it.

This is in my Step event
Code:
``````right_key = keyboard_check(vk_right);
left_key = keyboard_check(vk_left);
up_key = keyboard_check(vk_up);
down_key = keyboard_check(vk_down);

//Get the axis
var xaxis = (right_key - left_key);
var yaxis = (down_key - up_key);

// Get direction
var dir = point_direction(0,0,xaxis,yaxis);

//get the length
if (xaxis == 0 && yaxis == 0)
{
len = 0;
}
else
{
len = spd;
}

// Get the h spd and v spd
hspd = lengthdir_x(len,dir);
vspd = lengthdir_y(len,dir);

// Move

x += hspd;

y += vspd;``````

P

#### Paolo Mazzon

##### Guest
I fail to understand your logic here. Your code is way over complicated. That whole block can be simplified to
Code:
``````x += (-keyboard_check(vk_left) + keyboard_check(vk_right)) * spd;
y += (-keyboard_check(vk_up) + keyboard_check(vk_down)) * spd;``````
As far as collisions go, I use Shaun Spalding's method of setting the speed, checking if there is a wall there, and moving there. If there is a wall, you move as close as possible to the wall without going into it then stop. That implementation is also incredibly simple
Code:
``````hspd = (-keyboard_check(vk_left) + keyboard_check(vk_right)) * spd;
vspd = (-keyboard_check(vk_right) + keyboard_check(vk_down)) * spd;

if (place_meeting(x + hspd, y, WALL) {
while (!place_meeting(x + sign(hspd), y, WALL)
x += sign(hspd);
hspd = 0;
}
x += hspd;

if (place_meeting(x, y + vspd, WALL) {
while (!place_meeting(x, y + sign(vspd), WALL)
y += sign(vspd);
vspd = 0;
}
y += vspd;``````

#### ZeeStudio

##### Member
I Checked this code it also check the collisions
Code:
``````hspd = (keyboard_check(vk_right)-keyboard_check(vk_left))*4;
vspd = (keyboard_check(vk_down)-keyboard_check(vk_up))*4;

if ( place_free(x+(hspd),y+(vspd)) )
{
x += hspd;
y += vspd;
}``````
but don't know that this code is good for saving memory or not xD

P

#### Paolo Mazzon

##### Guest
Those collisions aren't pixel perfect though...

B

#### Ben Turner

##### Guest
[SOLVED] The reason why to use the point_direction() method over shaun's one is that you get a much smoother movement with point direction.
I'm having trouble with the same thing at the moment where I can't seem to figure out how do collisions either, and I specifically want to use to point_direction method because (in my opinion) if feels like a better movement system when playing. Any ideas?

I fail to understand your logic here. Your code is way over complicated. That whole block can be simplified to
Code:
``````x += (-keyboard_check(vk_left) + keyboard_check(vk_right)) * spd;
y += (-keyboard_check(vk_up) + keyboard_check(vk_down)) * spd;``````
As far as collisions go, I use Shaun Spalding's method of setting the speed, checking if there is a wall there, and moving there. If there is a wall, you move as close as possible to the wall without going into it then stop. That implementation is also incredibly simple
Code:
``````hspd = (-keyboard_check(vk_left) + keyboard_check(vk_right)) * spd;
vspd = (-keyboard_check(vk_right) + keyboard_check(vk_down)) * spd;

if (place_meeting(x + hspd, y, WALL) {
while (!place_meeting(x + sign(hspd), y, WALL)
x += sign(hspd);
hspd = 0;
}
x += hspd;

if (place_meeting(x, y + vspd, WALL) {
while (!place_meeting(x, y + sign(vspd), WALL)
y += sign(vspd);
vspd = 0;
}
y += vspd;``````

Last edited by a moderator: