• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Dynamic collision checking?

P

Pitu

Guest
Hello, how would I go on about making dynamic collision checking with lets say my player object, the video shows what i mean.
Also if i were to use for example a jumping sprite, that would extend his arms in mid air but that would cause him to be stuck way more than just that because the collision already happened, so how would i go on about fixing that? (i'm using place_meeting at the moment to collide the player with the walls, no physics)

Code for that player object (and only code im using so far in my entire game):
CREATE EVENT:
Code:
///Initialize Variables
grav = 1;
spd = 4;
jspd = 20;
hspd = 0;
vspd = 0;

//Stop animation
image_speed = 0;
STEP EVENT:
Code:
//Get the player's input
key_right = keyboard_check(ord('D'));
key_left = keyboard_check(ord('A'));
key_jump = keyboard_check_pressed(ord('W'));


//Check for collisios
if(place_meeting(x, y+2, obj_wall))
{
    vspd = 0;
   
    //Jumping
    if(key_jump){
       
        vspd = -jspd;
    }
} else{
    //Move down with gravity
    if(vspd < 10){
        vspd += grav;
    }
}
//Moving to the right
if(key_right){
 
    hspd = spd;
   
}
//Moving to the left
if(key_left){

    hspd = -spd;

}


//Check for not moving
if((!key_right && !key_left) || (key_right && key_left)){

    hspd = 0;
   
}
//Horizontal collisions ADD IN SCRIPT
if(place_meeting(x+hspd, y, obj_wall)){
    while(!place_meeting(x+sign(hspd), y, obj_wall)){
        x+= sign(hspd);
    }
    hspd = 0;
}
//Move horizontally
x += hspd;
   
//Verticle collisions ADD IN SCRIPT
if(place_meeting(x, y+vspd, obj_wall)){
    while(!place_meeting(x, y+sign(vspd), obj_wall)){
        y+= sign(vspd);
    }
    vspd = 0;
}
//Move vertically
y += vspd;
   
    //Sprite control
//Walking right
if(hspd > 0){
   
    image_speed = 0.2;
    sprite_index = spr_player_right;
}
//Walking left
else if(hspd < 0){
   
    image_speed = 0.2;
    sprite_index = spr_player_left;
}
/*Jumping
else if((vspd > 0) || (vspd < 0)){
    image_speed = 0.2;
    sprite_index = spr_player_jumping;
}*/
else{
   
    image_index = 0;
}

    //Audio control

//When walking right
if(hspd > 0){
    if !audio_is_playing(aud_walking)
    audio_play_sound(aud_walking, 1, true);
}
//When walking left
else if(hspd < 0){
    if !audio_is_playing(aud_walking)   
    audio_play_sound(aud_walking, 1, true);
}

else{
    audio_stop_sound(aud_walking);
    }

if(vspd > 0){
    audio_stop_sound(aud_walking);
}
PS. Sorry for bad comments in the code, not very efficient programmer. Thanks in advance
 
J

Jaqueta

Guest
Probably you haven't set a collision mask for your character. You can do that here.
 
P

Pitu

Guest
Hey thanks, but like won't that be the same for all sprites? I want the collision mask to change as the sprite changes, for example if im in mid air and there is an object (as shown in the video) how would i made it so that doesn't happen? like BEFORE the collision happens shouldn't the mask change? or do i have it all wrong?

EDIT: Thanks it worked, can you explain why it didn't work when it was set to 'same as sprite' because i mean its the same thing isn't it
 
Last edited by a moderator:
J

Jaqueta

Guest
Well, if you change the collision mask, and the mask gets inside a solid, it will most likely get the player stuck in there. So I recommend sticking with one collision mask only.
 
Top