RigidBody2D losing X velocity on falling contact.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By avencherus
:warning: Old Version Published before Godot 3 was released.

Not sure if there is some better way of handling this problem.

I have player controlled RigidBody2D. When falling and trying to run to the right or left, the velocity in the X component is killed when I get it from state.get_linear_velocity().

This makes for some stuttering on landings. slide() can’t help either since the collision detection is doing this in between _integrate_forces(). What is the ideal way of working around this, am I overlooking something?

My current thoughts are that either I should never use get_linear_velocity(), or split out player movement, comparing and restoring any lost velocity frame by frame. Anyone have any insight into this problem or better solutions?

Are you using state.set_linear_velocity() to control the character? Have you tried setting friction on the RigidBody2D to 0?

mollusca | 2017-11-24 08:43

Yep friction is 0, sorry didn’t mention that. It is a stuttering, the movement will continue from 0. The other changed properties are:

Continuous CD = RayCast
Can Sleep = false

Though it doesn’t feel quite right, I’m working around in a way something like pseudo code below:

var prev_lv = Vector2()

func _integrate_forces(state):

	var curr_lv = state.get_linear_velocity()

	if(curr_lv.length() < 1.0 and prev_lv.x >= X_THRESHOLD):
		curr_lv.x = prev_lv.x

		# ... custom gravity, friction, etc.


	state.set_linear_velocity(curr_lv)
	prev_lv = curr_lv

avencherus | 2017-11-24 08:53

One thing I noticed is that if I set x-velocity with set_linear_velocity() but use the default RigidBody2D gravity and have continuous CD set to ray, I get a weird stutter when the character lands. If continuous CD is off or set to shape there’s no stutter. If I turn custom integration on and apply gravity myself I sometimes get the stutter with continuous CD set to ray, depending on gravity strength. The other CD modes, no stutter. Try turning continuous CD off and see if it makes any difference.

mollusca | 2017-11-24 10:51

I seem to get the odd little hitch no matter what I do. I can check for just landed and restore the X velocity, but there still are oddities in the sprite positioning. It seems to be ejected out 30 pixels or so for a frame. So the character art will still jolt about on landing.

I was considering casting a shape motion query, but then there is no obvious way to move a RigidBody without sleeping it, which will be a delayed frame too.

I would prefer to stick with a RigidBody, because of the easier interaction with other RigidBody nodes in the scene, but these results are looking quite bad at the moment. :frowning:

avencherus | 2017-11-24 11:00

Seems to be an issue with circle collision shapes and rectangles, but certain capsules work fine. Though the sprite is still jerked about.

avencherus | 2017-11-24 13:47