Legacy GM [SOLVED] How to check if a colision is horizontal or vertical

RyanC

Member
Hi All,

Does anyone know a reliable way of checking if a collision is horizontal or vertical.

I'm trying to make a system where my player can only tackle enemies from above or below.

This code only seems to work from the right! (sprite width is 64 with centered origin)

Code:
if x < other.x
{
    var x_pbox = xprevious+32;
   
    if x_pbox < other.bbox_left
    image_index = 1 // get hit
}
else
{
    var x_pbox = xprevious-32;
   
    if x_pbox > other.bbox_right
    image_index = 1 // get hit
}
 
Last edited:

Dupletor

Member
"else" is too dangerous, many situations can occur when the objects have different forms and sizes.
Don't check if collision comes from one side or another. Check if it comes from one side. Then check if it comes from the other side.
Considering it comes from either sides is a free call for bugs.
 

RyanC

Member
"else" is too dangerous, many situations can occur when the objects have different forms and sizes.
Don't check if collision comes from one side or another. Check if it comes from one side. Then check if it comes from the other side.
Considering it comes from either sides is a free call for bugs.
Thanks for the advice, the left side is still not working though.

Code:
if x < other.x // this side is still not working
{
    var x_pbox = xprevious+32;
 
    if x_pbox < other.bbox_left
    image_index = 1 // get hit
}

if x >= other.x
{
    var x_pbox = xprevious-32;
 
    if x_pbox > other.bbox_right
    image_index = 1 // get hit
}
not working becuase +32 would jump forward more than the speed it moved to get there and so would be more than other.bbox_left.
 
Last edited:
G

GrAmmErX

Guest
ugh thats a bad collision code i use this:

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

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

RyanC

Member
ugh thats a bad collision code i use this:

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

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

Thanks for the reply!

A while loop using place_meeting is way too heavy in my opinion. My game is for Android medium spec+.

I also don't know how that will distinguish horizontal from vertical collisions, as they could both be possible at the same time.
 

RyanC

Member
Eventually solved it with this:

Code:
y = yprevious

if bbox_bottom < other.bbox_top or bbox_top > other.bbox_bottom
image_index = 0 // collision was vertical
else
image_index = 1 // collision was horizontal
 
W

Wraithious

Guest
"else" is too dangerous, many situations can occur when the objects have different forms and sizes.
Don't check if collision comes from one side or another. Check if it comes from one side. Then check if it comes from the other side.
Considering it comes from either sides is a free call for bugs.
yes else without if is dangerous, but else with if is a viable alternative, in fact in java anyway you cannot use else without if, it won't even compile, (I don't know if other languages are like that too) so I have learned in gms to either use else with if, or not even use else at all.
 
Top