Having issues with collision in platformer

A

assassin7

Guest
Hello,

I am having issues with this platformer I am working on.
Code is below but basically. I am having troubles with the player character getting stuck on the side of the platform or I get the issues as shown in the attached GIF image. I have been troubleshooting it for 3 days and havent made any lead way at all. I am still working on the sprites but using place holders for now.

But I really need help fixing this issues


/////////////////////////////////////////////
Create Event:
////////////////////////////////////////////
hsp = 0;

vsp = 0;

jump_spd = 50//25;


max_spd = 10

//movespd = 10 // ladder speed

lad_spd = 10;

lad_vsp = 0


accel=0.5;


grav = 1;

on_ground = true;

In_air = false;

//facing=1;

enum state{
normal,
ladder,
hurt,
ok

}

current_state= state.normal;

hp = 5;

//ok_time = 3

ok_timer = 3

////////////////////////////////////////////
Step Event:
/////////////////////////////////////////////
/// @description Insert description here
// You can write your code in this editor

//Getting players input
#region inputs
key_right = keyboard_check(vk_right);
key_left = keyboard_check(vk_left);
key_jump = keyboard_check_pressed(vk_space);
key_up_dir = keyboard_check_direct(vk_up);
key_down_dir = keyboard_check_direct(vk_down);
key_jump_rel = keyboard_check_released(vk_space);

key_up = -keyboard_check(vk_up);
key_down = keyboard_check(vk_down);
#endregion




//checking if player is own ladder

if place_meeting(x,y,o_ladder)
{
if (key_up_dir || key_down_dir)
{
current_state = state1.ladder1;
}
}

