GML Having problems with jumpthru platforms when colliding with 2 of them

Marossi

Member
So, basically, the following interaction is happening in my platformer metroidvania like game:




The green tiles have invisible square objects placed over used to only handle collision (oGround), and I'm having a lot of trouble to set these objects as jumpthru and not have a problem when I get to the situation as above.

Here's the code for the Player object for the jumpthru collision in the Step event
GML:
//Vertical Collision
  
  
    var vcollide;
    vcollide = instance_place(x, y+vsp,oGround);
    if (vcollide != noone)
    {
        if (sign(vsp) == 1)
        {
          
            if (!place_meeting(x,y, vcollide))
            {
                while (!place_meeting(x,y+sign(vsp),vcollide)) y += 1;
                vsp = 0
              
            }
        }
    }
I understand that when the Player object is colliding with two instances of the same object, he doesn't know what to do? I really don't know how to fix this, anyone have any clues or can just help me?
 

Marossi

Member
Wow, I'm actually surprised how collision_line worked perfectly for my need! I wonder why all the video tutorials don't use it. Granted I "redid" the collision check with it but it ended up being way simpler and easier to understand

GML:
    if (collision_line(bbox_left,bbox_bottom+vsp,bbox_right,bbox_bottom+vsp, oGround,false,false))
    {
        if (sign(vsp) == 1)
        {
            while (!collision_line(bbox_left,bbox_bottom,bbox_right,bbox_bottom,oGround,false,false)) y += 1
            vsp = 0
               
        }
    }
Thank you so much!!

Now I`m just wondering how to implement key_down in this code if the player wants to drop down, by the way the above is set up, the player will only drop if he clears totally from the tile, and not just the line.
 
Last edited:

Marossi

Member
Wow, I'm actually surprised how collision_line worked perfectly for my need! I wonder why all the video tutorials don't use it. Granted I "redid" the collision check with it but it ended up being way simpler and easier to understand

GML:
    if (collision_line(bbox_left,bbox_bottom+vsp,bbox_right,bbox_bottom+vsp, oGround,false,false))
    {
        if (sign(vsp) == 1)
        {
            while (!collision_line(bbox_left,bbox_bottom,bbox_right,bbox_bottom,oGround,false,false)) y += 1
            vsp = 0
              
        }
    }
Thank you so much!!

Now I`m just wondering how to implement key_down in this code if the player wants to drop down, by the way the above is set up, the player will only drop if he clears totally from the tile, and not just the line.
I did a workaround that work wonders for me

Used this code for collision from another forum that I've found and mixed with the key_down (keyboard_check_pressed)

GML:
//Collision with Platform
        //Checks for collision from player's feet through where player's feet will be next frame
        if (vsp >0 && collision_rectangle(bbox_left,bbox_bottom+1,bbox_right,bbox_bottom+1+vsp,oGround,false,true)) && (!key_down)
        {
            //while loop: If there is no platform 1 pixel underneath, player will move down 1 pixel
            while(!collision_line(bbox_left,bbox_bottom+sign(vsp),bbox_right,bbox_bottom+sign(vsp),oGround,false,true))
            {
                y += sign(vsp);
            }
            vsp = 0;
        }

        //IMPORTANT: Platform mask must be 1 pixel in height for this method to work
Set my oGround collision mask to be 1 pixel in height and later on when calculatin y position, since my gravity is a 0.3 float, I used the ceil() to round the number so my Player Object doesn't move in floaters

Just for record, thanks for the help!
 
I wonder why all the video tutorials don't use it.
There are many, many things in a lot of video tutorials that are sub-optimal. Usually what's being used in tutorials is the thing that is easiest to explain to beginners, or code that the tutorial maker is accustomed to and hasn't thought to challenge yet. Very rarely is the stuff taught in tutorials going to be the best way to approach a problem (let alone the fact that each project has individual needs and will do best with custom solutions tailored to those needs).
 
Top