0 votes

Because if not...

My character size is 40x64 (because it was too fat in 64x64).

Tiles are 40x20 (i still struggle deciding between 16:9 or 4:3 with 32x32 tiles).

And enemies are 32x40 (because they were too small in 32x32 in my opinion).

Here's the link to my project:

in Engine by (117 points)
edited by

1 Answer

+1 vote

That displacement is issued by the physics engine when two CollisionShapes on the same layer overlap - because, well, it's the physics engine's job to prevent that. I'm not aware of a way to influence the direction of that displacement though.

So if you're unhappy with the default displacement, you'll need to prevent the collsion in the first place and have to do the displacement yourself. Here's a sketch:

func set_cell(cell_coordinates, tile_id):
    var cell_origin = $TileMap.map_to_world(cell_coordinates)
    var cell_size = $TileMap.cell_size

    if Rect2(cell_origin, cell_size).has_point($Player.position):
        $Player.position.y -= $TileMap.cell_size.y

    $TileMap.set_cellv(cell_coordinates, tile_id)

This checks if the area of the cell contains the current position of the player node and if that's the case it moves the player one cell up before it changes the tile.

Now obviously there are certain shortcoming here like...

  • it only checks for the player, but maybe you want to check for other objects (NPCs, enemies, items, etc.) as well. In that case you could move an Area2D to the cells position and use get_overlapping_bodies(). However, that requires you to wait for the next physics update, so you've to yield for 1 or 2 frames.
  • it only checks if the player's position is inside the cell, but even if that's not the case, parts of the player could still be inside the cell (depending on how your movement works, with a grid-based system that wouldn't happen of course!). You could solve this by using Rect2's encloses-method and calculating exactly which rectangular area the player covers. Or you check all the outmost points of the player individually for more complex, non-rectangular player shapes.

But I feel like it's hard to come up with a general answer to all those problems.

Btw: asking the same question over and over again just slightly paraphrased will not yield better or quicker answers - it just annoys the fuck out of everyone! However, investing the time to ask a well-structured question that includes what you've already tried, why it didn't work or fit your requirements as well as everything else you think could make the life easier for anyone trying to help you out (voluntarily, in their free time, on an unpaid basis) will increase the odds of an answer significantly!

For starters you could provide people with a copy of your project, so they don't have to replicate your setup first. Or at least you could give some details about the size of your cells and the player, how your scene tree looks or the movement of your player works. The less information there is, the less useful the answer will be as well!

by (10,153 points)

Thank you very much for your suggestions, i'll take them into account!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.