Legacy GM Collision Detection Not Working

AiNaKa

Member
I'm trying to convert the collision detection that prevents the player from falling through the floor into code. Originally, I had it set as a collision event with the collision object that would set vspeed to 0, and move to contact with said object (to prevent this issue I got with the player bouncing when landing). I tried deleting that and putting code into the step event to execute it instead so I could do more with it. I've tried 2 different blocks of code:
Code:
if collision_rectangle(x+46,y+128,x+81,y+129,tile_collision,true,false)
{
    vspeed = 0;
    move_contact_solid(270,-1);
}
Code:
if place_meeting(x,y+1,tile_collision)
{
    vspeed = 0;
    move_contact_solid(270,-1);
}
Neither of these work, and the player just slowly sinks through the ground. I've also tried adding "x += 1", but that messes up my animations and prevents the player from jumping. Is their anything different I should be trying here?
 

TheouAegis

Member
If you had to use Move To Contact in the collision event, that means it moving you outside of the collision. Was your ground object marked as "solid"?
 

TheouAegis

Member
if place_meeting(x,y+1,tile_collision) { vspeed = 0; move_contact_solid(270,-1); }
Change the +1 to +vspeed+1.

One thing to keep in mind is that hspeed and vspeed do not update x and y until the END of the Step Event. So if you check for a collision at your current position in the Step Event, there won't be one and you'll move down into it and then suddenly you're too far down.

Collision Events with solid objects cause GM to force the objects back to their previous positions, which is why you needed to use Move Contact Solid, because GM automatically put your player back outside the platform.

Also, if you use move_contact_solid(), I think you should be using move_contact_solid(point_direction(0,0,hspeed,vspeed),-1) so that you continue moving the direction you were headed when you collided with the solid. But that's just me...
 

AiNaKa

Member
Change the +1 to +vspeed+1.

One thing to keep in mind is that hspeed and vspeed do not update x and y until the END of the Step Event. So if you check for a collision at your current position in the Step Event, there won't be one and you'll move down into it and then suddenly you're too far down.

Collision Events with solid objects cause GM to force the objects back to their previous positions, which is why you needed to use Move Contact Solid, because GM automatically put your player back outside the platform.

Also, if you use move_contact_solid(), I think you should be using move_contact_solid(point_direction(0,0,hspeed,vspeed),-1) so that you continue moving the direction you were headed when you collided with the solid. But that's just me...
The collision event actually worked just fine with "vspeed = 0" I only used move to contact because often when the player landed from a high height, they'd bounce a little bit, making quite a few frames where the player was unable to jump and stuck in the falling animation while on the ground. Adding move to contact solved that.

I changed "+1" to "+vspeed+1" and it did absolutely nothing. Yes, the collision object is solid. I used the debug menu I made to check the player's vspeed and when in contact with the ground it says his vspeed = 1. I don't know why that is, especially when I set it to 0 every step. I tried using "vspeed -= 1" instead of "y -= 1" (I said x -= 1 by accident). It solved the problem with sinking through the ground and screwed up animations, but the player can no longer jump.
The code for jumping is in a step event and it looks like this if it helps:
Code:
if keyboard_check_pressed(vk_up)
{
    if place_meeting(x,y+1,tile_collision)
    {
        if highjump = true
        {
            vspeed = -25
        }
        else
        {
            vspeed = -15
        }
    }
}

if keyboard_check_released(vk_up)
{
    if vspeed < 0
    {
        jumpstop = true;
    }
}

if jumpstop = true
{
    vspeed += 2;
}

if vspeed >= 0
{
    jumpstop = false;
}
 

TheouAegis

Member
That's where your vspeed=1 is coming from, then. And what's pulling you into the ground.

FYI the "bouncing" you experienced was from the player falling just a tad too fast and so the "solid" flag on the ground bumped him back up into the air a pixel or two off the ground, which gravity then pulled him down to again.

A lot of people disable gravity when place_meeting(x,y+1,obj_ground). If you don't want to do that, you could try setting vspeed to -gravity so it will counteract the gravity instead of 0.
 

AiNaKa

Member
That's where your vspeed=1 is coming from, then. And what's pulling you into the ground.

FYI the "bouncing" you experienced was from the player falling just a tad too fast and so the "solid" flag on the ground bumped him back up into the air a pixel or two off the ground, which gravity then pulled him down to again.

A lot of people disable gravity when place_meeting(x,y+1,obj_ground). If you don't want to do that, you could try setting vspeed to -gravity so it will counteract the gravity instead of 0.
Again, setting vspeed to -gravity fixed it, but now he can't jump at all.
 

AiNaKa

Member
Move your jump code to the very end of that block of code so that it's the LAST thing to set vspeed.
Thankyou so much! I feel like an absolute moron now. I totally forgot that the order of code execution matters. This was literally all it took and i've been stressing so hard for days trying to figure out what it was. This entirely fixed the problem! Thankyou!
 

AiNaKa

Member
Move your jump code to the very end of that block of code so that it's the LAST thing to set vspeed.
Actually, I kinda take that back, because another problem has arisen. I remember getting this bug actually a long time ago when I messed with gamemaker studio, but I didn't get anywhere with that project and abandoned it like over a year ago. Anyways, often times when the player is landing, He'll end up inside the ground. How can I make it so that the player always stays on top of the ground?
 

TheouAegis

Member
Are you using

if place_meeting(x,y+1,tile_collision) vspeed=0

Or are you using

if place_meeting(x,y+vspeed+1, tile_collision) vspeed=0
 

AiNaKa

Member
Are you using

if place_meeting(x,y+1,tile_collision) vspeed=0

Or are you using

if place_meeting(x,y+vspeed+1, tile_collision) vspeed=0
vspeed = 0 wasn't working, I was using vspeed = -gravity. But using y+vspeed+1 instead of y+1 worked, thanks! Now I can actually work on horizontal collisions properly
 
Top