[SOLVED] How do you move a rigidbody2d to a location using func _integrate_forces(state) without jitter?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Richardv07

So I’ve got a rigidbody2d, and when the player runs over it, it begins to follow the player.

using func _integrate_forces(state): I’m getting the correct direction, then applying an impulse in that direction * velocity.

Here’s the code https://i.imgur.com/FVL8tI0.png
While the player’s actively moving, everything is smooth and looks great, but the issue is that when the player stops and the rigidbodies catch up to their intended target, of course, is that since I’m applying an impulse every time the function updates, it causes them to jitter when they shouldn’t be moving.

I need these rigidbody2ds to follow the player, but also be affected by area2d gravity traps which will try to suck the rigid bodies in and destroy them, and I’m completely at a loss as to how I can have them smoothly follow the player, but still be affected by gravity. I’m open to alternatives as well.

I scripted something similar in unreal engine (that updates every tick) and it didn’t jitter when stationary, so while I can get seemingly why it’s jittering, I can’t think of a viable alternative.

Again, I’d really appreciate any help, because I’ve been tearing my hair out over this.

:bust_in_silhouette: Reply From: Richardv07

Hey all,
found a solution to the issue I was having where rigidbody2ds would jitter when following the player (they would jitter around their intended destination).

I was initially doing a check to see if the absolute distance to the target location was over a certain amount in both the X and Y of the vector. If so, it would apply an impulse.

Here’s what the jitter looked like: https://giant.gfycat.com/CloudyDelayedJackal.webm

Something like this:

func _integrate_forces(state):
	if updatingposition:
		if assignedasocket:
			followtarget = player.orbtargets[orbindex]
			var direction = (followtarget.global_position - global_position).normalized()
			if abs(direction.x) > .01 && abs(direction.y) > .01:
				apply_central_impulse(direction * 700)

This had the result of making the rigidbody jitter or bounce back and forth at distances equivalent to the .01 or whatever value I placed in the check.

Here it is with no jitter: https://giant.gfycat.com/QuarrelsomeFrailArmyworm.webm

The correct solution was:

func _integrate_forces(state):
	if updatingposition:
		if assignedasocket:
			followtarget = player.orbtargets[orbindex]
			var direction = (followtarget.global_position - global_position).normalized()
			var distance_to_player = global_position.distance_to(followtarget.global_position)
			if distance_to_player > 10 :
				apply_central_impulse(direction * 700)    

Basically, I needed to check distance_to and play around until I found a happy medium distance wise. I’m sure there’s probably a cleaner and easier way to do this, but if anyone else runs into a similar issue, this is what solved it for me.