GML - Collision Correction Alternative
| Object | Sprite | Properties |
|---|---|---|
| objPlayer | sprPlayer | Visible |
| objBlock | sprBlock | Visible, and MARK AS SOLID this time |
//controls for movement:
hkey = keyboard_check( vk_right ) - keyboard_check( vk_left );
//check if on ground:
if( place_free( x, y+1 )){
gravity = 0.8; //set gravity, as we are in the air
} else {
gravity = 0; //no need for gravity on ground
//since we are on ground at this point,
//we can check if we need to jump:
if( keyboard_check_pressed( vk_up )){
vspeed = -12;
}
}
//set horizontal movement based on controls:
if( hkey == 0 ){
hspeed *= 0.75; //friction
if( abs( hspeed ) < 0.5 ){ hspeed = 0; }
} else {
hspeed *= 0.75;
hspeed += 2 * sign( hkey );
}
//first check if we need to correct for collision:
if( speed != 0 and x == xprevious and y == yprevious ){
//move up first if vspeed is up:
if( vspeed < 0 ){
move_contact_solid( 90, -vspeed );
}
//move over if hspeed is not 0:
if( hspeed != 0 ){
//(setting some temporary variables we will use):
temp_y = y;
//move up:
move_contact_solid( 90, abs( hspeed ));
//move over:
move_contact_solid( 90 - sign( hspeed ) * 90, abs( hspeed ));
//move down (what we've moved up):
if(temp_y-y != 0){
move_contact_solid( 270, temp_y-y);
}
//move down again if it puts us on ground:
if( !place_free( x, y + abs( hspeed ) + 1 )){
move_contact_solid( 270, abs( hspeed ));
}
}
//move down if vspeed is down:
if( vspeed > 0 ){
move_contact_solid( 270, vspeed );
}
//if we are blocked then set vspeed to 0:
if( !place_free( x, y + sign( vspeed ))){
vspeed = 0;
}
//set hspeed to 0 if we can't move horizontally (we're blocked):
if( hspeed != 0 and x == xprevious ){
hspeed = 0;
}
}
// empty script to trigger collision event - important!
thoughts? the remaining tutorials will not use this alternative (FYI)


