• 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!

GML Collision bug

P

Puddim

Guest
I made a GML collision system that works perfectly with 1 collision box ( the floor ), but if i create a new collision box and then collide with it on the left/right (not top) it bugs, and i don't know how to fix it. (i've been using GameMaker since last week so i'm sorry if this code is messy.) (English is not my native language.) (Example: http://prntscr.com/cj9g3w )

Create Event:
Code:
xsd = image_xscale;
image_speed = 0.3;
idle = spr_idle;
run = spr_run;
sprite_index = idle;
yv = 0;
jv = 8;
ya = 0.4;
xv = 0;
xa = 0.5;
mx = 14;
jumped_tt = false;
jumping = false;
Step Event:
Code:
jumped_tt = false;
if (keyboard_check(vk_right)) {
    image_xscale = xsd;
    xv += xa;
};
if (keyboard_check(vk_left)) {
    image_xscale = -xsd;
    xv -= xa;
};
if (!keyboard_check(vk_right) && !keyboard_check(vk_left)) {
    if (xv > 0) {
        xv -= xa;
    } else {
        if (xv < 0) {
            xv += xa;
        };
    };
};
if (keyboard_check(vk_up) && jumping == false) {
    yv = -jv;
    jumping = true;
    jumped_tt = true;
};
if (xv > mx) {
    xv = mx;
};
if (xv < -mx) {
    xv = -mx;
};
if (place_meeting(x, y, obj_collide)) {
    if (jumped_tt == false) {
        yv = 0;
        jumping = false;
    };
} else {
    yv += ya;
};
x += xv;
y += yv;

a = 0;
d = false;
while (a < 100) {
    if (d == false) {
        y -= 0.1;
        if (place_meeting(x, y, obj_collide) == false) {
            y +=0.1;
            d = true;
            a = 100;
        };
    
    };
    a += 1;
};
if (xv > 0 || xv < 0) {
    image_speed = 0.4;
    sprite_index = run;
    audio_sound_pitch(obj_musicplayer.musicp, (1 + (abs(xv)/120)));
} else {
    image_speed = 0.3;
    sprite_index = idle;
    audio_sound_pitch(obj_musicplayer.musicp, 1);
};
 
T

TaylorBramble

Guest
Shaun Spalding has an excellent collision tutorial on his youtube, along with many other great beginner tutorials, just search his name.
I'll put down the basics of his system here though:


///CREATE
xsp = 0
ysp = 0

//STEP

//Basically, just check if you will be colliding at your current speed
if(place_meeting(x+xsp, y, obj_collide))
{
//////Then if you are going to collide, inch your way toward the collision as long as you aren't colliding.
while(!place_meeting(x+sign(xsp), y, obj_collision))
{
x += sign(xsp) //////////sign returns a value of 1 or -1 depending on direction of movement
}
//////Once you have gotten as close as possible, make sure you are no longer moving
xsp = 0
}

////////////Do the same for the y direction

if (place_meeting(x, y+ysp, obj_collide))
{
while(!place_meeting(x, y+sign(ysp), obj_collide))
{
y += sign(ysp)
}
ysp = 0

/////then move appropriately

x += xsp
y += ysp




This is the best way I've found to do basic movement
 
P

Puddim

Guest
Shaun Spalding has an excellent collision tutorial on his youtube, along with many other great beginner tutorials, just search his name.
I'll put down the basics of his system here though:


///CREATE
xsp = 0
ysp = 0

//STEP

//Basically, just check if you will be colliding at your current speed
if(place_meeting(x+xsp, y, obj_collide))
{
//////Then if you are going to collide, inch your way toward the collision as long as you aren't colliding.
while(!place_meeting(x+sign(xsp), y, obj_collision))
{
x += sign(xsp) //////////sign returns a value of 1 or -1 depending on direction of movement
}
//////Once you have gotten as close as possible, make sure you are no longer moving
xsp = 0
}

////////////Do the same for the y direction

if (place_meeting(x, y+ysp, obj_collide))
{
while(!place_meeting(x, y+sign(ysp), obj_collide))
{
y += sign(ysp)
}
ysp = 0

/////then move appropriately

x += xsp
y += ysp




This is the best way I've found to do basic movement
I tried to understand what this code does and found my problem. Now it's working fine! Thanks.
 
Top