• Hey! Guest! The 39th GMC Jam will take place between November 26th, 12:00 UTC and November 30th, 12:00 UTC. Why not join in! Click here to find out more!

GML Top-down game wall collision

Alexir

Member
I'm creating a top down shooter, and I want walls that keep the player from going out of bounds. I've got some code set up that works pretty fine:

(Keep in mind, my player character is named Eddy)

//up and left
if(place_meeting(x+eddyspeed,y,obj_wall))
{
obj_Eddy.x = obj_Eddy.x - 5
}

if(place_meeting(x,y+eddyspeed,obj_wall))
{
obj_Eddy.y= obj_Eddy.y - 5
}

//down and right

if(place_meeting(x-eddyspeed,y,obj_wall))
{
obj_Eddy.x = obj_Eddy.x + 5
}

if(place_meeting(x,y-eddyspeed,obj_wall))
{
obj_Eddy.y= obj_Eddy.y + 5
}

It's a bit messy but works, except for the fact that sometimes the player character can diagonally clip through the wall by accident, which is obviously not something I want. I only have 1 speed variable, being eddyspeed, and not distinct ones for horizontal and vertical movement, so many of the other tutorials don't apply to me. Is there any cleaner method of getting this accomplished, or perhaps just a solution to the glitch? Any help is appreciated.
 

tagwolf

Member
One of the most common problems. Here's the collision code from my platformer tutorial (in the tutorial section of the forum). It should be easy enough to adapt.

Code:
// Collisions and stuck/overlap prevention
if (place_meeting(x + hspeed, y, obj_Block)) {
   while (!place_meeting(x + sign(hspeed), y, obj_Block)) {
      x += sign(hspeed);
   }
   hspeed = 0;
}
if (place_meeting(x, y + vspeed, obj_Block)) {
   while (!place_meeting(x, y + sign(vspeed), obj_Block)) {
      y += sign(vspeed);
   }
   vspeed = 0;
}
// Diagonal
if (place_meeting(x + hspeed, y + vspeed, obj_Block)) {
   while (!place_meeting(x + sign(hspeed), y + sign(vspeed), obj_Block)) {
      x += sign(hspeed);
      y += sign(vspeed);
   }
   hspeed = 0;
   vspeed = 0;
}
[code]
 

Alexir

Member
One of the most common problems. Here's the collision code from my platformer tutorial (in the tutorial section of the forum). It should be easy enough to adapt.

Code:
// Collisions and stuck/overlap prevention
if (place_meeting(x + hspeed, y, obj_Block)) {
   while (!place_meeting(x + sign(hspeed), y, obj_Block)) {
      x += sign(hspeed);
   }
   hspeed = 0;
}
if (place_meeting(x, y + vspeed, obj_Block)) {
   while (!place_meeting(x, y + sign(vspeed), obj_Block)) {
      y += sign(vspeed);
   }
   vspeed = 0;
}
// Diagonal
if (place_meeting(x + hspeed, y + vspeed, obj_Block)) {
   while (!place_meeting(x + sign(hspeed), y + sign(vspeed), obj_Block)) {
      x += sign(hspeed);
      y += sign(vspeed);
   }
   hspeed = 0;
   vspeed = 0;
}
[code]
I fit it into my code but it didn't work sadly. Here's what I did:

if (place_meeting(x + eddyspeed, y + eddyspeed, obj_wall)) {
while (!place_meeting(x + sign(eddyspeed), y + sign(eddyspeed), obj_wall)) {
x += sign(eddyspeed);
y += sign(eddyspeed);
}
eddyspeed = 0;

}
 

tagwolf

Member
I fit it into my code but it didn't work sadly. Here's what I did:

if (place_meeting(x + eddyspeed, y + eddyspeed, obj_wall)) {
while (!place_meeting(x + sign(eddyspeed), y + sign(eddyspeed), obj_wall)) {
x += sign(eddyspeed);
y += sign(eddyspeed);
}
eddyspeed = 0;

}
It should, but looking at your previous code it seems you aren't using hspeed and vspeed for movement which is a problem as it makes it harder to track where the player is going to be. It means you'll need to manually back the player out of the blocks instead of making speed 0. If you look at my full platformer movement tutorial you can see how to do movement using vspeed and hspeed or change my code to back your player out.

I think if you combine my evaluation with your previous code you can make it work. I see you doing an if evaluation on eddyspeed for both x and y positioning, I'm not sure how that would track the players movement as you need to eval both it's x axis and y axis movement.

https://forum.yoyogames.com/index.p...fer-air-control-diagonal-collision-etc.65684/
 
Last edited:

Alexir

Member
It should, but looking at your previous code it seems you aren't using hspeed and vspeed for movement which is a problem. It means you'll need to manually back the player out of the blocks instead of making speed 0. If you look at my full platformer movement tutorial you can see how to do movement using vspeed and hspeed or change my code to back your player out.
I stated that I didnt have an indepedent vertical and horizontal variable, just a variable that handled both. How would I change the code to back my player out?
 
Top