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

GameMaker Issue with collisions

R

RossDear

Guest
Hi GameMaker Community!

So I'm having some troubles with collisions detection.
I'm using some basic code to make my walls tangible:
Code:
//Horizontal Collision (ok)
     if (place_meeting(x+hsp,y,obj_wall))
    {
        while (!place_meeting(x+sign(hsp),y,obj_wall))
        {
            x = x + sign(hsp);
        }
        hsp=0;
    }
    x = x + hsp;

//Vertical Collision (ok)
    if (place_meeting(x,y+vsp,obj_wall))
    {
        while (!place_meeting(x,y+sign(vsp),obj_wall))
        {
            y  = y + sign(vsp);
        }
        vsp=0;
    }
    y = y + vsp;
My issue is that the player will sometime goes to great speeds, so he might end up in a wall or even going directly throw it.
I guess it could be solved by comparing the position at the frame X and check if it will be in a wall at the distance corresponding to the frame X+1 and then reducing the speed to goes against the wall.

I might be overanalyzing that stuff. Is there an easier way to do that?
 

CloseRange

Member
I'm surprised he would end up in the wall. The going through it I understand as that can occur with any speed greater than the size of the wall (if the wall is 32 pixels and your speed is more than 32)
Actually I can imagine going inside walls if you are going diagonal. This is because this code doesn't check for both x/y in once single case.
Hmm try this maybe:
Code:
if(collision_line(x, y, x+hsp, y+ysp, obj_wall, false, false)) {
     var d = point_direction(hsp, ysp);
     var dx = lengthdir_x(1, d);
     var dy = lengthdir_y(1, d);
     while(!place_meeting(x+dx, y+dy, obj_wall)) {
          x += dx;
          y += dy;
     }
     hsp = 0;
     ysp = 0;
}
x += hsp;
y += ysp;
I just wrote this and didn't test it so it may or may not work. If not at least it can point you in the right direction.
 
R

RossDear

Guest
Ho I see what's that about, it look like a way more efficient way to do that and I can also handle the "tp throught wall" issue using collision_line.

Unfortunatly there is a problem with point_direction, the function need actuals points in space

Code:
if(collision_line(x, y, x+hsp, y+vsp, obj_wall, false, false))
{
    var wx = instance_nearest(x, y, obj_wall).x;
    var wy = instance_nearest(x, y, obj_wall).y;
    var d = point_direction(x+hsp, y+vsp, wx, wy);
    var dx = lengthdir_x(1, d);
    var dy = lengthdir_y(1, d);
    while(!place_meeting(x+dx, y+dy, obj_wall)) {
          x += dx;
          y += dy;
     }
     hsp = 0;
     vsp = 0;
}
x += hsp;
y += vsp;
So this is working yeah but exactly as my old code, so that's already a good improvement to have my number of lines for this part halved.

Thanks for your time! ^^

Edit: When wrote this I've tried with only the code on that post, while using both this one and my olders collisions detections it seems to improve itself a bit, but nothing revelant.
 
Last edited by a moderator:
Top