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

[HELP!] I need help with creating a collision with wall for my topdown game

P

ProductivInc

Guest
Hello!

I'm totally a beginner when it comes to GML, I recently followed a youtube tutorial on how to make a smooth movement. Now I want to make a smooth collision with my object (wall), I just don't know what code to use. Can someone help me by giving me a code that suits my movement code down below?

Here are some details about my movement code, I just need a collision code with my wall:

//So these are my keywords, I just followed a random youtube video.
keybinding.png

My next code (STEP/MOVEMENT CODE):

step code.png



I appreciate if you could help me:)
 

Anixias

Member
Try moving your object 1 pixel at a time.

1. Add 1 to x (-1 if going left) if axis_x > 0
2. axis_x -= 1 (-1 if going left)
3. Repeat for y and axis_y
4. Repeat this until you hit a wall object (if in the x/axis_x stage, set axis_x to 0. If in the y/axis_y stage, set axis_y to 0)

I have a script that does exactly that but it is needlessly complicated as I wrote it for a specific type of game. I'll post the code but you'll have to edit some stuff to make it work for you:
Code:
/// @param obstacle
var obstacle = argument0;
//var line = collision_line(x,y,x+hspd*system.delta,y+vspd*system.delta,obstacle,false,true);
if (!instance_exists(obstacle))
{
    x += hspd*system.secPass;
    y += vspd*system.secPass;
    return;
}
var moved = false;
var hrep = true;
var vrep = true;
var ohspd = hspd;
var ovspd = vspd;
var ocol = instance_create_depth(-100,-100,0,par_collision);
    
hspd *= system.secPass;
vspd *= system.secPass;
    
if (abs(hspd) > 1000 || abs(vspd) > 1000) return; //Moving extremely fast... This would take an enormous amount of time to run
    
//All Collisions
    
var c = 0;
var maxc = max(ceil(abs(hspd)),ceil(abs(vspd)))*2;
var list = ds_list_create();
while (get_speed() > 0 && c < maxc)
{
    //Horizontal Collision
    var h = sign(hspd)*min(1,abs(hspd));
    instance_place_list(x+h,y,obstacle,list,false);
    var col = false;
    while(ds_list_size(list) > 0)
    {
        var o = list[| 0];
        ds_list_delete(list,0);
        if (instance_exists(o))
        {
            col = true;
            break;
        }
    }
    ds_list_clear(list);
    if (!col)
    {
        if (abs(hspd) > 0)
        {
            x += h;
            moved = true;
            hspd -= h;
            h = sign(hspd)*min(1,abs(hspd));
            c++;
        }
    }
    else
    {
        hspd = 0;
        hrep = false;
    }
    
    //Vertical Collision
    var v = sign(vspd)*min(1,abs(vspd));
    instance_place_list(x,y+v,obstacle,list,false);
    col = false;
    while(ds_list_size(list) > 0)
    {
        var o = list[| 0];
        ds_list_delete(list,0);
        if (instance_exists(o))
        {
            col = true;
            break;
        }
    }
    ds_list_clear(list);
    if (!col)
    {
        if (abs(vspd) > 0)
        {
            y += v;
            moved = true;
            vspd -= v;
            v = sign(vspd)*min(1,abs(vspd));
            c++;
        }
    }
    else
    {
        vspd = 0;
        vrep = false;
    }
}
    
if (hrep)
{
    hspd = ohspd;
}
if (vrep)
{
    vspd = ovspd;
}
instance_destroy(ocol);
ds_list_destroy(list);
Things to know about this script:
1. You don't need to spawn "ocol", or destroy it.
2. get_speed() is a custom script. You can replace this with point_distance(0,0,axis_x,axis_y)
3. Replace hspd with axis_x
4. Replace vspd with axis_y
5. system.secPass is used for delta timing. Delete that stuff if you don't use delta timing.
Please put this into a script, and not into the step event directly. Replace your x += axis_x and y+= axis_y with this script.
 
P

ProductivInc

Guest
I appreciate this, but I'm a complete beginner. Can you please clarify more and give me step by step what I should replace for example objects, variables and so on... It's a lot of stuff I don't understand what you gave me. Maybe if it's okay to you can you clear the code and give me a clean one and only the important part that gives me a smooth collision with my objWall? I don't need a movement code, I already find mine pretty good.

I would be grateful!

Thanks,
 

Anixias

Member
My script just moves the calling instance 1 pixel at a time alternating between horizontal and vertical. This makes it pixel perfect. You can probably just write something that moves 1px to the right or left over and over, reducing the horizontal speed by 1 at the same time, until the next location would be a wall or you've run out of speed. Then just do it again for the vertical axis. I just made mine alternate the x and y stuff because I wanted extremely laggy games to still accurately show you where your character moved to with delta timing.
 
Top