//Checking states when player transitions between sprites and perform different actions
#region State machine
switch(current_state)
{
case state1.normal1:

image_speed = 1;


// Check if player is On Ground or In Air
if vsp == 0
{
on_ground = true;
In_air = false
}
else
{
on_ground = false;
In_air = true
}

////////////////////////////////
Getting players movements horizontally
/////////////////////////////////
#region Movement
if (hsp != 0)
{
image_xscale = sign(hsp);
}

#region Horizontal Movements
//changing left or right
if (key_right && key_left)
{
hsp = 0;
}


if(key_right or key_left)
{
hsp += (key_right - key_left) * accel
hsp = clamp(hsp, -max_spd, max_spd)
}
else
{
scr_friction(accel);
}

scr_collision(o_solid);
//scr_collision(o_oneway_plat1)
#endregion


/////////////////////////////////////
Players vertical movements
//////////////////////////////////////
#region Jumping
if !place_meeting(x,y + 1,o_solid)
{
vsp += grav;

if key_jump_rel && vsp < -20
{
vsp = -15;
}
}
else
{
vsp = 0

if (key_jump)
{
vsp = -jump_spd; // applies the jump speed
}


}

#endregion

#endregion

#region Animation normal state
//animations changes sprites for player


if (on_ground)
{
In_air = false;
if(hsp == 0)
{
sprite_index = spr_p
}

if (hsp !=0)
{
sprite_index = spr_p;

}
}
else
{
In_air = true;
}
//jumping and landing animation
if (In_air)
{
on_ground = false;
if (vsp < 0)
{
sprite_index= spr_p;
}

if (vsp > 0)
{
sprite_index= spr_p;
}
}
else
{
on_ground = true;
}

#endregion

break;

#region Ladder State
case state1.ladder1:


sprite_index = spr_p;

#region ladder movement

#region Vertical Movements
//changing left or right

var v_move = key_down + key_up;
vsp = lad_spd * v_move;

//grav = 0
if v_move == 0
{
image_speed = 0;
}

if v_move != 0
{
image_speed = 1;
}

y+=vsp
//scr_collision(o_ladder);




#endregion


#endregion


#region cancel climb
if key_jump
{
current_state= state1.normal1;
}

if !place_meeting(x,y,o_ladder)
{
current_state= state1.normal1;
}
#endregion


//////////////////////////////////////////////////////////////
Collision Script:(scr_collision)
/////////////////////////////////////////////////////////////
#region collision

var collison_obj = argument0
//vsp += grav;
// Horizontal
if place_meeting(x+hsp,y,collison_obj)
{
while (!place_meeting(x+round(sign(hsp)),y,collison_obj))
{
x += sign(hsp);
}

hsp = 0;
}

x+=hsp

//vertical
if place_meeting(x,y+vsp,collison_obj)
{
while (!place_meeting(x,y+sign(vsp),collison_obj))
{
y += sign(vsp)
}

vsp = 0;
}

y+=vsp
#endregion


//////////////////////////////////////
friction script : scr_friction
////////////////////////////////////

var amount = argument0;


if(hsp != 0)
{
if(abs(hsp) - amount > 0)
{
hsp -= amount * image_xscale;

}
else
{
hsp = 0;
}
}
 

Attachments

Not sure if this is the issue, but if the player can move while floating off the ledge it might be a collision mask issue. If this isn't it another member will have to help you.
 

Yal

šŸ§ *penguin noises*
GMC Elder
Your post is not particularly readable, could you please re-post the code within
Code:
[code]
tags?
 
A

assassin7

Guest
ok I just re-posted the code
Code:
/////////////////////////////////////////////
//Create Event:
////////////////////////////////////////////
hsp = 0;

vsp = 0;

jump_spd = 50//25;


max_spd = 10

//movespd = 10 // ladder speed

lad_spd = 10;

lad_vsp = 0


accel=0.5;


grav = 1;

on_ground = true;

In_air = false;

//facing=1;

enum state{
normal,
ladder,
hurt,
ok

}

current_state= state.normal;

hp = 5;

//ok_time = 3

ok_timer = 3
Code:
////////////////////////////////////////////
//Step Event:
/////////////////////////////////////////////
/// @description Insert description here
// You can write your code in this editor

//Getting players input
#region inputs
key_right = keyboard_check(vk_right);
key_left = keyboard_check(vk_left);
key_jump = keyboard_check_pressed(vk_space);
key_up_dir = keyboard_check_direct(vk_up);
key_down_dir = keyboard_check_direct(vk_down);
key_jump_rel = keyboard_check_released(vk_space);

key_up = -keyboard_check(vk_up);
key_down = keyboard_check(vk_down);
#endregion




//checking if player is own ladder

if place_meeting(x,y,o_ladder)
{
if (key_up_dir || key_down_dir)
{
current_state = state1.ladder1;
}
}

//Checking states when player transitions between sprites and perform different actions
#region State machine
switch(current_state)
{
case state1.normal1:

image_speed = 1;


// Check if player is On Ground or In Air
if vsp == 0
{
on_ground = true;
In_air = false
}
else
{
on_ground = false;
In_air = true
}
Code:
////////////////////////////////
//Getting players movements horizontally
/////////////////////////////////
#region Movement
if (hsp != 0)
{
image_xscale = sign(hsp);
}

#region Horizontal Movements
//changing left or right
if (key_right && key_left)
{
hsp = 0;
}


if(key_right or key_left)
{
hsp += (key_right - key_left) * accel
hsp = clamp(hsp, -max_spd, max_spd)
}
else
{
scr_friction(accel);
}

scr_collision(o_solid);
//scr_collision(o_oneway_plat1)
#endregion
Code:
/////////////////////////////////////
//Players vertical movements
//////////////////////////////////////
#region Jumping
if !place_meeting(x,y + 1,o_solid)
{
vsp += grav;

if key_jump_rel && vsp < -20
{
vsp = -15;
}
}
else
{
vsp = 0

if (key_jump)
{
vsp = -jump_spd; // applies the jump speed
}


}

#endregion

#endregion

#region Animation normal state
//animations changes sprites for player


if (on_ground)
{
In_air = false;
if(hsp == 0)
{
sprite_index = spr_p
}

if (hsp !=0)
{
sprite_index = spr_p;

}
}
else
{
In_air = true;
}
//jumping and landing animation
if (In_air)
{
on_ground = false;
if (vsp < 0)
{
sprite_index= spr_p;
}

if (vsp > 0)
{
sprite_index= spr_p;
}
}
else
{
on_ground = true;
}

#endregion

break;

#region Ladder State
case state1.ladder1:


sprite_index = spr_p;

#region ladder movement

#region Vertical Movements
//changing left or right

var v_move = key_down + key_up;
vsp = lad_spd * v_move;

//grav = 0
if v_move == 0
{
image_speed = 0;
}

if v_move != 0
{
image_speed = 1;
}

y+=vsp
//scr_collision(o_ladder);




#endregion


#endregion


#region cancel climb
if key_jump
{
current_state= state1.normal1;
}

if !place_meeting(x,y,o_ladder)
{
current_state= state1.normal1;
}
#endregion
Code:
//////////////////////////////////////////////////////////////
//Collision Script:(scr_collision)
/////////////////////////////////////////////////////////////
#region collision

var collison_obj = argument0
//vsp += grav;
// Horizontal
if place_meeting(x+hsp,y,collison_obj)
{
while (!place_meeting(x+round(sign(hsp)),y,collison_obj))
{
x += sign(hsp);
}

hsp = 0;
}

x+=hsp

//vertical
if place_meeting(x,y+vsp,collison_obj)
{
while (!place_meeting(x,y+sign(vsp),collison_obj))
{
y += sign(vsp)
}

vsp = 0;
}

y+=vsp
#endregion
Code:
//////////////////////////////////////
//friction script : scr_friction
////////////////////////////////////

var amount = argument0;


if(hsp != 0)
{
if(abs(hsp) - amount > 0)
{
hsp -= amount * image_xscale;

}
else
{
hsp = 0;
}
}
 
Last edited by a moderator:
A

assassin7

Guest
Not sure if this is the issue, but if the player can move while floating off the ledge it might be a collision mask issue. If this isn't it another member will have to help you.
Yes thats what I thought was the issues I changed the numbers on the mask from auto to Manuel and tried to play around with the mask numbers making Left:115 Top:0 right: 295 bottom: 458

and using other random numbers to figured this out but nothing else worked I think whats happen is it maybe something I am not checking for probably
 

Catastrophe

Member
Well, honestly if you're making a platformer, it's best to use one of 19746 platformer engines.

Edit nvm, still looking

Hmm, it's weird that you're using o_solid in some areas but collision_obj in others.

As for getting stuck in platforms, I believe most platformer engines avoid using solids for this sort of reason. If you aren't using solids, you can manually push yourself up at the end if you somehow get stuck inside the floor at the end of step, but it acts screwy if you get stuck in a solid otherwise



Btw, to debug this, I would draw some text over the player's head so you get a constant idea of what's going on like


draw_text(x,y-10,vsp)
draw_text(x,y-50,currentStateString)

and you could set flags in code that you can draw at the end like

draw_text(x,y-90,collidedWithSolid ? "hit solid" : "did not hit solid")
 
Last edited:

Elkrom

Member
I had a couple similar problems, when i started making the platformer I'm working on, and it always seemed to be a collision mask issue. i just made sure the mask was the same on all the different player sprites i had and everythings been working fine since. Like when i made my jump frame sprite the legs were extended to more of a split, so in my mind i thought it would make sense to make the mask wider to contain the whole sprite within the mask. But that just caused problems like when you're next to a wall then you press jump and now the mask is clipping because the mask is wider and extends into the wall if that makes sense. So if you have say an idle, walking, running, jumping, sliding sprite etc.. Make sure you keep the mask consistent on all of them and not just the size the placement as well. sorry if thats not the issue but it seems a lot of people i see on here with collision issues most of the time its just a simple mask issue.
 
Does jumping work ok? Or does it bug out just when the player falls of a edge?
If jumping works the issue is most likely in the hsp movement.
 
A

assassin7

Guest
Does jumping work ok? Or does it bug out just when the player falls of a edge?
If jumping works the issue is most likely in the hsp movement.
Yes jumping works just fine only collision when getting on the edge of the platform
 
A

assassin7

Guest
Well, honestly if you're making a platformer, it's best to use one of 19746 platformer engines.

Edit nvm, still looking

Hmm, it's weird that you're using o_solid in some areas but collision_obj in others.

As for getting stuck in platforms, I believe most platformer engines avoid using solids for this sort of reason. If you aren't using solids, you can manually push yourself up at the end if you somehow get stuck inside the floor at the end of step, but it acts screwy if you get stuck in a solid otherwise



Btw, to debug this, I would draw some text over the player's head so you get a constant idea of what's going on like


draw_text(x,y-10,vsp)
draw_text(x,y-50,currentStateString)

and you could set flags in code that you can draw at the end like

draw_text(x,y-90,collidedWithSolid ? "hit solid" : "did not hit solid")

the collision obj is a scripts that is ran called scr_collision which requires the object in which the player will be colliding with to be enter which is scr_collision(o_solid).

Let me try the debug method you were talking about about
 
A

assassin7

Guest
I had a couple similar problems, when i started making the platformer I'm working on, and it always seemed to be a collision mask issue. i just made sure the mask was the same on all the different player sprites i had and everythings been working fine since. Like when i made my jump frame sprite the legs were extended to more of a split, so in my mind i thought it would make sense to make the mask wider to contain the whole sprite within the mask. But that just caused problems like when you're next to a wall then you press jump and now the mask is clipping because the mask is wider and extends into the wall if that makes sense. So if you have say an idle, walking, running, jumping, sliding sprite etc.. Make sure you keep the mask consistent on all of them and not just the size the placement as well. sorry if thats not the issue but it seems a lot of people i see on here with collision issues most of the time its just a simple mask issue.
Thanks for the input, yes all the masks are set the same on all sprites. I do believe that this may be a mask issue. Because after looking at the GIF again I see the player head has not cleared the platform yet. Then Once the head clears the platform then player falls. Now the question is how to fix that issue?
 
Top