Pixel movements and collisions

Hello, I’m currently working on a “maze” top-down view game.
My cells are 16x16 pixels, and I’d like my player to move from cells to cells by “jumping” from one cell to another. Of course, I’d also like it to be impossible for him to pass through the walls…

So far, I’ve tried 3 different systems for moving, but none of them work as well as I’d like in terms of collisions.

Method 1

I’ve implemented the “PixelPerfectTopDownMovement” behavior on my player. With this behavior, the character “slides” and respects the distance in each moove.
In my events I set the condition “If player collides with wall” → Separate player and wall.

The problem is when the character “bumps” into a collision. Once done, if I then give him the right direction, he’ll make 2 moves instead of one.

Example: A wall is at the top, empty space to the left. I do up arrow → it doesn’t move (OK), then I do left arrow, it moves left then moves up by itself.

I’ve also noticed that sometimes with collisions the character tends to “shift” by a pixel

ezgif-7-5e5c957cb5

Method 2

I then tested another method of movement.

In my events, I assigned keys to directions and applied 1000px forces to my player on his X or Y axis depending on the directions I wanted to give him.

I also set the condition “If player collides with wall” → Separate Player and Wall”.

With this my character “jumps” well from square to square as I’d like… but he also jumps over walls…

ezgif-7-6439dd73fc

Method 3

Finally, I had tried changing the X or Y variable by adding or removing 16 pixels, but as a result, it goes over the walls.

Does anyone have a solution for this? Thanks a lot

Basic idea should be
You create 1st fake object (can be hidden) in place where you want to move to
IF its NOT in collision with wall you allow movement

I would consider using custom points on objects
But creating custom object is also cool idea

1 Like

For tile based movement I use a separate object to check if the space is empty before moving. Basically you just position the object on the destination tile, then check any necessary collisions or tile ID

1 Like

I will add my method. There are infinite ways to do things in GD.

I’m on my phone. I can’t use keyboard events. I used a joystick object. The keyboard method would be similar. It uses a variable named Angle to track movement. Set it to a default of -1. You can copy past most of the expression just make sure you change the X’s into Y’s.

I moved my object origin to the center. You could use the CenterX by changing the expression

It’s checking the target location for a wall or any other object or object group. The invert condition is a NOT condition.

Player.CenterX() + XFromAngleAndDistance(Angle, distance)

My grid was 64 x 64. You could change the 64s to 16.

1 Like

Thank you for you answer ! May I ask you how do you check if the space is empty ?

I will give this a try, thank you so much for your help !

1 Like

Thank you for your help, I will check the video and try your method ! I get back to you if it worked

1 Like

I am not sure I understood well of everything works… I copied everything but it didn’t work, I tried to change including “Center” ou reverse the X and the Y but none of them worked, could you explain me what’s wrong with my code…? Thank you very much for your help :

It looks like it should work. Are you using any behaviors on the player?

It checks for the wall object in the direction you’re trying to move. It adds the distance from the player using the angle.

The NOT inverts the result. So, if there’s an object then it inverts it and returns false. So, you can’t move. If there isn’t a wall then it would normally be false but the NOT inverts it and makes it true.

No I did all my test on a player without any behaviors :confused: But thank you ! At the end I managed to find a solution and I am moving my player this way, thank you for your help :

1 Like

Just FYI, you could create just one Pointeur object at the beginning of the scene (or as part of the layout), and then move that around when needed. It doesn’t really matter in this case but it could help performance in cases where you are moving a lot of objects.

1 Like