2D Top-down Grid-based Collision Mayhem

Hello,

I posted this in physics but it got no replies so maybe it will here :frowning:

I have been trying to set up 2d collision with certain constraints for my game for quite a while now but all of my attempts have failed, so maybe someone can help.

Game Design:
Each game object (player or enemy) has a 2D box collider with square size of 1 and rigid body attached to it.
Movement in-game is always by 1 and exactly 1 (if player is in position {0,0} moving right makes his position {1, 0}) (Grid-based movement without snapping)

Problems on collision with other players or enemies:

  1. Player pushing other players
  2. False collision
  3. Starting position which is snapped to a grid won’t be snapped to grid anymore.

Attempts:

  1. Freeze other units when player is moving to stop the pushing which introduces the false colliders problem when moving around other units due to the default offset.
    If I decrease the offset (minimum is 0.0001) and decrease the enemy’s collider to 0.9 for example, the player won’t falsely collide with it when moving around it but if it tries to move towards the enemy, the position will be changed from {0,0} to {0,0.1} for example which will ruin my movement(grid-based movement) and the error will keep adding up until the player will start moving on square edges instead of the middle of a square(cell).
  2. Check surroundings of player by tags and not move him if there’s an enemy in the cell he wants to move to or an obstacle. (maybe high computations every single move from every single unit in the game which will make using colliders useless as well)

Summary:
I tried to solve pushing other units by freezing other units when player is moving but now it falsely collides with other units that are frozen (e.g. Player pos (0,0) Enemy pos (1,1) player will still collide), if I reduce box collider size the player won’t collide with the enemy but if the player tries to move towards (1,1) by going right (1, 0) then up (1,1) the player won’t be able to push the enemy but its position will change to be (1.1,0) which should be (1,0) so the error will build up and there won’t be grid based movement.

While I’m writing this, I’m thinking maybe I need to get rid of Unity’s default grid and find or code my own grid system to avoid using collisions in the first place?

you could also code your own physics and collisions, if you have a cell system you can always know what entity exists in a given cell

when your player would move to a certain cell, you check that cell, if there is something there > you check if it can be pushed, if it can you push it if it cant > you dont allow the player to move to the cell he wanted to ( its blocked )

1 Like

Thank you @rarac