Legacy GM Platforming Collision

H

HalfSquat

Guest
I have some code in a step event for my character for colliding with some platforms. The vertical collision seems to work okay, although the character seems to land at random elevations on the platform. I can't seem to get the horizontal collision to work for when the character walks straight into a platform. Also, the character will also float in midair if even the top of him is overlapping with a platform. I can't seem to find a way to check for literal pixel-perfect collision from every direction, is that somehow possible?

Code:
//collision with platforms

//vertical collision
if place_meeting(x,y,obj_ground) && hp > 0 || place_meeting(x,y + vspeed,obj_platform) && hp > 0
        {
            vspeed = 0
            sprite_index = spr_idle
            if keyboard_check_pressed(vk_space)          //press space to jump (works fine)
            {
                vspeed = -jspeed;
                sprite_index = spr_jump
            }
            }else{
            if (vspeed < grav) && !place_meeting(x,y, obj_ground) || (vspeed < grav) && !place_meeting(x,y, obj_platform){
            vspeed += grav
            sprite_index = spr_jump
            }else if (vspeed < grav) && place_meeting(x,y, obj_ground) || (vspeed < grav) && place_meeting(x,y, obj_platform)
            {
               vspeed = 0
            }
        }
       
//horizontal collision with platforms (doesn't work)
if place_meeting(x + hspeed,y,obj_platform) && hp > 0
        {
            hspeed = 0
        }
Capture1.PNG Capture2.PNG
 
J

JopRillos2001

Guest
For more control over your speed, change vspeed and hspeed to vsp and hsp. Change this in the create event

Code:
vsp = 0;
hsp = 0;
In the step event you could use this code for collsions. Should be pixel perfect.

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

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

HalfSquat

Guest
I did what you said, and went and replaced vspeed and hspeed with vsp and hsp. I also set tthe variables in the create event. However, I'm still getting the same issue of overlapping objects, and now the character doesn't use the correct sprites for certain positions anymore.

Here's the code I have for collisions, along with the controls for movement:

Code:
[
          //Horizontal Collision
            if (place_meeting(x+hsp,y,obj_ground)) {
                while(!place_meeting(x+sign(hsp),y,obj_ground))
                {
                    x += sign(hsp);
                }
                hsp = 0;
            }
            x += hsp;
        
            //Vertical Collision
            if (place_meeting(x,y+vsp,obj_ground)) {
                while(!place_meeting(x,y+sign(vsp),obj_ground))
                {
                    y += sign(vsp);
                }
                vsp = 0;
            }
            y += vsp;
    
        //jumping
        if place_meeting(x,y+vsp,obj_platform) && hp > 0
        {
            vsp = 0
            sprite_index = spr_idle
            if keyboard_check_pressed(vk_space)
            {
                vsp = -jspeed;
                sprite_index = spr_jump
            }
            }else{
            if (vsp < grav) && !place_meeting(x,y, obj_leftspike){
            vsp += grav
            sprite_index = spr_jump
            }else if (vsp < grav) && place_meeting(x,y, obj_leftspike)
            {
               vsp = 0
            }
        }
    
       //moving left and right
        if keyboard_check_direct(ord("A"))
        {
            hsp = -7.5
            image_xscale = -1
            if vsp < 0
            {
                sprite_index = spr_jump
            }else if vsp = 0
            {
                sprite_index = spr_running
            }
        }else if keyboard_check_direct(ord("D"))
        {
            hsp = 7.5
            image_xscale = 1
            if vsp < 0
            {
                sprite_index = spr_jump
            }else if vsp = 0
            {
                sprite_index = spr_running
            }
        }else{
            hsp = 0
            //sprite_index = spr_idle
        }
        }
Capture1.PNG Capture2.PNG
 
Top