GMS 2 falling object problem (lemmings-esque), using ds_grid values

L

lohtangclan

Guest
Hi all...
I'm trying to make a lemmings-y object that walks left and right & falls down whenever there's a gap in my ds_grid. eg. a 0..
At the moment I spawn the object in by clicking a button. It falls correctly (ie at the speed I want) when I spawn it, but after it has hit some ground and then walks off a second edge, it immediately teleports (for lack of a better word) to the closest y=1 ground, instead of falling according to my set speed. I've enclosed the step event of my lemming object..
I've also noticed it doesn't *seem* to enter the if(on_ground==0) , as it never changes sprite to s_falling on the second fall. I've tested out putting show_message("falling"); into the if statement and it also only ever displays when the object is spawned & falling... But yet..it falls?!

So.... where am I going wrong..and how do I make sure the object enters falling state correctly?
Thanks..
Code:
var calc_v_move=o_control.time_passed*grav; //delta time x gravity

on_ground=is_on_ground(x,y); //simple external script that checks grid value of y+1 for 0 or 1
if(on_ground==0){
    sprite_index=s_falling;
    var i=1;
//the calc_v_move is how much the lemming should fall in 1 frame, so whilst it is free to fall, move it
    while(i<=calc_v_move&&on_ground==0){
        i++;
        y++;
        
        on_ground=is_on_ground(x,y);   
    }

}
var side_hit=0;//side collision flag
var endLoop=false;

if(on_ground==1){//on ground obvs
    sprite_index=s_dog;
    if(x>room_width-5){
        travel_dir=-1;
        }
        
    if(x<5){
        travel_dir=1;
        }
    if(travel_dir==1){
        
        var i=bbox_top;
        
        while(endLoop==false){
            
            if(global.screenGrid[# x+1,i]==1){//check to the side of object for collision
                side_hit=1;
                if(y-i<slope_height){
                    y=i-1;
                    
                
                    side_hit=0;//reset detector
                    endLoop=true;
                }
                else{//not less than slope means too steep, turn around
                travel_dir=-1;
                side_hit=1;
                endLoop=true;
                }
            }
            else i++;
        
        }
        //side is clear
        if(side_hit!=1){
        
        x+=1;
        }
    }
    
    if(travel_dir==-1){
        var i=bbox_top;
        while(endLoop=false){
            if(global.screenGrid[# x-1,i]==1){//check to the side of object for collision
                
                if(y-i<slope_height){
                    y=i-1;
                    side_hit=0;//reset detector
                    endLoop=1;
                }
                else{
                travel_dir=1;
                side_hit=1;
                endLoop=1;
                break;//too steep
                }
            }
            else i++;
        }
        //side is clear
        if(side_hit!=1){
            
            x+=-1;
        }
    
    
    }
    
}
 
might it be that you never reset the fall speed when it hit the ground?
I mean that it keeps increasing when it walks and then it's so high when it walks of the next cliff it instantly fall down as you said?
 
L

lohtangclan

Guest
Hey Carl, thanks for replying.. so my grav is just set to 100 or so, and the o_control.time_passed is just delta_time/1000000, so that my movement is linked to time and not frames (I know, I haven't done this with the horizontal movement..yet).
My understanding is that, the vertical amount of movement should be recalculated each step...so shouldn't be increasing? Right?
 
L

lohtangclan

Guest
OK! I think I solved it.. or at least have made a sticky-plaster band-aid fix..
At the end of each (travel_dir) I check to see if there's anything diagonally below the object (eg, x+1,y+1) and if it's 0, then i set on_ground to 0.
I also remove the on_ground check at the start, and added one in my Create event.

Would be interesting to hear why my existing code doesn't work, as I still don't quite get why the falling code isn't triggered properly as-is..!

UPDATE - only half fixed it... the same problem of the objects teleporting down still exists sometimes :(
 
Last edited by a moderator:
Top