GML Move Towards Point and Collision

M

mentalredux

Guest
Hello there, I'm working on making my first game and I need a little help. I have the playable character's movement working perfectly and now I am working on adding walls and other objects for the player to interact with. I am using a step function for movement and I'm not sure where to add the code that checks if the place is free before the object tries to move to the location. Any help would be greatly appreciated as well as criticism, thank you!

GML:
move_towards_point(mouse_x,mouse_y,2);
if distance_to_point (mouse_x,mouse_y)>200{speed=10};
else
if distance_to_point (mouse_x,mouse_y)>170{speed=8};
else
if distance_to_point (mouse_x,mouse_y)>150{speed=7};
else
if distance_to_point (mouse_x,mouse_y)>50{speed=4};
else
if distance_to_point (mouse_x,mouse_y)>25{speed=3};
else
if distance_to_point (mouse_x,mouse_y)>5{speed=2};
else
if distance_to_point (mouse_x,mouse_y)>3{speed=1};
else
if distance_to_point (mouse_x,mouse_y)<1{speed=0};


dir=floor(((point_direction(x+8,y+8,mouse_x,mouse_y)+22.5)%360)/45)
if(dir==0){
    sprite_index=spr1_r;
}
if(dir==1){
    sprite_index=spr1_ur;
}
if(dir==2){
    sprite_index=spr1_u;
}
if(dir==3){
    sprite_index=spr1_ul;
}
if(dir==4){
    sprite_index=spr1_l;
}
if(dir==5){
    sprite_index=spr1_dl;
}
if(dir==6){
    sprite_index=spr1_d;
}
if(dir==7){
    sprite_index=spr1_dr;
}
 

sp202

Member
You'd replace move_towards_point with something like this:
GML:
var dir=point_direction(x,y,mouse_x,mouse_y)
var spd=10 //The speed to move at
if !place_meeting(x+lengthdir_x(spd,dir),y+lengthdir_y(spd,dir),objWall)
{
    x+=lengthdir_x(spd,dir)
    y+=lengthdir_y(spd,dir)
}
else
{
    while(!place_meeting(x+lengthdir_x(1,dir),y+lengthdir_y(1,dir),objWall))
    {
        x+=lengthdir_x(1,dir)
        y+=lengthdir_y(1,dir)
    }
}
I don't recommend using the built in speed variable as then GM will automatically move the object each step before you've had a chance to do any collision checking.
 
If you're wanting to ease the speed a bit you can accomplish that like so:

GML:
if (point_distance(x, y, mouse_x, mouse_y) > 1){spd = point_distance(x, y, mouse_x, mouse_y)/20;}

//Adjust the '20' at the end to fine-tune the ease-in movement. Use a lower number to make it faster or a higher number to make it slower.
Or if you wanna straight-up use this similar method using 'move_towards_point':
GML:
if (point_distance(x, y, mouse_x, mouse_y) > 1){move_towards_point(mouse_x, mouse_y, point_distance(x, y, mouse_x, mouse_y)/20);}
else (x = mouse_x; y = mouse_y;)
 

kburkhart84

Firehammer Games
If you want to continue using the built-in movement, you could utilize the xprevious and yprevious built-in variables as well. You would have to add in collision response to what you need as well.

Add a collision event for the things your player is going to run into. I recommend you take a look at how to use object inheritance so you could simplify all the different walls into a single parent to make it need only the one collision event for all the walls. Either way, add that collision event, and when it happens, you can set x and y to the xprevious and yprevious. From there, depending on the movement speed, you may want to use functions like instance_place() to scoot the player as close to the wall as it can get without actually entering the wall, but it depends on how you want to handle collision if you want to make them as accurate as possible as far as the response.
 
M

mentalredux

Guest
Would there be a way to make the wall object push the player by a pixel or two before they touch?
 
Top