RigidBody2D gets stuck in corners when changing the gravity

Godot Version

Godot 4.2.2

Question

I am making a game where the player moves a ball, a RigidBody2D, by changing the direction of gravity. However, the ball has a tendency to get stuck to walls, especially in corners, and I can’t figure out why. I’ve tried many different permutations of bounciness, roughness, and friction on both the walls and the ball; reducing friction helps but doesn’t entirely solve the issue. The ball has a circular collision shape (though I tried changing it to a square and it still gets stuck) and the walls are StaticBody2Ds. I’ve attached the relevant code of the gravity controller, an Area2D with SpaceOverride set to Replace covering the entire map. There’s essentially no other code in the project at the moment, so I’m very confused by this. I’ve also attached a gif of the issue. If anyone could help me with this it would be enormously appreciated.

extends Area2D

var grav = Vector2.DOWN

func _physics_process(delta):
	if Input.is_action_just_pressed("grav_down"):
		grav = Vector2.DOWN
	if Input.is_action_just_pressed("grav_up"):
		grav = Vector2.UP
	if Input.is_action_just_pressed("grav_left"):
		grav = Vector2.LEFT
	if Input.is_action_just_pressed("grav_right"):
		grav = Vector2.RIGHT
	gravity_direction = grav

wall stuck gif

Try to use jolt physics, godot built in physics is not so good.

This is not very helpful, the Godot Jolt physics doesn’t support 2D at the moment. I found a hacky solution to this issue; whenever the gravity changes, I give the ball an imperceptibly tiny impulse in the direction of gravity, which gets it unstuck.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.