Collision detection - friction

Godot Version



I have added physics layer and that alone lets me draw tiles in my tile-set which can be collided with. I am using CharacterBody2D and a CollisionShape2D with Sprite2D.

I have a few problems:

  • the character pushes 1px into the wall before stopping. I’d prefer it stopped before the wall. I am using code like this in CharacterBody2D:
func _physics_process(delta):
	velocity = Vector2.ZERO

	if Input.is_action_pressed("ui_left"): velocity.x -= speedMove
	if Input.is_action_pressed("ui_right"): velocity.x += speedMove
	if Input.is_action_pressed("ui_up"):	velocity.y -= speedMove
	if Input.is_action_pressed("ui_down"): velocity.y += speedMove


The code is simplified but it is what is needed to reproduce the problem.

I’ve tested it with 2.5 camera zoom and it seems to be more of an issue with “camera smoothing” on. With 2.0 zoom, or 3.0 it is not visible. With 5.0 it is visible again.

  • another problem is - if I press arrow left, the character moves at certain speed, but if it is hitting the wall below (or above) and I press diagonal movement (left + down, or left + up) it doubles the speed of movement (horizontally, since it won’t pass the wall). but if i remove collision, it seems that diagonal speed is as expected. I’ve tested the speed and it really moves faster when facing the wall than when moving diagonally.

  • finally, i’m wondering about physics material. some of my tiles are “ice” and i see that it has friction but when enabling it, it only collides with that tile as if it is a wall. What is the friction for? I have alternative method of doing it, by using custom data layer and setting some tiles as “ice” but if it is possible with physics layer i don’t see if i’ll be going in the wrong direction doing more work than really necessary.

I’ve recorded the video to show the first 2 issues:

I think I solved the issue of pushing against the wall with some camera zoom values, the solution is just to lower the collision safe margin (CharacterBody2D) to as low as possible. It was on 0.08, now I’ve set it to 0.001

I’d be grateful for the rest of issues guidance.