Problem with collisions

Zaksley

Member
Hi guys ! I'm pretty new in GML so I'm pretty sure that my bug is easy to fix but hey, i'm a noob ! :)

So, I looked a youtube tutorial for the movment of my character :

Script PlayerMovement()
Code:
        //MOVEMENT PLAYER

//Calculate Movement 
var move = checkKeyboard_Right - checkKeyboard_Left; 
hsp = move * walkspeed;
vsp += grv; 


//Jump from floor 
if ( (place_meeting(x, y+1, obj_wall)) && (checkKeyboard_Jump) )
{
    vsp = -8;
}

//Double Jump
if  (!place_meeting(x, y+1, obj_wall) && double_jump) && checkKeyboard_Jump
{
    vsp = - 8;
    double_jump = false;
}

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


//Horizontal Collision 
if place_meeting(x, y + vsp, obj_wall)
{
    
    while (!place_meeting(x, y + sign(vsp), obj_wall))
    {
        y += sign(vsp)    
    }
    
    if !place_meeting(x, y+vsp, obj_ceiling) 
    {
        //Reset Double Jump
        if !double_jump   double_jump = true;
    }

    vsp = 0;    
}

//Movement 
x += hsp;
y += vsp;
Alright so with this code, there is 2 major problems :
* I can be block if my character hits a corner. It that case, my character don't respond to any of my command and he's stuck in the wall. I think it's because he tries to do both collision vertical and horizontal. Not sure about that but I think thats the problem.

* Sometimes (not a lot of time), I have a very weird bug. Let's imagine i'm just under a wall. If I jump and I hit perfectly the pixel of the corner of the wall above me, i'm going to teleport instantly my character backdown. It's like, I jump and just after hiting the corner, I'm exactly where I was before the jump. Very weird and I just saw an ennemy (because I use the same script for collisions for my ennemies) doing it but in an horizontal way : He hit the corner from the right and got teleported to the left (from a wall).


Alright, if you have any idea how to help me, it could save my game ! :D
Sorry for the english, i'm learning it too ;)

See ya,
Zaksley.
 

samspade

Member
Like the other post, I don't actually see any issues. I might be missing something. Is this in 2.3? Was it a converted project? I assume obj_ceiling is a child of obj_wall?

I would start by reducing the code to something slightly simpler. So for example, start by removing the double jump code. Does the problem still occur? Then remove the jump code, does the problem still occur? If you can repeat the problem with nothing more than movement code, then at lease you've isolated what's going on.

As with the other post, I'd recommend going through this in the debugger.
 

Tony Brice

Member
I'm betting this is happening on diagonal movements, like jumping and moving right/left at the same time. Where you have code for the movement to move up to next to any object you would have collided with on the next frame (the while loop part), I think something might be glitching there. I also think the double jump is a problem as you seem to be use the line "vsp = - 8" again instead of possibly meaning to use "vsp -= 8" to increase the jump height, but I could be thinking that all wrong, as I'm not trying it in GMS itself.

One more thing I noticed, which I'd suggest maybe trying first is updating the vsp and hsp lines straight after their collision checks rather than doing both of them after both the collisions would have already taken place and been acted on. I seem to recall that's what the original tutorial this is based on did. That way you're doing a collision check horizontal and then updating the x position, and then doing a check vertical before you update the y position.
 
Is this code from the Shaun Spalding Tutorial? If so, he addressed this issue on another video of his. Just reorder the last two lines to fit right below the horizontal and vertical collision functions. It should read something like this:

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

//Horizontal Collision 
if place_meeting(x, y + vsp, obj_wall)
{
    
    while (!place_meeting(x, y + sign(vsp), obj_wall))
    {
        y += sign(vsp)    
    }
    
    if !place_meeting(x, y+vsp, obj_ceiling) 
    {
        //Reset Double Jump
        if !double_jump   double_jump = true;
    }

    vsp = 0;    
}
x += hsp;
 

Zaksley

Member
Hi,
I tried your solution MaxCampbell
New problem : My character is falling in the ground, no matter what I do xD
He's going slowly in the ground, so yeahh, i'm not stuck but that's not good xD
May you link me the fixed bug by Shaun Spalding? The tutorial is from him yeahh! :D
 

Zaksley

Member
Like the other post, I don't actually see any issues. I might be missing something. Is this in 2.3? Was it a converted project? I assume obj_ceiling is a child of obj_wall?

I would start by reducing the code to something slightly simpler. So for example, start by removing the double jump code. Does the problem still occur? Then remove the jump code, does the problem still occur? If you can repeat the problem with nothing more than movement code, then at lease you've isolated what's going on.

As with the other post, I'd recommend going through this in the debugger.
Yeahh, the problem is still there even without the double jump.
It is 2.2.3, pretty sure it won't change anything if I download the last last last version but I'm going to do it someday ;)
I don't really know how works the debugger :'( :'( :'( :'(
 
Hi,
I tried your solution MaxCampbell
New problem : My character is falling in the ground, no matter what I do xD
He's going slowly in the ground, so yeahh, i'm not stuck but that's not good xD
May you link me the fixed bug by Shaun Spalding? The tutorial is from him yeahh! :D
Here you go, I believe he mentions it around 20 seconds in.

 

Zaksley

Member
Alright guys !
It might be working but I still need help from you !

So, I did what Max proposed (in the correct order), I mean, it has more sens to add x movement after the vertical collision and then y after the horizontal collision than the opposite.
So, it fixed the problem of falling in the ground and I got a little bit less stuck. But there is still the bug on getting on walls.

So, I tried to remove this little thing that I didn't think it was important :

{code]
// ANIMATION PLAYER

//Left or Right
if hsp != 0
{
image_xscale = size * sign(hsp);

sprite_index = spr_run;
image_speed = 1;
}
[/code]

It was just just under the script's code that you guys see. And since I removed this, it's working perfectly fine!?
So question, when I'm turning, is this why my character is getting stuck? It might be the answer !
But here we are, new question, how to fix this? :D
I want my character to run where the player wants him to run hahaha !
 
Top