Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jitter when colliding a corner (KinematicBody2D and CollisionShape2D) #64747

Open
pc9098 opened this issue Aug 22, 2022 · 3 comments
Open

Jitter when colliding a corner (KinematicBody2D and CollisionShape2D) #64747

pc9098 opened this issue Aug 22, 2022 · 3 comments

Comments

@pc9098
Copy link

pc9098 commented Aug 22, 2022

Godot version

v3.4.4-stable_win64

System information

Windows 10, i5-9600K, RTX 2060, 16GB RAM, GLES3

Issue description

``When I move my 2D character to any corner shape it jitters.

My player is: a KinematicBody2D with a Sprite and a CollisionShape2D (it happens in any shape: rectangle, capsule, circle, ...).
My player has a script attached.
The walls are: a StaticBody2D with a CollisionPolygon2D.

image_2022-08-22_203047419

image

Godot_v3.2022-08-20.16-35-11-47.mp4
extends KinematicBody2D

var vel = Vector2.ZERO

const MAX_SPD = 80

func _physics_process(delta):
	var input_vector = Vector2.ZERO
	input_vector.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
	input_vector.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
	input_vector = input_vector.normalized()
	
	vel = input_vector * MAX_SPD
	
	vel = move_and_slide(vel)

The prints on the wall:

"before(-80, 0)
after(6.303683, 7.879045)
Falsefloor
Truewall

before(-80, 0)
after(6.29903, -31.520666)
Falsefloor
Truewall

before(-80, 0)
after(-3.076349, 15.385308)
Falsefloor
Truewall"

etc

The prints on the floor:

"before(0, 80)
after(28.24184, -7.050445)
Falsefloor
Truewall

before(0, 80)
after(-18.827187, 4.720105)
Falsefloor
Truewall

before(0, 80)
after(-7.062556, -7.058139)
Falsefloor
Truewall"

etc

This bug only happens with a corner, but not a single wall

Steps to reproduce

Make the same scene as I did with the same script, and just collide with a corner-like (not a wall but 2 walls making a corner!)

Minimal reproduction project

p2.zip

@Calinou
Copy link
Member

Calinou commented Aug 22, 2022

This is likely an issue with your code, rather than the physics engine. In other words, your player is "flip-flopping" endlessly between two walls due to the instant acceleration and lack of friction.

Try ignoring movement if it's smaller than a certain amount, or implement some kind of movement acceleration/friction.

@pc9098
Copy link
Author

pc9098 commented Aug 22, 2022

I knew with acceleration it can be "fixed" (not really fixed but making the jittering so little that the screen can't show it), but I'd rather have a solution which implied pushing the character to the right place (right above the corner) instead of some units in the above

@Hiiamwilliam
Copy link

My understanding is that when you slide in those corners you always end up entering one wall then pushed away into the other wall. This is why it jitters. If this is the actual reason, there is no bug here.

Do you want an easy way of "fixing" this jitter? Instead of move_and_slide with default parameters, decrease max_slides to 2:

vel = move_and_slide(vel, Vector2.ZERO, false, 2)

This will likely have negative consequences if your game has moving platforms and other KinematicBody interactions, but it does stop this specific jittering.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants