• 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 [SOLVED]collisoon problems

A

azuarf

Guest
I added a collision event for player with wall.
and here is my code for player movement in a step event. its pretty basic.
Code:
if(global.pl_mov_state = 1)
{
if(keyboard_check(ord("W")))
    {
    y -= global.pl_speed;
    }
if(keyboard_check(ord("A")))
    {
    x -= global.pl_speed;
    }
if(keyboard_check(ord("S")))
    {
    y += global.pl_speed;
    }
if(keyboard_check(ord("D")))
    {
    x += global.pl_speed;
    }
}
when player object is colliding with the wall and i am pressing two buttons the player dose not move.
eg... when i press "W + A" the player object moves diagonally Up + Left, but when it collides with the wall it comes to a complete halt. how to make it so that the object dose not come to a complete halt when pressing two buttons(moving diagonally) when in collision with the wall and moves perpendicular.
my collision event is just a collision event with a comment inside. both objects are solid.

it just dose not look immersive.
 

jo-thijs

Member
I added a collision event for player with wall.
and here is my code for player movement in a step event. its pretty basic.
Code:
if(global.pl_mov_state = 1)
{
if(keyboard_check(ord("W")))
    {
    y -= global.pl_speed;
    }
if(keyboard_check(ord("A")))
    {
    x -= global.pl_speed;
    }
if(keyboard_check(ord("S")))
    {
    y += global.pl_speed;
    }
if(keyboard_check(ord("D")))
    {
    x += global.pl_speed;
    }
}
when player object is colliding with the wall and i am pressing two buttons the player dose not move.
eg... when i press "W + A" the player object moves diagonally Up + Left, but when it collides with the wall it comes to a complete halt. how to make it so that the object dose not come to a complete halt when pressing two buttons(moving diagonally) when in collision with the wall and moves perpendicular.
my collision event is just a collision event with a comment inside. both objects are solid.

it just dose not look immersive.
This issue is inherent to using solids.

The best solution is to not use solids at all and implement your own collision system in the step or end step event of the player object.
The simplest way to do so is by replacing your code with:
Code:
if global.pl_move_state == 1 {
    var dx = (keyboard_check(ord("D")) - keyboard_check(ord("A"))) * global.pl_speed;
    var dy = (keyboard_check(ord("S")) - keyboard_check(ord("W"))) * global.pl_speed;
    if !place_meeting(x + dx, y, obj_wall)
        x += dx;
    if !place_meeting(x, y + dy, obj_wall)
        y += dy;
}
 
A

azuarf

Guest
This issue is inherent to using solids.

The best solution is to not use solids at all and implement your own collision system in the step or end step event of the player object.
The simplest way to do so is by replacing your code with:
Code:
if global.pl_move_state == 1 {
    var dx = (keyboard_check(ord("D")) - keyboard_check(ord("A"))) * global.pl_speed;
    var dy = (keyboard_check(ord("S")) - keyboard_check(ord("W"))) * global.pl_speed;
    if !place_meeting(x + dx, y, obj_wall)
        x += dx;
    if !place_meeting(x, y + dy, obj_wall)
        y += dy;
}
your code is way shorter. i only know basic stuff.
i came up with
Code:
/*n = global.room_no;
if(global.pl_mov_state = 1)
{
if(keyboard_check(ord("W")))
    {
    y -= global.pl_speed;
    if(y <= global.room_bdy1[n] + 32)
        {
        y = global.room_bdy1[n] +32;
        }
    }
if(keyboard_check(ord("A")))
    {
    x -= global.pl_speed;
    if(x <= global.room_bdx1[n] +32)
        {
        x = global.room_bdx1[n] + 32;
        }
    }
if(keyboard_check(ord("S")))
    {
    y += global.pl_speed;
    if(y >= global.room_bdy2[n] -32)
        {
        y = global.room_bdy2[n] - 32;
        }
    }
if(keyboard_check(ord("D")))
    {
    x += global.pl_speed;
    if(x >= global.room_bdx2[n] -32)
        {
        x = global.room_bdx2[n] -32;
        }
    }
}
which is actually a lot of work and will not work with obsticles XD.
which i was working on next. You just saved the forms form another noob question.
Thanks a lot
 
A

azuarf

Guest
i still fully do not understand what you did there though. can you explain a bit.
what dose "(keyboard_check(ord("D")) - keyboard_check(ord("A")))" actually do and why dose it work?

from my point of view you just substracted a key input from a key input and multiplied it with a variable. . .... i cant understand it. but it works perfectly.
 
A

azuarf

Guest
Those functions return true or false - 1 or 0, respectively. It's then just basic math, so the rest should be easy to figure out.
oh i get it. if a is pressed then its 1 - 0 *speed = +x
for d its 0 - 1 * speed = -x
a+d = 1-1*speed = 0x
then it adds to the x axis if wall is not meeting with player in x direction.
right?
i did not know key press could be used as variables thanks you :D
 
Last edited by a moderator:
Top