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

Strange thing in Place_meeting

E

Emre

Guest
Hi guys i have a question. I have this code in the step event of my player.
if keyboard_check(vk_right) and !place_meeting(x+5,y,obj_wall){
x += 5;
}

if keyboard_check(vk_left) and !place_meeting(x-5,y,obj_wall){
x -= 5;
}

if keyboard_check(vk_down) and !place_meeting(x,y+5,obj_wall){
y += 5;
}

if keyboard_check(vk_up) and !place_meeting(x,y-5,obj_wall){
y -= 5;
}

The collision does work but its not pixelperfect for vertical collisions while it is pixelperfect for horizontal collisions. Here is how it looks like:

Capture1.PNG Capture2.PNGCapture3.PNG Capture4.PNG

As you can see in the images above the vertical collision is different than the horizantal collision which is strange since i use the same piece of code for both. I really want to know why this happens. It has nothing to do with the sprite mask. I have no other code in other objects or events. This is literally all the code i have in my game. Please someone explain.
 
E

Ethanicus

Guest
You'll want to use move_contact_solid to make the player stick to the wall. Currently he's checking if the place is free, finding it's not, then staying still.
 
D

Docker

Guest
move_contact
That only applies if hes using the solid tick option

Your vertical is the one that's 'pixel perfect' not your horizontal judging by the pictures. You're not going to get pixel perfect because your code will not allow it to move any closer if it is 4 pixels or less away from the object.

My guess as to why vertical is allowing pixel perfect collision is simply because the width of the room is a factor of 5 whereas your height is not.

Most people using place_meeting for collision normally put a while statement inside their if statement saying:
Code:
while (!place_meeting(x+sign(hspd), y, obj_wall){
   x += sign(hspd)
}
}
Code:
while (!place_meeting(x, y+sign(vspd), obj_wall){
   y += sign(vspd)
}
}
As you are changing the x and y position directly, just modify the above while code with x+1, y+1, x-1, y+1.

That means that when it detects a collision with obj_wall within 5 pixels, it will continue to move 1 pixel until the collision is 1 pixel away.
 
E

Emre

Guest
You'll want to use move_contact_solid to make the player stick to the wall. Currently he's checking if the place is free, finding it's not, then staying still.
Yes but its not doing the same thing with horizontal collision. My goal is not to make the player stick to the wall. I am just trying to understand why the same code acts different. Thank you for the reply :). I will experiment with move_contact and see how that works.
 
E

Emre

Guest
That only applies if hes using the solid tick option

Your vertical is the one that's 'pixel perfect' not your horizontal judging by the pictures. You're not going to get pixel perfect because your code will not allow it to move any closer if it is 4 pixels or less away from the object.

My guess as to why vertical is allowing pixel perfect collision is simply because the width of the room is a factor of 5 whereas your height is not.

Most people using place_meeting for collision normally put a while statement inside their if statement saying:
Code:
while (!place_meeting(x+sign(hspd), y, obj_wall){
   x += sign(hspd)
}
}
Code:
while (!place_meeting(x, y+sign(vspd), obj_wall){
   y += sign(vspd)
}
}


As you are changing the x and y position directly, just modify the above while code with x+1, y+1, x-1, y+1.

That means that when it detects a collision with obj_wall within 5 pixels, it will continue to move 1 pixel until the collision is 1 pixel away.
Thank you for the reply. This code you said will work and make a good collision system but I dont really understand what you mean with the factor 5.
 
E

Emre

Guest
I am very surprised that i got superfast replies. First time i ask question on forum. Will do it more often from now on.
 
D

Docker

Guest
Thank you for the reply. This code you said will work and make a good collision system but I dont really understand what you mean with the factor 5.
The player moves 5 pixels, so as long as the rooms width is set up in such a way that the final movement to touch either wall is 5 pixels away then you will get perfect collision, any change to that rooms width would produce the same result as your original horizontal collision unless it still produces a final movement of 5 pixels. I can't think of a better way to explain it sorry.
 
E

Emre

Guest
The player moves 5 pixels, so as long as the rooms width is set up in such a way that the final movement to touch either wall is 5 pixels away then you will get perfect collision, any change to that rooms width would produce the same result as your original horizontal collision unless it still produces a final movement of 5 pixels. I can't think of a better way to explain it sorry.
I tried out this:

if keyboard_check(vk_right) and !place_meeting(x+5,y,obj_wall){
x += 5;
}else{
while !place_meeting(x+1,y,obj_wall){
x += 1;
}
}

It doesnt work.
 
D

Docker

Guest
Your code will need rewriting as well.
Code:
if (keyboard_check(vk_right)) {
   if (!place_meeting(x+5,y,obj_wall)){
   x += 5;
   } else {
      while (!place_meeting(x+1,y,obj_wall)){
         x += 1;
      }
   }
}
This executes like this:
Code:
if player presses right
   if space 5 pixels to the right
      move 5 pixels to the right
   else there is not space 5 pixels to the right
      while there is space 1 pixel to the right
         move 1 pixel to the right
I'd also recommend watching heartbeasts RPG tutorials on youtube, he will show you a different way of writing up, down, left, right movement and collision that is similar to what I use and I find it much cleaner, it also gives you more control of your game by using variables rather than hard coded figures such as 5.
 
Last edited:
C

CoderJoe

Guest
Another thing you can do is loop through the max amount of space you want to move (i think 5 is what you are trying to do) and check each space starting with the closest. Then move that many spaces. So check 1 pixel away, if empty, check 2 pixels away, etc... Then move that many pixels. This should give you pixel perfect movement (I often use it for platforming games)
 
E

Emre

Guest
Another thing you can do is loop through the max amount of space you want to move (i think 5 is what you are trying to do) and check each space starting with the closest. Then move that many spaces. So check 1 pixel away, if empty, check 2 pixels away, etc... Then move that many pixels. This should give you pixel perfect movement (I often use it for platforming games)
Thx for the tip.
 
E

Emre

Guest
Your code will need rewriting as well.
Code:
if (keyboard_check(vk_right)) {
   if (!place_meeting(x+5,y,obj_wall)){
   x += 5;
   } else {
      while (!place_meeting(x+1,y,obj_wall)){
         x += 1;
      }
   }
}
This executes like this:
Code:
if player presses right
   if space 5 pixels to the right
      move 5 pixels to the right
   else there is not space 5 pixels to the right
      while there is space 1 pixel to the right
         move 1 pixel to the right
I'd also recommend watching heartbeasts RPG tutorials on youtube, he will show you a different way of writing up, down, left, right movement and collision that is similar to what I use and I find it much cleaner, it also gives you more control of your game by using variables rather than hard coded figures such as 5.
Usually i do use variables but in this case i am not working on a game. I just created a new project to test just this code. Then i would delete it. Thats why i didnt use variables. But usually i do if i am working on a game.
 
Top